"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ColorSpecs = exports.colorType = exports.colorMarkName = void 0;
const tslib_1 = require("tslib");
const color_1 = tslib_1.__importDefault(require("../../../../markdown-it/color.js"));
const schema_1 = require("../../../../utils/schema.js");
const const_1 = require("./const.js");
var const_2 = require("./const.js");
Object.defineProperty(exports, "colorMarkName", { enumerable: true, get: function () { return const_2.colorMarkName; } });
exports.colorType = (0, schema_1.markTypeFactory)(const_1.colorMarkName);
function getColorName(className) {
    const regexp = /^(yfm|md)-colorify--(\w+)$/;
    return className.match(regexp)?.[2];
}
const ColorSpecs = (builder, opts) => {
    const { validateClassNameColorName, parseStyleColorValue } = opts;
    builder
        .configureMd((md) => md.use(color_1.default, { defaultClassName: const_1.colorClassName, inline: false }))
        .addMark(const_1.colorMarkName, () => ({
        spec: {
            attrs: { [const_1.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 {
                                    [const_1.colorMarkName]: color,
                                };
                            }
                        }
                        return false;
                    },
                },
                {
                    style: 'color',
                    preserveWhitespace: false,
                    getAttrs(node) {
                        if (typeof node !== 'string')
                            return false;
                        const color = parseStyleColorValue?.(node);
                        if (color) {
                            return {
                                [const_1.colorMarkName]: color,
                            };
                        }
                        return false;
                    },
                },
            ],
            toDOM(node) {
                const colorValue = node.attrs[const_1.colorMarkName];
                return [
                    'span',
                    {
                        class: [const_1.colorClassName, `${const_1.colorClassName}--${colorValue}`].join(' '),
                        [const_1.domColorAttr]: colorValue,
                    },
                    0,
                ];
            },
        },
        fromMd: {
            tokenSpec: {
                name: const_1.colorMarkName,
                type: 'mark',
                getAttrs(token) {
                    return {
                        [const_1.colorMarkName]: token.info,
                    };
                },
            },
        },
        toMd: {
            open: (_state, mark) => {
                return `{${mark.attrs[const_1.colorMarkName]}}(`;
            },
            close: ')',
            mixable: true,
            expelEnclosingWhitespace: true,
        },
    }));
};
exports.ColorSpecs = ColorSpecs;
//# sourceMappingURL=index.js.map
