import mdPlugin from 'markdown-it-color';
import { markTypeFactory } from "../../../../utils/schema.js";
import { colorClassName, colorMarkName, domColorAttr } from "./const.js";
export { colorMarkName } from "./const.js";
export const colorType = markTypeFactory(colorMarkName);
function getColorName(className) {
    const regexp = /^(yfm|md)-colorify--(\w+)$/;
    return className.match(regexp)?.[2];
}
export const ColorSpecs = (builder, opts) => {
    const { validateClassNameColorName, parseStyleColorValue } = opts;
    builder
        .configureMd((md) => md.use(mdPlugin, { defaultClassName: colorClassName, inline: false }))
        .addMark(colorMarkName, () => ({
        spec: {
            attrs: { [colorMarkName]: {} },
            parseDOM: [
                {
                    tag: `span`,
                    preserveWhitespace: false,
                    getAttrs(node) {
                        if (typeof node === 'string')
                            return false;
                        for (const className of Array.from(node.classList)) {
                            const color = getColorName(className);
                            if (color && (validateClassNameColorName?.(color) ?? true)) {
                                return {
                                    [colorMarkName]: color,
                                };
                            }
                        }
                        return false;
                    },
                },
                {
                    style: 'color',
                    preserveWhitespace: false,
                    getAttrs(node) {
                        if (typeof node !== 'string')
                            return false;
                        const color = parseStyleColorValue?.(node);
                        if (color) {
                            return {
                                [colorMarkName]: color,
                            };
                        }
                        return false;
                    },
                },
            ],
            toDOM(node) {
                const colorValue = node.attrs[colorMarkName];
                return [
                    'span',
                    {
                        class: [colorClassName, `${colorClassName}--${colorValue}`].join(' '),
                        [domColorAttr]: colorValue,
                    },
                    0,
                ];
            },
        },
        fromMd: {
            tokenSpec: {
                name: colorMarkName,
                type: 'mark',
                getAttrs(token) {
                    return {
                        [colorMarkName]: token.info,
                    };
                },
            },
        },
        toMd: {
            open: (_state, mark) => {
                return `{${mark.attrs[colorMarkName]}}(`;
            },
            close: ')',
            mixable: true,
            expelEnclosingWhitespace: true,
        },
    }));
};
//# sourceMappingURL=index.js.map
