import { getLoggerFromState } from "../../../core/index.js";
import { batchUploadFiles, } from "../../../utils/upload.js";
export class FilesBatchUploadProcess {
    view;
    files;
    uploadHandler;
    logger;
    skeletonDescriptor;
    constructor(view, files, uploadHandler) {
        this.view = view;
        this.files = files;
        this.uploadHandler = uploadHandler;
        this.logger = 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 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));
    }
}
//# sourceMappingURL=upload.js.map
