"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeWidget = exports.addWidget = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const WidgetDecoration_1 = require("../../../behavior/WidgetDecoration/index.js");
const markdown_1 = require("../../../markdown/index.js");
const upload_1 = require("../YfmFilePaste/upload.js");
const YfmFileSpecs_1 = require("../YfmFileSpecs/index.js");
const view_1 = require("./view.js");
const addWidget = (tr, deps, uploadFiles) => {
    return new FileWidgetDescriptor(tr.selection.from, deps, uploadFiles).applyTo(tr);
};
exports.addWidget = addWidget;
exports.removeWidget = WidgetDecoration_1.removeDecoration;
class FileWidgetDescriptor extends WidgetDecoration_1.ReactWidgetDescriptor {
    domElem;
    deps;
    uploadFiles;
    widgetHandler = null;
    constructor(initPos, deps, uploadFiles) {
        super(initPos, 'file_placeholder');
        this.uploadFiles = uploadFiles;
        this.domElem = document.createElement('span');
        this.deps = deps;
    }
    getWidgetHandler(view, getPos) {
        if (!this.widgetHandler) {
            this.widgetHandler = new FileWidgetHandler({
                view,
                getPos,
                decoId: this.id,
                uploadFiles: this.uploadFiles,
            }, 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 = null;
    }
}
class FileWidgetHandler {
    view;
    getPos;
    decoId;
    uploadFiles;
    normalizeUrl;
    constructor({ decoId, view, getPos, uploadFiles }, deps) {
        this.decoId = decoId;
        this.view = view;
        this.getPos = getPos;
        this.uploadFiles = uploadFiles;
        this.normalizeUrl = (0, markdown_1.normalizeUrlFactory)(deps);
    }
    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.uploadFiles && this.onAttach }));
    }
    onCancel = () => {
        this.view.dispatch((0, WidgetDecoration_1.removeDecoration)(this.view.state.tr, this.decoId));
        this.view.focus();
    };
    onSubmit = (params) => {
        if (!params.src || !params.name)
            return;
        const href = this.normalizeUrl(params.src)?.url;
        const download = params.name;
        if (!href)
            return;
        const node = (0, YfmFileSpecs_1.fileType)(this.view.state.schema).create({ href, download });
        this.insertNodes([node]);
    };
    onAttach = async (files) => {
        if (!this.uploadFiles)
            return;
        const { view } = this;
        new YfmFileWidgetUploadProcess(view, files, this.uploadFiles, this.getPos()).run();
        view.dispatch((0, exports.removeWidget)(view.state.tr, this.decoId));
        view.focus();
    };
    insertNodes(fileNodes) {
        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();
    }
}
class YfmFileWidgetUploadProcess extends upload_1.YfmFilesUploadProcessBase {
    skeletonInitPos;
    constructor(view, files, uploadHandler, position) {
        super(view, files, uploadHandler);
        this.skeletonInitPos = position;
    }
    getSkeletonInitPos() {
        return this.skeletonInitPos;
    }
}
//# sourceMappingURL=widget.js.map
