import { Fragment, Slice } from 'prosemirror-model';
import { Plugin } from 'prosemirror-state';
import { dropPoint } from 'prosemirror-transform';
import { isFunction } from "../../../../lodash.js";
import { clipboardUtils } from "../../../behavior/Clipboard/index.js";
import { imageType } from "../../../markdown/index.js";
import { createImageNode } from "../../ImgSize/utils.js"; // TODO: remove hard import
import { fileType } from "../YfmFileSpecs/index.js";
import { YfmFilesUploadProcessBase } from "./upload.js";
const { isFilesOnly, isFilesFromHtml, isImageFile } = clipboardUtils;
export const YfmFilePaste = (builder, opts) => {
    if (!opts || !isFunction(opts.fileUploadHandler))
        throw new Error('YfmFilePaste extension: fileUploadHandler is not a function');
    builder.addPlugin(() => new Plugin({
        props: {
            handleDOMEvents: {
                paste(view, e) {
                    const files = getPastedFiles(e.clipboardData);
                    if (files) {
                        e.preventDefault();
                        new YfmFilesPasteUploadProcess(view, files, {
                            uploadHandler: opts.fileUploadHandler,
                            needToSetDimensionsForUploadedImages: opts.needToSetDimensionsForUploadedImages,
                        }).run();
                        return true;
                    }
                    return false;
                },
                drop(view, e) {
                    // handle drop files from device
                    if (view.dragging)
                        return false;
                    const files = getPastedFiles(e.dataTransfer);
                    if (!files)
                        return false;
                    const dropPos = view.posAtCoords({ left: e.clientX, top: e.clientY })?.pos ?? -1;
                    if (dropPos === -1)
                        return false;
                    const posToInsert = dropPoint(view.state.doc, dropPos, createFakeFileSlice(view.state.schema));
                    if (posToInsert !== null) {
                        new YfmFilesPasteUploadProcess(view, files, {
                            pos: posToInsert,
                            uploadHandler: opts.fileUploadHandler,
                            needToSetDimensionsForUploadedImages: opts.needToSetDimensionsForUploadedImages,
                        }).run();
                    }
                    e.preventDefault();
                    return true;
                },
            },
        },
    }), builder.Priority.Low);
};
function getPastedFiles(data) {
    if (!data)
        return null;
    if (!isFilesOnly(data) && !isFilesFromHtml(data))
        return null;
    return Array.from(data.files);
}
function createFakeFileSlice(schema) {
    return new Slice(Fragment.from(fileType(schema).create({
        href: 'fake',
        download: 'file',
    })), 0, 0);
}
class YfmFilesPasteUploadProcess extends YfmFilesUploadProcessBase {
    pos;
    createImage;
    constructor(view, files, opts) {
        super(view, files, opts.uploadHandler);
        this.pos = opts.pos;
        const { schema } = this.view.state;
        if (imageType(schema)) {
            this.createImage = createImageNode(imageType(schema), {
                needDimensions: opts.needToSetDimensionsForUploadedImages,
            });
        }
    }
    getSkeletonInitPos() {
        return this.pos ?? this.view.state.tr.selection.from;
    }
    async createPMNode(res) {
        return isImageFile(res.file) && this.createImage
            ? this.createImage(res)
            : this.createFile(res);
    }
}
//# sourceMappingURL=index.js.map
