"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = imageInputFileToBase64;
/**
 * # Image input File top Base64
 */
function imageInputFileToBase64(_a) {
    return __awaiter(this, arguments, void 0, function* ({ imageInputFile, maxWidth, imagePreviewNode, }) {
        /**
         * Make https request
         *
         * @description make a request to datasquirel.com
         */
        try {
            let imageName = imageInputFile.name.replace(/\..*/, "");
            let imageDataBase64;
            let imageSize;
            let canvas = document.createElement("canvas");
            const MIME_TYPE = imageInputFile.type;
            const QUALITY = 0.95;
            const MAX_WIDTH = maxWidth ? maxWidth : null;
            const file = imageInputFile;
            const blobURL = URL.createObjectURL(file);
            const img = new Image();
            /** ********************* Add source to new image */
            img.src = blobURL;
            imageDataBase64 = yield new Promise((res, rej) => {
                /** ********************* Handle Errors in loading image */
                img.onerror = function () {
                    URL.revokeObjectURL(this.src);
                    console.log("Cannot load image");
                };
                /** ********************* Handle new image when loaded */
                img.onload = function (e) {
                    const imgEl = e.target;
                    URL.revokeObjectURL(imgEl.src);
                    if (MAX_WIDTH) {
                        const scaleSize = MAX_WIDTH / img.naturalWidth;
                        canvas.width =
                            img.naturalWidth < MAX_WIDTH
                                ? img.naturalWidth
                                : MAX_WIDTH;
                        canvas.height =
                            img.naturalWidth < MAX_WIDTH
                                ? img.naturalHeight
                                : img.naturalHeight * scaleSize;
                    }
                    else {
                        canvas.width = img.naturalWidth;
                        canvas.height = img.naturalHeight;
                    }
                    const ctx = canvas.getContext("2d");
                    ctx === null || ctx === void 0 ? void 0 : ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
                    const srcEncoded = canvas.toDataURL(MIME_TYPE, QUALITY);
                    if (imagePreviewNode) {
                        imagePreviewNode.src = srcEncoded;
                    }
                    res(srcEncoded);
                };
            });
            imageSize = yield new Promise((res, rej) => {
                canvas.toBlob((blob) => {
                    res(blob === null || blob === void 0 ? void 0 : blob.size);
                }, MIME_TYPE, QUALITY);
            });
            return {
                imageBase64: imageDataBase64 === null || imageDataBase64 === void 0 ? void 0 : imageDataBase64.replace(/.*?base64,/, ""),
                imageBase64Full: imageDataBase64,
                imageName: imageName,
                imageSize: imageSize,
            };
        }
        catch (error) {
            console.log("Image Processing Error! =>", error.message);
            return {
                imageBase64: undefined,
                imageBase64Full: undefined,
                imageName: undefined,
                imageSize: undefined,
            };
        }
    });
}