"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.CdmFolderDefinition = void 0;
const internal_1 = require("../internal");
/**
 * The object model implementation for Folder object.
 */
class CdmFolderDefinition extends internal_1.CdmObjectDefinitionBase {
    /**
     * Initializes a new instance of theFolderImpl class.
     * @param ctx The context.
     * @param name The name.
     */
    constructor(ctx, name) {
        super(ctx);
        this.TAG = CdmFolderDefinition.name;
        this.name = name;
        this.folderPath = `${name}/`;
        this.childFolders = new internal_1.CdmFolderCollection(ctx, this);
        this.documents = new internal_1.CdmDocumentCollection(ctx, this);
        this.documentLookup = new Map();
        this.objectType = internal_1.cdmObjectType.folderDef;
    }
    /**
     * @inheritdoc
     */
    get folderPath() {
        return this._folderPath;
    }
    /**
     * @inheritdoc
     * @internal
     */
    set folderPath(value) {
        this._folderPath = value;
    }
    /**
     * @inheritdoc
     */
    get namespace() {
        return this._namespace;
    }
    /**
     * @inheritdoc
     * @internal
     */
    set namespace(value) {
        this._namespace = value;
    }
    static get objectType() {
        return internal_1.cdmObjectType.folderDef;
    }
    /**
     * @inheritdoc
     */
    getName() {
        return this.name;
    }
    /**
     * @inheritdoc
     */
    validate() {
        if (!this.name) {
            let missingFields = ['name'];
            internal_1.Logger.error(this.ctx, this.TAG, this.validate.name, this.atCorpusPath, internal_1.cdmLogCode.ErrValdnIntegrityCheckFailure, missingFields.map((s) => `'${s}'`).join(', '), this.atCorpusPath);
            return false;
        }
        return true;
    }
    /**
     * @inheritdoc
     */
    isDerivedFrom(base, resOpt) {
        return false;
    }
    get atCorpusPath() {
        if (!this.namespace) {
            // We're not under any adapter (not in a corpus), so return special indicator.
            return `NULL:/${this.folderPath}`;
        }
        else {
            return `${this.namespace}:${this.folderPath}`;
        }
    }
    /**
     * @internal
     * @inheritdoc
     */
    fetchChildFolderFromPath(path, makeFolder = true) {
        let name;
        let remainingPath = path;
        let childFolder = this;
        while (childFolder && remainingPath.indexOf('/') !== -1) {
            let first = remainingPath.indexOf('/');
            name = remainingPath.slice(0, first);
            remainingPath = remainingPath.slice(first + 1);
            if (name.toLowerCase() !== childFolder.name.toLowerCase()) {
                internal_1.Logger.error(this.ctx, this.TAG, this.fetchChildFolderFromPath.name, undefined, internal_1.cdmLogCode.ErrInvalidPath);
                return undefined;
            }
            // the end?
            if (remainingPath === '') {
                return childFolder;
            }
            first = remainingPath.indexOf('/');
            let childFolderName = remainingPath;
            if (first !== -1) {
                childFolderName = remainingPath.slice(0, first);
            }
            else {
                // the last part of the path will be considered part of the part depending on the make_folder flag.
                break;
            }
            // get next child folder.
            childFolder = childFolder.childFolders.getOrCreate(childFolderName);
        }
        if (makeFolder) {
            childFolder = childFolder.childFolders.push(remainingPath);
        }
        return childFolder;
    }
    /**
     * @internal
     * @inheritdoc
     */
    fetchDocumentFromFolderPathAsync(objectPath, forceReload = false, resOpt = undefined) {
        return __awaiter(this, void 0, void 0, function* () {
            let docName;
            let remainingPath;
            const first = objectPath.indexOf('/', 0);
            if (first < 0) {
                remainingPath = '';
                docName = objectPath;
            }
            else {
                remainingPath = objectPath.slice(first + 1);
                docName = objectPath.substring(0, first);
            }
            // got that doc?
            let doc;
            if (this.documentLookup.has(docName)) {
                doc = this.documentLookup.get(docName);
                if (!forceReload) {
                    return doc;
                }
                // remove them from the caches since they will be back in a moment
                if (doc.isDirty) {
                    internal_1.Logger.warning(this.ctx, this.TAG, this.fetchDocumentFromFolderPathAsync.name, undefined, internal_1.cdmLogCode.WarnDocChangesDiscarded, doc.name);
                }
                this.documents.remove(docName);
            }
            // go get the doc
            doc = yield this.corpus.persistence.LoadDocumentFromPathAsync(this, docName, doc, resOpt);
            return doc;
        });
    }
    /**
     * @inheritdoc
     */
    getObjectType() {
        return internal_1.cdmObjectType.folderDef;
    }
    /**
     * @inheritdoc
     */
    visit(pathFrom, preChildren, postChildren) {
        return false;
    }
    /**
     * @inheritdoc
     */
    fetchObjectDefinition(resOpt) {
        return undefined;
    }
    /**
     * @inheritdoc
     * @internal
     */
    fetchResolvedTraits(resOpt) {
        return undefined;
    }
    /**
     * @inheritdoc
     * @internal
     */
    fetchResolvedAttributes() {
        return undefined;
    }
    /**
     * @inheritdoc
     */
    copy(resOpt, host) {
        return undefined;
    }
}
exports.CdmFolderDefinition = CdmFolderDefinition;
//# sourceMappingURL=CdmFolderDefinition.js.map