"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.webFile = webFile;
const APINotImplementedError_1 = require("../../../../exception/APINotImplementedError");
function webFile(window, opt) {
    const { document } = window;
    const isSaveFilePickerSupported = () => {
        // @ts-ignore
        return !!window.showSaveFilePicker;
    };
    const isOpenFilePickerSupported = () => {
        // @ts-ignore
        return !!window.showOpenFilePicker;
    };
    function showSaveFilePicker(opt) {
        if (isSaveFilePickerSupported()) {
            // @ts-ignore
            return window.showSaveFilePicker(opt);
        }
        else {
            throw new APINotImplementedError_1.APINotSupportedError('File System');
        }
    }
    function showOpenFilePicker(opt) {
        if (isOpenFilePickerSupported()) {
            // @ts-ignore
            return window.showOpenFilePicker(opt);
        }
        else {
            throw new APINotImplementedError_1.APINotSupportedError('File System');
        }
    }
    function fallbackShowOpenFilePicker(opt) {
        return new Promise((resolve, reject) => {
            var _a;
            const input = window.document.createElement('input');
            input.type = 'file';
            input.hidden = true;
            input.multiple = opt.multiple || false;
            input.accept = (_a = opt.accept) !== null && _a !== void 0 ? _a : '*/*';
            window.document.body.appendChild(input);
            input.onchange = async () => {
                const files = input.files;
                if (!files) {
                    reject(new Error('No files selected'));
                    return;
                }
                const final_files = [];
                for (let i = 0; i < files.length; i++) {
                    const file = files.item(i);
                    final_files.push(file);
                }
                resolve(final_files);
            };
            input.click();
            window.document.body.removeChild(input);
        });
    }
    const downloadText = async ({ name, mimetype: mimeType, charset, text, }) => {
        const url = `data:${mimeType};charset=${charset},${encodeURIComponent(text)}`;
        return downloadURL({ name, url });
    };
    const testGet = async (url) => {
        try {
            await fetch(url);
            return true;
        }
        catch (err) {
            // console.log(err)
            return false;
        }
    };
    const downloadURL = async ({ name, url }) => {
        if (url.startsWith('http://') || url.startsWith('https://')) {
            const accessible = await testGet(url);
            if (!accessible) {
                throw new Error('URL is not accessible due to CORS policy');
            }
        }
        const a = document.createElement('a');
        document.body.appendChild(a);
        a.download = name;
        a.href = url;
        a.click();
        document.body.removeChild(a);
    };
    const file = {
        // @ts-ignore
        FileReader: window.FileReader,
        showSaveFilePicker,
        isSaveFilePickerSupported,
        isOpenFilePickerSupported,
        showOpenFilePicker,
        fallbackShowOpenFilePicker,
        downloadURL,
        downloadText,
    };
    return file;
}
//# sourceMappingURL=file.js.map