"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.YfmFilePaste = void 0;
const prosemirror_model_1 = require("prosemirror-model");
const prosemirror_state_1 = require("prosemirror-state");
const prosemirror_transform_1 = require("prosemirror-transform");
const lodash_1 = require("../../../../lodash.js");
const Clipboard_1 = require("../../../behavior/Clipboard/index.js");
const markdown_1 = require("../../../markdown/index.js");
const utils_1 = require("../../ImgSize/utils.js"); // TODO: remove hard import
const YfmFileSpecs_1 = require("../YfmFileSpecs/index.js");
const upload_1 = require("./upload.js");
const { isFilesOnly, isFilesFromHtml, isImageFile } = Clipboard_1.clipboardUtils;
const YfmFilePaste = (builder, opts) => {
    if (!opts || !(0, lodash_1.isFunction)(opts.fileUploadHandler))
        throw new Error('YfmFilePaste extension: fileUploadHandler is not a function');
    builder.addPlugin(() => new prosemirror_state_1.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 = (0, prosemirror_transform_1.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);
};
exports.YfmFilePaste = YfmFilePaste;
function getPastedFiles(data) {
    if (!data)
        return null;
    if (!isFilesOnly(data) && !isFilesFromHtml(data))
        return null;
    return Array.from(data.files);
}
function createFakeFileSlice(schema) {
    return new prosemirror_model_1.Slice(prosemirror_model_1.Fragment.from((0, YfmFileSpecs_1.fileType)(schema).create({
        href: 'fake',
        download: 'file',
    })), 0, 0);
}
class YfmFilesPasteUploadProcess extends upload_1.YfmFilesUploadProcessBase {
    pos;
    createImage;
    constructor(view, files, opts) {
        super(view, files, opts.uploadHandler);
        this.pos = opts.pos;
        const { schema } = this.view.state;
        if ((0, markdown_1.imageType)(schema)) {
            this.createImage = (0, utils_1.createImageNode)((0, markdown_1.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
