"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.YfmFileSpecs = exports.fileType = exports.YfmFileAttr = exports.yfmFileNodeName = void 0;
const file_extension_1 = require("@diplodoc/file-extension");
const schema_1 = require("../../../../utils/schema.js");
const const_1 = require("./const.js");
var const_2 = require("./const.js");
Object.defineProperty(exports, "yfmFileNodeName", { enumerable: true, get: function () { return const_2.yfmFileNodeName; } });
Object.defineProperty(exports, "YfmFileAttr", { enumerable: true, get: function () { return const_2.YfmFileAttr; } });
exports.fileType = (0, schema_1.nodeTypeFactory)(const_1.yfmFileNodeName);
const YfmFileSpecs = (builder) => {
    const directiveContext = builder.context.get('directiveSyntax');
    builder.configureMd((md) => md.use((0, file_extension_1.transform)({
        bundle: false,
        directiveSyntax: directiveContext?.mdPluginValueFor('yfmFile'),
    })));
    builder.addNode(const_1.yfmFileNodeName, () => ({
        spec: {
            group: 'inline',
            inline: true,
            attrs: const_1.fileNodeAttrsSpec,
            parseDOM: [
                {
                    tag: `a[class="${file_extension_1.FileClassName.Link}"]`,
                    getAttrs(p) {
                        const elem = p;
                        const attrs = {};
                        for (const name of const_1.KNOWN_ATTRS) {
                            const value = elem.getAttribute(name);
                            if (value) {
                                attrs[name] = value;
                            }
                        }
                        return attrs;
                    },
                    priority: 101,
                },
            ],
            toDOM(node) {
                const a = document.createElement('a');
                a.contentEditable = 'false';
                a.classList.add(file_extension_1.FileClassName.Link);
                for (const [key, value] of Object.entries(node.attrs)) {
                    if (value)
                        a.setAttribute(key, value);
                }
                const span = document.createElement('span');
                span.classList.add(file_extension_1.FileClassName.Icon);
                a.appendChild(span);
                a.append(node.attrs[file_extension_1.FileHtmlAttr.Download]);
                return a;
            },
        },
        fromMd: {
            tokenName: const_1.yfmFileNodeName,
            tokenSpec: {
                name: const_1.yfmFileNodeName,
                type: 'node',
                getAttrs: (tok) => {
                    const attrs = Object.fromEntries(tok.attrs || []);
                    attrs[const_1.YfmFileAttr.Markup] = tok.markup;
                    return attrs;
                },
            },
        },
        toMd: (state, node) => {
            if (directiveContext?.shouldSerializeToDirective('yfmFile', node.attrs[const_1.YfmFileAttr.Markup])) {
                state.write(serializeToDirective(node));
                return;
            }
            const attrsStr = Object.entries(node.attrs)
                .reduce((arr, [key, value]) => {
                if (key !== const_1.YfmFileAttr.Markup && value) {
                    if (key in const_1.LINK_TO_FILE_ATTRS_MAP) {
                        key = const_1.LINK_TO_FILE_ATTRS_MAP[key];
                    }
                    arr.push(`${key}="${value.replace(/"/g, '')}"`);
                }
                return arr;
            }, [])
                .join(' ');
            state.write(`${file_extension_1.FILE_MARKUP_PREFIX}${attrsStr} %}`);
        },
    }));
};
exports.YfmFileSpecs = YfmFileSpecs;
function serializeToDirective(node) {
    const filename = node.attrs[const_1.YfmFileAttr.Name] || '';
    const filelink = node.attrs[const_1.YfmFileAttr.Link] || '';
    let fileMarkup = `:file[${filename}](${filelink})`;
    const attrs = const_1.YFM_FILE_DIRECTIVE_ATTRS.reduce((acc, key) => {
        const value = node.attrs[key];
        if (value)
            acc.push(`${key}="${value}"`);
        return acc;
    }, []);
    if (attrs.length)
        fileMarkup += `{${attrs.join(' ')}}`;
    return fileMarkup;
}
//# sourceMappingURL=index.js.map
