"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeWidget = exports.addWidget = void 0;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const is_number_1 = tslib_1.__importDefault(require("is-number"));
const WidgetDecoration_1 = require("../../../behavior/WidgetDecoration/index.js");
const markdown_1 = require("../../../markdown/index.js");
const specs_1 = require("../../../specs.js");
const upload_1 = require("../ImagePaste/upload.js");
const view_1 = require("./view.js");
const addWidget = (tr, deps, opts) => {
    return new ImageWidgetDescriptor(tr.selection.from, deps, opts).applyTo(tr);
};
exports.addWidget = addWidget;
exports.removeWidget = WidgetDecoration_1.removeDecoration;
class ImageWidgetDescriptor extends WidgetDecoration_1.ReactWidgetDescriptor {
    domElem;
    deps;
    uploadImages;
    needToSetDimensionsForUploadedImages;
    widgetHandler = null;
    constructor(initPos, deps, opts) {
        super(initPos, 'image_placeholder');
        this.domElem = document.createElement('span');
        this.deps = deps;
        this.uploadImages = opts.uploadImages;
        this.needToSetDimensionsForUploadedImages = opts.needToSetDimensionsForUploadedImages;
    }
    getWidgetHandler(view, getPos) {
        if (!this.widgetHandler) {
            this.widgetHandler = new ImageWidgetHandler({
                view,
                getPos,
                decoId: this.id,
                uploadImages: this.uploadImages,
                needToSetDimensionsForUploadedImages: this.needToSetDimensionsForUploadedImages,
            }, this.deps);
        }
        return this.widgetHandler;
    }
    getDomElem() {
        return this.domElem;
    }
    renderReactElement(view, getPos) {
        return this.getWidgetHandler(view, getPos).renderWidgetView(view, getPos);
    }
    remove() {
        super.remove();
        this.widgetHandler?.destruct();
        this.widgetHandler = null;
    }
}
class ImageWidgetHandler {
    view;
    getPos;
    decoId;
    uploadImages;
    normalizeUrl;
    needToSetDimensionsForUploadedImages;
    cancelled = false;
    constructor({ decoId, view, getPos, uploadImages, needToSetDimensionsForUploadedImages, }, deps) {
        this.decoId = decoId;
        this.view = view;
        this.getPos = getPos;
        this.uploadImages = uploadImages;
        this.normalizeUrl = (0, markdown_1.normalizeUrlFactory)(deps);
        this.needToSetDimensionsForUploadedImages = needToSetDimensionsForUploadedImages;
    }
    destruct() {
        this.cancelled = true;
    }
    renderWidgetView(view, getPos) {
        this.view = view;
        this.getPos = getPos;
        return ((0, jsx_runtime_1.jsx)(view_1.FilePlaceholder, { onCancel: this.onCancel, onSubmit: this.onSubmit, onAttach: this.uploadImages && this.onAttach }));
    }
    onCancel = () => {
        this.cancelled = true;
        this.view.dispatch((0, WidgetDecoration_1.removeDecoration)(this.view.state.tr, this.decoId));
        this.view.focus();
    };
    onSubmit = (params) => {
        if (this.cancelled)
            return;
        const url = this.normalizeUrl(params.url)?.url;
        if (!url)
            return;
        const attrs = {
            [specs_1.ImgSizeAttr.Src]: url,
            [specs_1.ImgSizeAttr.Title]: params.name,
            [specs_1.ImgSizeAttr.Alt]: params.alt,
            [specs_1.ImgSizeAttr.Width]: (0, is_number_1.default)(params.width) ? String(params.width) : null,
            [specs_1.ImgSizeAttr.Height]: (0, is_number_1.default)(params.height) ? String(params.height) : null,
        };
        const node = (0, markdown_1.imageType)(this.view.state.schema).create(attrs);
        this.insertNodes([node]);
    };
    onAttach = async (files) => {
        if (this.cancelled || !this.uploadImages)
            return;
        const { view } = this;
        new upload_1.ImagesUploadProcess(view, files, this.uploadImages, this.getPos(), {
            needDimensions: this.needToSetDimensionsForUploadedImages,
        }).run();
        view.dispatch((0, exports.removeWidget)(view.state.tr, this.decoId));
        view.focus();
    };
    insertNodes(fileNodes) {
        if (this.cancelled)
            return;
        let tr = this.view.state.tr;
        tr = tr.insert(tr.mapping.map(this.getPos()), fileNodes);
        tr = (0, exports.removeWidget)(tr, this.decoId);
        this.view.dispatch(tr);
        this.view.focus();
    }
}
//# sourceMappingURL=widget.js.map
