"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FilesBatchUploadProcess = void 0;
const core_1 = require("../../../core/index.js");
const upload_1 = require("../../../utils/upload.js");
class FilesBatchUploadProcess {
    view;
    files;
    uploadHandler;
    logger;
    skeletonDescriptor;
    constructor(view, files, uploadHandler) {
        this.view = view;
        this.files = files;
        this.uploadHandler = uploadHandler;
        this.logger = (0, core_1.getLoggerFromState)(view.state);
    }
    async run() {
        await this.insertSkeleton();
        await this.batchUpload();
        this.removeSkeleton();
    }
    async insertSkeleton() {
        const { view } = this;
        const { state } = view;
        this.skeletonDescriptor = await this.createSkeleton();
        let tr = state.tr;
        if (!state.selection.empty)
            tr = tr.deleteSelection();
        tr = this.skeletonDescriptor.applyTo(tr);
        view.dispatch(tr);
    }
    async batchUpload() {
        const insertQueue = [];
        await (0, upload_1.batchUploadFiles)(this.files, this.uploadHandler, (res) => {
            insertQueue.push((async () => {
                const node = await this.createPMNode(res);
                const { view } = this;
                const { schema } = view.state;
                view.dispatch(view.state.tr.insert(this.skeletonDescriptor.pos, [
                    schema.text(' '),
                    node,
                    schema.text(' '),
                ]));
            })().catch(console.error));
        });
        await Promise.allSettled(insertQueue);
    }
    removeSkeleton() {
        const { view } = this;
        view.dispatch(this.skeletonDescriptor.rmDeco(view.state.tr));
    }
}
exports.FilesBatchUploadProcess = FilesBatchUploadProcess;
//# sourceMappingURL=upload.js.map
