import { findAndReplace } from "hast-util-find-and-replace";
import { h } from "hastscript";
const RE_EMOJI = /:([\w-]+):/g;
const DEFAULT_OPTIONS = {
    sourceFile: "/assets/emojis.png",
    hidpiSourceFile: undefined,
    emojis: {},
    size: 64,
    class: "custom-emoji",
};
const plugin = (options) => {
    const settings = Object.assign({}, DEFAULT_OPTIONS, options);
    const srcset = settings.hidpiSourceFile
        ? `${settings.sourceFile} 1x, ${settings.hidpiSourceFile} 2x`
        : undefined;
    const emojiElement = (x, y, label) => {
        return h("img", {
            className: settings.class,
            src: settings.sourceFile,
            srcset,
            style: `object-fit: none; object-position: -${x}px -${y}px; width: ${settings.size}px; height: ${settings.size}px`,
            alt: label,
            title: label,
        });
    };
    const replaceEmoji = (_, match) => {
        if (!(match in settings.emojis))
            return false;
        const emoji = settings.emojis[match];
        return emojiElement(emoji[0], emoji[1], match);
    };
    return (tree) => {
        findAndReplace(tree, [RE_EMOJI, replaceEmoji]);
    };
};
export default plugin;