"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ImgSizeSpecs = exports.ImgSizeAttr = void 0;
const tslib_1 = require("tslib");
const log_js_1 = require("@diplodoc/transform/lib/log.js");
const index_js_1 = tslib_1.__importDefault(require("@diplodoc/transform/lib/plugins/imsize/index.js"));
const is_number_1 = tslib_1.__importDefault(require("is-number"));
const ImageSpecs_1 = require("../../../markdown/Image/ImageSpecs/index.js");
const const_1 = require("./const.js");
Object.defineProperty(exports, "ImgSizeAttr", { enumerable: true, get: function () { return const_1.ImgSizeAttr; } });
const utils_1 = require("./utils.js");
const ImgSizeSpecs = (builder, opts) => {
    const placeholderContent = builder.context.get('placeholder')?.imgSize;
    if (!builder.hasNodeSpec(const_1.imageNodeName)) {
        builder.use(ImageSpecs_1.ImageSpecs);
    }
    builder.configureMd((md) => md.use(index_js_1.default, { log: log_js_1.log, enableInlineStyling: true }));
    builder.overrideNodeSpec(const_1.imageNodeName, (prev) => ({
        ...prev,
        attrs: {
            ...prev.attrs,
            [const_1.ImgSizeAttr.Height]: { default: null },
            [const_1.ImgSizeAttr.Width]: { default: null },
            [const_1.ImgSizeAttr.Id]: { default: null },
        },
        placeholder: placeholderContent ? { content: placeholderContent } : opts.placeholder,
        parseDOM: [
            {
                tag: 'img[src]',
                getAttrs(dom) {
                    const height = dom.getAttribute(const_1.ImgSizeAttr.Height);
                    const width = dom.getAttribute(const_1.ImgSizeAttr.Width);
                    return {
                        [const_1.ImgSizeAttr.Src]: dom.getAttribute(const_1.ImgSizeAttr.Src),
                        [const_1.ImgSizeAttr.Alt]: dom.getAttribute(const_1.ImgSizeAttr.Alt),
                        [const_1.ImgSizeAttr.Title]: dom.getAttribute(const_1.ImgSizeAttr.Title),
                        [const_1.ImgSizeAttr.Loading]: dom.getAttribute(const_1.ImgSizeAttr.Loading),
                        [const_1.ImgSizeAttr.Height]: (0, is_number_1.default)(height) ? height : null,
                        [const_1.ImgSizeAttr.Width]: (0, is_number_1.default)(width) ? width : null,
                        [const_1.ImgSizeAttr.Id]: dom.getAttribute(const_1.ImgSizeAttr.Id),
                    };
                },
            },
        ],
    }));
    builder.overrideMarkdownTokenParserSpec(const_1.imageNodeName, (prev) => ({
        ...prev,
        getAttrs: (tok) => ({
            [const_1.ImgSizeAttr.Src]: tok.attrGet(const_1.ImgSizeAttr.Src),
            [const_1.ImgSizeAttr.Title]: tok.attrGet(const_1.ImgSizeAttr.Title),
            [const_1.ImgSizeAttr.Height]: tok.attrGet(const_1.ImgSizeAttr.Height),
            [const_1.ImgSizeAttr.Width]: tok.attrGet(const_1.ImgSizeAttr.Width),
            [const_1.ImgSizeAttr.Loading]: tok.attrGet(const_1.ImgSizeAttr.Loading),
            [const_1.ImgSizeAttr.Alt]: tok.children?.[0]?.content || null,
            [const_1.ImgSizeAttr.Id]: tok.attrGet(const_1.ImgSizeAttr.Id),
        }),
    }));
    builder.overrideNodeSerializerSpec(const_1.imageNodeName, () => (0, utils_1.imageToMarkdown)({ renderExtra: utils_1.renderImgSizeExtra }));
};
exports.ImgSizeSpecs = ImgSizeSpecs;
//# sourceMappingURL=index.js.map
