import { merge } from "lume/core/utils/object.ts";
import mtnt from "./pluginData/mutant.json" with { type: "json" }
import Site from "lume/core/site.ts";

export interface Options {
    /** Extensions processed by this plugin to extract the utility classes */
    extensions?: string[];

}

export const defaults: Options = {
    extensions: [".md", ".mdx", ".html"],
};

export default function (userOptions?: Options) {
    const options = merge(defaults, userOptions);
    return (site: Site) => {
        site.process(options.extensions, (pages) => {
            pages.forEach((page: any) => {
                const regex = /:(.*?):/g;
                let m: RegExpExecArray | null;
                while ((m = regex.exec(page.content as unknown as string)) !== null) {
                    const text = m[0].trim();
                    const textBetweenColons = m[1].trim();
                    // deno-lint-ignore ban-ts-comment
                    // @ts-ignore 
                    if (mtnt.some(item => item.short === textBetweenColons)) {
                        const emoji = mtnt.find(item => item.short === textBetweenColons).src
                        page.content = (page.content as unknown as string).replace(new RegExp(text, 'g'),
                            `<img class="markdown-emoji" src="/files/emojis/${emoji}" />`);
                        // deno-lint-ignore ban-ts-comment
                        // @ts-ignore
                        site.getOrCreatePage(`/files/emojis/${emoji}`)
                        // site.copy(`files/emojis/${emoji}`, `/files/emojis/${emoji}`)
                    }
                }
            });
        });
    };
}
