/*
This file is part of the Notesnook project (https://notesnook.com/)

Copyright (C) 2023 Streetwriters (Private) Limited

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
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());
    });
};
import DataURL from "@notesnook/core/dist/utils/dataurl";
/**
 * macOS uses its own Uniform Type Identifier format instead of mime-types.
 * This is an incomplete map of UTI->mime-type for image types taken from
 * https://gist.github.com/RhetTbull/7221ef3cfd9d746f34b2550d4419a8c2
 */
const UTITypes = {
    "com.microsoft.bmp": "image/bmp",
    "com.canon.crw-raw-image": "image/x-canon-crw",
    "com.adobe.raw-image": "image/x-adobe-dng",
    "com.dxo.raw-image": "image/x-dxo-dxo",
    "com.j2.efx-fax": "image/efax",
    "com.epson.raw-image": "image/x-epson-erf",
    "com.kodak.flashpix-image": "image/fpx",
    "com.compuserve.gif": "image/gif",
    "com.microsoft.ico": "image/vnd.microsoft.icon",
    "public.jpeg": "image/jpeg",
    "public.jpeg-2000": "image/jp2",
    "com.nikon.nrw-raw-image": "image/x-nikon-nrw",
    "com.apple.pict": "image/pict",
    "public.png": "image/png",
    "com.adobe.photoshop-image": "image/vnd.adobe.photoshop",
    "com.leica.pwl-raw-image": "image/x-leica-pwl",
    "com.apple.quicktime-image": "image/x-quicktime",
    "com.sgi.sgi-image": "image/sgi",
    "com.sony.sr2-raw-image": "image/x-sony-sr2",
    "public.svg-image": "image/svg+xml",
    "com.truevision.tga-image": "image/targa",
    "public.tiff": "image/tiff",
    "public.xbitmap-image": "image/x-xbitmap",
    "public.avci": "image/avci",
    "public.avcs": "image/avcs",
    "public.heic": "image/heic",
    "public.heif": "image/heif",
    "public.heics": "image/heic-sequence",
    "public.heifs": "image/heif-sequence"
};
export function corsify(url, host) {
    if (host && url && !url.startsWith("blob:") && !DataURL.isValid(url))
        return `${host}/${url}`;
    return url;
}
export function downloadImage(url, options) {
    return __awaiter(this, void 0, void 0, function* () {
        const corsifiedURL = corsify(url, options === null || options === void 0 ? void 0 : options.corsHost);
        if (!corsifiedURL)
            return;
        const response = yield fetch(corsifiedURL, {
            mode: "cors",
            credentials: "omit",
            cache: "force-cache",
            signal: options === null || options === void 0 ? void 0 : options.signal
        });
        if (!response.ok)
            throw new Error(`invalid status code ${response.status}`);
        let contentType = response.headers.get("Content-Type");
        if (contentType && UTITypes[contentType])
            contentType = UTITypes[contentType];
        if (!contentType || !contentType.startsWith("image/"))
            return;
        let blob = yield response.blob();
        if (UTITypes[blob.type])
            blob = new Blob([blob], {
                type: contentType
            });
        return {
            blob,
            url: URL.createObjectURL(blob),
            mimeType: contentType,
            size: blob.size
        };
    });
}
export function toDataURL(blob) {
    return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.onload = (_e) => resolve(reader.result);
        reader.onerror = (_e) => reject(reader.error);
        reader.onabort = (_e) => reject(new Error("Read aborted"));
        reader.readAsDataURL(blob);
    });
}
const OBJECT_URL_CACHE = {};
export function toBlobURL(dataurl, type = "other", mimeType, id) {
    if (id && OBJECT_URL_CACHE[id])
        return OBJECT_URL_CACHE[id];
    if (!DataURL.isValid(dataurl))
        return;
    const dataurlObject = DataURL.toObject(dataurl);
    let mime = dataurlObject.mimeType || "";
    const data = dataurlObject.data;
    if (!data)
        return;
    // sometimes the provided mime type in the dataurl can be wrong so we
    // fallback and make sure the browser loads the image properly.
    if (type === "image" && !mime.startsWith("image/")) {
        mime = mimeType && mimeType.startsWith("image/") ? mimeType : "image/*";
    }
    const objectURL = URL.createObjectURL(new Blob([Buffer.from(data, "base64")], { type: mimeType }));
    if (id)
        OBJECT_URL_CACHE[id] = objectURL;
    return objectURL;
}
export function revokeBloburl(id) {
    const url = OBJECT_URL_CACHE[id];
    if (!url)
        return;
    URL.revokeObjectURL(url);
    OBJECT_URL_CACHE[id] = undefined;
}
