"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
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.LocalBrowserAdapter = void 0;
const internal_1 = require("../internal");
/**
 * This adapter is necessary for utilizing local files with the Entity Explorer on the browser.
 * Since we cannot pull any local files from the browser, all files are loaded from a dialog
 * and then stored in this adapter. Any files that are needed must be loaded from that dialog
 * and cannot be pulled in in any other way.
 */
class LocalBrowserAdapter extends internal_1.StorageAdapterBase {
    constructor(config) {
        super();
        this.folders = new Set();
        if (config.fileMap) {
            this.fileMap = config.fileMap;
            for (let path of this.fileMap.keys()) {
                // remove file name
                path = path.slice(0, path.lastIndexOf('/'));
                const pathSplit = path.split('/')
                    .filter((x) => { return !!x; });
                let currentPath = '';
                for (const folder of pathSplit) {
                    currentPath = `${currentPath}/${folder}`;
                    this.folders.add(currentPath);
                }
            }
            this.folders.add('/');
        }
    }
    canRead() {
        return true;
    }
    readAsync(corpusPath) {
        return __awaiter(this, void 0, void 0, function* () {
            if (this.fileMap.has(corpusPath)) {
                const file = this.fileMap.get(corpusPath).file;
                const f = (fileObj) => __awaiter(this, void 0, void 0, function* () {
                    return new Promise((resolve, reject) => {
                        const reader = new FileReader();
                        reader.onloadend = (event) => {
                            resolve(reader.result.toString());
                        };
                        reader.onerror = (event) => {
                            reject('Could not read data from the file');
                        };
                        reader.readAsText(fileObj);
                    });
                });
                const data = yield f(file);
                if (data) {
                    return data;
                }
            }
            return undefined;
        });
    }
    dirExists(folderPath) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.folders.has(folderPath);
        });
    }
    fetchAllFilesAsync(folderCorpusPath) {
        return __awaiter(this, void 0, void 0, function* () {
            return Array.from(this.fileMap.keys());
        });
    }
}
exports.LocalBrowserAdapter = LocalBrowserAdapter;
//# sourceMappingURL=LocalBrowserAdapter.js.map