"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.StorageAdapterCacheContext = exports.StorageAdapterBase = void 0;
/**
  * The CDM base class for an adapter object that can read and write documents from a data source.
  * This allows a user flexibility to interact with data from multiple sources without having
  * to manually copy data to the location where the Object Model is running. By deriving from this
  * this class, users can to create their own adapter if needed.
 */
class StorageAdapterBase {
    constructor() {
        this.activeCacheContexts = new Set();
    }
    /**
     * Returns true if the adapter can read data, false otherwise.
     */
    canRead() {
        return false;
    }
    /**
     * Returns true if the adapter can write data, false otherwise.
     */
    canWrite() {
        return false;
    }
    /**
     * Returns JSON data that exists at the path as a JSON string
     */
    readAsync(corpusPath) {
        return __awaiter(this, void 0, void 0, function* () {
            throw new Error('Method not implemented.');
        });
    }
    /**
     * writes the object data to the specified document path
     */
    writeAsync(corpusPath, data) {
        return __awaiter(this, void 0, void 0, function* () {
            throw new Error('Method not implemented.');
        });
    }
    /**
     * converts a corpus path into a path in the domain of this adapter
     */
    createAdapterPath(corpusPath) {
        return corpusPath;
    }
    /**
     * converts a path in the domain of this adapter into a corpus path
     */
    createCorpusPath(adapterPath) {
        return adapterPath;
    }
    /**
     * Returns the last modified time of the file accessible by the adapter
     */
    computeLastModifiedTimeAsync(corpusPath) {
        return __awaiter(this, void 0, void 0, function* () {
            return new Date();
        });
    }
    /**
     * Returns a list corpus paths to all files and folders at or under the
     * provided corpus path to a folder
     */
    fetchAllFilesAsync(folderCorpusPath) {
        return __awaiter(this, void 0, void 0, function* () {
            return undefined;
        });
    }
    /**
     * Applies the JSON config, has to be called after default constructor.
     */
    updateConfig(config) {
    }
    /**
     * Constructs the config.
     * Reeturns the object, representing the constructed config for that adapter.
     */
    fetchConfig() {
        return "{}";
    }
    /**
     * Empties the cache of files and folders if the storage adapter uses a cache
     */
    clearCache() {
    }
    /**
     * If true, inherited classes should cache and reuse file query results if they support caching
     */
    isCacheEnabled() {
        return this.activeCacheContexts.size > 0;
    }
    /**
     * Calling this function tells the adapter it may cache and reuse file query results, as opposed to
     * reissuing queries to the underlying file storage, which may be a costly operation. This adapter is
     * allowed to cache and reuse queries until the object returned by this function has its dispose
     * function called. If createFileQueryCacheContext is called multiple times, caching is allowed until
     * all objects returned have thier dispose function called. Intended usage is for callers to wrap a
     * set of operations that should use caching with a try-finally block and call dispose inside finally.
     */
    createFileQueryCacheContext() {
        let context = new StorageAdapterCacheContext(this.createDisposeAdapterContextFunc(this));
        this.activeCacheContexts.add(context);
        return context;
    }
    /**
     * This function exists so that the StorageAdapterCacheContext's dispose function can remove itself
     * from the StorageAdapter's activeCacheContexts without needing to make activeCacheContexts public
     * @param adapter the adapter this function is created for
     */
    createDisposeAdapterContextFunc(adapter) {
        let func = (context) => {
            this.activeCacheContexts.delete(context);
            if (!this.isCacheEnabled()) {
                this.clearCache();
            }
        };
        return func;
    }
}
exports.StorageAdapterBase = StorageAdapterBase;
/**
 * This class is used to track requests to enable file query caching. Each time a request to enable
 * caching is made an instance of this class is created and added the the StorageAdapter's activeCacheContexts
 * set. When dispose is called, this object is removed from that set. Whenever any items are in the adapter's
 * activeCacheContexts, caching is enabled.
 */
class StorageAdapterCacheContext {
    constructor(dispose) {
        this.disposeFunc = dispose;
    }
    dispose() {
        this.disposeFunc(this);
    }
}
exports.StorageAdapterCacheContext = StorageAdapterCacheContext;
//# sourceMappingURL=StorageAdapterBase.js.map