"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ImgSizeSpecs = exports.ImgSizeAttr = void 0;
const tslib_1 = require("tslib");
const log_1 = tslib_1.__importDefault(require("@diplodoc/transform/lib/log"));
const imsize_1 = tslib_1.__importDefault(require("@diplodoc/transform/lib/plugins/imsize"));
const is_number_1 = tslib_1.__importDefault(require("is-number"));
const const_1 = require("../../../markdown/Image/const.js");
const const_2 = require("./const.js");
Object.defineProperty(exports, "ImgSizeAttr", { enumerable: true, get: function () { return const_2.ImgSizeAttr; } });
const ImgSizeSpecs = (builder, opts) => {
    const placeholderContent = builder.context.get('placeholder')?.imgSize;
    builder.configureMd((md) => md.use(imsize_1.default, { log: log_1.default, enableInlineStyling: true }));
    builder.addNode(const_1.imageNodeName, () => ({
        spec: {
            inline: true,
            attrs: {
                [const_2.ImgSizeAttr.Src]: {},
                [const_2.ImgSizeAttr.Alt]: { default: null },
                [const_2.ImgSizeAttr.Title]: { default: null },
                [const_2.ImgSizeAttr.Height]: { default: null },
                [const_2.ImgSizeAttr.Width]: { default: null },
                [const_2.ImgSizeAttr.Loading]: { default: null },
            },
            placeholder: placeholderContent ? { content: placeholderContent } : opts.placeholder,
            group: 'inline',
            draggable: true,
            parseDOM: [
                {
                    tag: 'img[src]',
                    getAttrs(dom) {
                        const height = dom.getAttribute(const_2.ImgSizeAttr.Height);
                        const width = dom.getAttribute(const_2.ImgSizeAttr.Width);
                        return {
                            [const_2.ImgSizeAttr.Src]: dom.getAttribute(const_2.ImgSizeAttr.Src),
                            [const_2.ImgSizeAttr.Alt]: dom.getAttribute(const_2.ImgSizeAttr.Alt),
                            [const_2.ImgSizeAttr.Title]: dom.getAttribute(const_2.ImgSizeAttr.Title),
                            [const_2.ImgSizeAttr.Loading]: dom.getAttribute(const_2.ImgSizeAttr.Loading),
                            [const_2.ImgSizeAttr.Height]: (0, is_number_1.default)(height) ? height : null,
                            [const_2.ImgSizeAttr.Width]: (0, is_number_1.default)(width) ? height : null,
                        };
                    },
                },
            ],
            toDOM(node) {
                return ['img', node.attrs];
            },
        },
        fromMd: {
            tokenSpec: {
                name: const_1.imageNodeName,
                type: 'node',
                getAttrs: (tok) => ({
                    [const_2.ImgSizeAttr.Src]: tok.attrGet(const_2.ImgSizeAttr.Src),
                    [const_2.ImgSizeAttr.Title]: tok.attrGet(const_2.ImgSizeAttr.Title),
                    [const_2.ImgSizeAttr.Height]: tok.attrGet(const_2.ImgSizeAttr.Height),
                    [const_2.ImgSizeAttr.Width]: tok.attrGet(const_2.ImgSizeAttr.Width),
                    [const_2.ImgSizeAttr.Loading]: tok.attrGet(const_2.ImgSizeAttr.Loading),
                    [const_2.ImgSizeAttr.Alt]: tok.children?.[0]?.content || null,
                }),
            },
        },
        toMd: (state, node) => {
            const attrs = node.attrs;
            let result = '![';
            if (attrs.alt) {
                result += state.esc(attrs.alt);
            }
            result += '](';
            if (attrs.src) {
                result += state.esc(attrs.src);
            }
            if (attrs.title) {
                result += ` ${state.quote(attrs.title)}`;
            }
            result += getSize(attrs);
            result += ')';
            state.write(result);
        },
    }));
};
exports.ImgSizeSpecs = ImgSizeSpecs;
function getSize({ width, height }) {
    if (width || height) {
        return ` =${width || ''}x${height || ''}`;
    }
    return '';
}
//# sourceMappingURL=index.js.map
