import { jsx as _jsx } from "react/jsx-runtime";
import isNumber from 'is-number';
import { ReactWidgetDescriptor, removeDecoration } from "../../../behavior/WidgetDecoration/index.js";
import { imageType, normalizeUrlFactory } from "../../../markdown/index.js";
import { ImgSizeAttr } from "../../../specs.js";
import { ImagesUploadProcess } from "../ImagePaste/upload.js";
import { ImagePlaceholder } from "./view.js";
export const addWidget = (tr, deps, opts) => {
    return new ImageWidgetDescriptor(tr.selection.from, deps, opts).applyTo(tr);
};
export const removeWidget = removeDecoration;
class ImageWidgetDescriptor extends ReactWidgetDescriptor {
    domElem;
    deps;
    uploadImages;
    needToSetDimensionsForUploadedImages;
    enableNewImageSizeCalculation;
    renderImageForm;
    widgetHandler = null;
    constructor(initPos, deps, opts) {
        super(initPos, 'image_placeholder');
        this.domElem = document.createElement('span');
        this.deps = deps;
        this.uploadImages = opts.uploadImages;
        this.renderImageForm = opts.renderImageForm;
        this.needToSetDimensionsForUploadedImages = opts.needToSetDimensionsForUploadedImages;
        this.enableNewImageSizeCalculation = opts.enableNewImageSizeCalculation;
    }
    getWidgetHandler(view, getPos) {
        if (!this.widgetHandler) {
            this.widgetHandler = new ImageWidgetHandler({
                view,
                getPos,
                decoId: this.id,
                uploadImages: this.uploadImages,
                renderImageForm: this.renderImageForm,
                needToSetDimensionsForUploadedImages: this.needToSetDimensionsForUploadedImages,
                enableNewImageSizeCalculation: this.enableNewImageSizeCalculation,
            }, 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;
    enableNewImageSizeCalculation;
    renderImageForm;
    cancelled = false;
    constructor({ decoId, view, getPos, uploadImages, renderImageForm, needToSetDimensionsForUploadedImages, enableNewImageSizeCalculation, }, deps) {
        this.decoId = decoId;
        this.view = view;
        this.getPos = getPos;
        this.uploadImages = uploadImages;
        this.normalizeUrl = normalizeUrlFactory(deps);
        this.renderImageForm = renderImageForm;
        this.needToSetDimensionsForUploadedImages = needToSetDimensionsForUploadedImages;
        this.enableNewImageSizeCalculation = enableNewImageSizeCalculation;
    }
    destruct() {
        this.cancelled = true;
    }
    renderWidgetView(view, getPos) {
        this.view = view;
        this.getPos = getPos;
        return (_jsx(ImagePlaceholder, { onCancel: this.onCancel, onSubmit: this.onSubmit, onAttach: this.uploadImages && this.onAttach, renderForm: this.renderImageForm }));
    }
    onCancel = () => {
        this.cancelled = true;
        this.view.dispatch(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 = {
            [ImgSizeAttr.Src]: url,
            [ImgSizeAttr.Title]: params.name,
            [ImgSizeAttr.Alt]: params.alt,
            [ImgSizeAttr.Width]: isNumber(params.width) ? String(params.width) : null,
            [ImgSizeAttr.Height]: isNumber(params.height) ? String(params.height) : null,
        };
        const node = imageType(this.view.state.schema).create(attrs);
        this.insertNodes([node]);
    };
    onAttach = async (files) => {
        if (this.cancelled || !this.uploadImages)
            return;
        const { view } = this;
        new ImagesUploadProcess(view, files, this.uploadImages, this.getPos(), {
            needDimensions: this.needToSetDimensionsForUploadedImages,
            enableNewImageSizeCalculation: this.enableNewImageSizeCalculation,
        }).run();
        view.dispatch(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 = removeWidget(tr, this.decoId);
        this.view.dispatch(tr);
        this.view.focus();
    }
}
//# sourceMappingURL=widget.js.map
