import { globalLogger } from "../../../../logger.js";
import { getProportionalSize, } from "../../../../utils/index.js";
import { FilesBatchUploadProcess } from "../../../behavior/utils/upload.js";
import { imageType } from "../../../markdown/index.js";
import { IMG_MAX_HEIGHT } from "../const.js";
import { createImageNode, loadImage } from "../utils.js";
import { ImageSkeletonDescriptor } from "./skeleton.js";
export class ImagesUploadProcess extends FilesBatchUploadProcess {
    createImage;
    initPosition;
    enableNewImageSizeCalculation;
    constructor(view, files, uploadHandler, position, opts) {
        super(view, files, uploadHandler);
        this.initPosition = position;
        this.createImage = createImageNode(imageType(this.view.state.schema), opts, this.logger);
        this.enableNewImageSizeCalculation = opts.enableNewImageSizeCalculation;
    }
    async createSkeleton() {
        return new ImageSkeletonDescriptor(this.initPosition, await getSkeletonSize(this.files, {
            enableNewImageSizeCalculation: this.enableNewImageSizeCalculation,
        }, this.logger));
    }
    async createPMNode(res) {
        return this.createImage(res);
    }
}
async function getSkeletonSize(files, opts, logger) {
    const skeletonSize = { width: '300', height: '200' };
    if (files.length === 1) {
        try {
            const size = await loadImage(files[0]).then(opts.enableNewImageSizeCalculation ? calcSkeletonSizeNew : calcSkeletonSize);
            skeletonSize.width = String(size.width);
            skeletonSize.height = String(size.height);
        }
        catch (err) {
            globalLogger.error(err);
            logger.error({ error: err });
        }
    }
    return skeletonSize;
}
function calcSkeletonSize({ width, height }) {
    if (height <= IMG_MAX_HEIGHT)
        return { width, height };
    const ratio = IMG_MAX_HEIGHT / height; // ratio<1
    return { height: IMG_MAX_HEIGHT, width: width * ratio };
}
function calcSkeletonSizeNew({ width, height }) {
    return getProportionalSize({
        width,
        height,
        imgMaxHeight: IMG_MAX_HEIGHT,
    });
}
//# sourceMappingURL=upload.js.map
