import { jsx as _jsx } from "react/jsx-runtime";
import { ReactWidgetDescriptor, removeDecoration } from "../../../behavior/WidgetDecoration/index.js";
import { normalizeUrlFactory } from "../../../markdown/index.js";
import { YfmFilesUploadProcessBase } from "../YfmFilePaste/upload.js";
import { fileType } from "../YfmFileSpecs/index.js";
import { FilePlaceholder } from "./view.js";
export const addWidget = (tr, deps, uploadFiles) => {
    return new FileWidgetDescriptor(tr.selection.from, deps, uploadFiles).applyTo(tr);
};
export const removeWidget = removeDecoration;
class FileWidgetDescriptor extends 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 = normalizeUrlFactory(deps);
    }
    renderWidgetView(view, getPos) {
        this.view = view;
        this.getPos = getPos;
        return (_jsx(FilePlaceholder, { onCancel: this.onCancel, onSubmit: this.onSubmit, onAttach: this.uploadFiles && this.onAttach }));
    }
    onCancel = () => {
        this.view.dispatch(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 = 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(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 = removeWidget(tr, this.decoId);
        this.view.dispatch(tr);
        this.view.focus();
    }
}
class YfmFileWidgetUploadProcess extends YfmFilesUploadProcessBase {
    skeletonInitPos;
    constructor(view, files, uploadHandler, position) {
        super(view, files, uploadHandler);
        this.skeletonInitPos = position;
    }
    getSkeletonInitPos() {
        return this.skeletonInitPos;
    }
}
//# sourceMappingURL=widget.js.map
