import emojis from "../config/emojis.js";
import _ from "lodash";

interface EmojisIndex {
    [key: string]: string | EmojisIndex | EmojisIndex[];
}

const EMOJIPATHS: string[] = [];

function getEmojiPaths(obj: EmojisIndex, path: string[] = []) {
    for (const key in obj) {
        if (typeof obj[key] === "string") {
            EMOJIPATHS.push([...path, key].join("."));
        } else {
            getEmojiPaths(obj[key] as EmojisIndex, [...path, key]);
        }
    }
}
getEmojiPaths(emojis);

function getEmojiByName(name: (typeof EMOJIPATHS)[number], format?: string): string {
    const parts = name.split(".");
    let id: string | EmojisIndex | EmojisIndex[] | undefined = emojis;
    for (const part of parts) {
        if (typeof id === "string" || id === undefined) {
            throw new Error(`Emoji ${name} not found`);
        }
        if (_.isArray(id)) {
            id = id[parseInt(part)];
        } else {
            id = id[part];
        }
    }
    if (typeof id !== "string" && id !== undefined) {
        throw new Error(`Emoji ${name} not found`);
    }
    return getEmojiFromId(id, format);
}

function getEmojiFromId(id: string | undefined, format?: string): string {
    if (format === "id") {
        if (id === undefined) return "0";
        return id.toString();
    }
    if (id === undefined) {
        return "";
    } else if (id.toString().startsWith("a")) {
        return `<a:N:${id.toString().slice(1, id.toString().length)}>`;
    }
    return `<:N:${id}>`;
}

export default getEmojiByName;
