"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ImagesUploadProcess = void 0;
const logger_1 = require("../../../../logger.js");
const utils_1 = require("../../../../utils/index.js");
const upload_1 = require("../../../behavior/utils/upload.js");
const markdown_1 = require("../../../markdown/index.js");
const const_1 = require("../const.js");
const utils_2 = require("../utils.js");
const skeleton_1 = require("./skeleton.js");
class ImagesUploadProcess extends upload_1.FilesBatchUploadProcess {
    createImage;
    initPosition;
    enableNewImageSizeCalculation;
    constructor(view, files, uploadHandler, position, opts) {
        super(view, files, uploadHandler);
        this.initPosition = position;
        this.createImage = (0, utils_2.createImageNode)((0, markdown_1.imageType)(this.view.state.schema), opts);
        this.enableNewImageSizeCalculation = opts.enableNewImageSizeCalculation;
    }
    async createSkeleton() {
        return new skeleton_1.ImageSkeletonDescriptor(this.initPosition, await getSkeletonSize(this.files, {
            enableNewImageSizeCalculation: this.enableNewImageSizeCalculation,
        }));
    }
    async createPMNode(res) {
        return this.createImage(res);
    }
}
exports.ImagesUploadProcess = ImagesUploadProcess;
async function getSkeletonSize(files, opts) {
    const skeletonSize = { width: '300', height: '200' };
    if (files.length === 1) {
        try {
            const size = await (0, utils_2.loadImage)(files[0]).then(opts?.enableNewImageSizeCalculation ? calcSkeletonSizeNew : calcSkeletonSize);
            skeletonSize.width = String(size.width);
            skeletonSize.height = String(size.height);
        }
        catch (err) {
            logger_1.logger.error(err);
        }
    }
    return skeletonSize;
}
function calcSkeletonSize({ width, height }) {
    if (height <= const_1.IMG_MAX_HEIGHT)
        return { width, height };
    const ratio = const_1.IMG_MAX_HEIGHT / height; // ratio<1
    return { height: const_1.IMG_MAX_HEIGHT, width: width * ratio };
}
function calcSkeletonSizeNew({ width, height }) {
    return (0, utils_1.getProportionalSize)({
        width,
        height,
        imgMaxHeight: const_1.IMG_MAX_HEIGHT,
    });
}
//# sourceMappingURL=upload.js.map
