"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.ResourceAdapter = void 0;
const fs = require("fs");
const path = require("path");
const util = require("util");
const StorageAdapterBase_1 = require("./StorageAdapterBase");
let readFile;
if (fs.readFile) {
    readFile = util.promisify(fs.readFile);
}
class ResourceAdapter extends StorageAdapterBase_1.StorageAdapterBase {
    constructor() {
        super();
        this.ROOT = 'Microsoft.CommonDataModel.ObjectModel.Resources';
        this.resourcesPath = path.join(__dirname, '..', 'Resources');
    }
    canRead() {
        return true;
    }
    readAsync(corpusPath) {
        return __awaiter(this, void 0, void 0, function* () {
            const adapterPath = this.resourcesPath + corpusPath;
            const content = yield readFile(adapterPath, 'utf-8');
            if (content === undefined || content === '') {
                throw new Error(`The requested document '${adapterPath}' is empty`);
            }
            return content;
        });
    }
    createAdapterPath(corpusPath) {
        if (!corpusPath) {
            return undefined;
        }
        return this.ROOT + corpusPath;
    }
    createCorpusPath(adapterPath) {
        if (!adapterPath || !adapterPath.startsWith(this.ROOT)) {
            return undefined;
        }
        return adapterPath.substring(this.ROOT.length);
    }
}
exports.ResourceAdapter = ResourceAdapter;
//# sourceMappingURL=ResourceAdapter.js.map