"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
Object.defineProperty(exports, "__esModule", { value: true });
exports.CdmDocumentCollection = void 0;
const util_1 = require("util");
const internal_1 = require("../internal");
class CdmDocumentCollection extends internal_1.CdmCollection {
    constructor(ctx, owner) {
        super(ctx, owner, internal_1.cdmObjectType.documentDef);
        this.TAG = CdmDocumentCollection.name;
    }
    get owner() {
        return super.owner;
    }
    set owner(value) {
        super.owner = value;
    }
    /**
     * @inheritdoc
     */
    push(documentOrName, documentNameOrSimpleRef) {
        let document;
        if (documentOrName !== undefined && (0, util_1.isString)(documentOrName)) {
            const simpleRef = (0, util_1.isBoolean)(documentNameOrSimpleRef) ? documentNameOrSimpleRef : false;
            document = this.ctx.corpus.MakeObject(this.defaultType, documentOrName, simpleRef);
        }
        else if (documentOrName instanceof internal_1.CdmDocumentDefinition) {
            document = documentOrName;
        }
        if (document) {
            if (documentNameOrSimpleRef !== undefined && (0, util_1.isString)(documentNameOrSimpleRef)) {
                document.name = documentNameOrSimpleRef;
            }
            if (!this.checkAndAddItemModifications(document)) {
                return undefined;
            }
            // why is this collection unlike all other collections?
            // because documents are in folders. folders are not in documents.
            document.owner = this.owner;
            this.allItems.push(document);
            return document;
        }
    }
    insert(index, docDef) {
        if (!this.checkAndAddItemModifications(docDef)) {
            return;
        }
        // why is this collection unlike all other collections?
        // because documents are in folders. folders are not in documents.
        docDef.owner = this.owner;
        const l = this.length;
        for (let i = l; i > index; i = i - 1) {
            this.allItems[i] = this.allItems[i - 1];
        }
        this.allItems[index] = docDef;
    }
    remove(object) {
        if ((0, util_1.isString)(object)) {
            if (this.owner.documentLookup.has(object)) {
                this.removeItemModifications(object);
                const index = this.allItems.findIndex((elem) => elem.name === object);
                // setting this currentlyResolving flag will keep the base collection code from setting the inDocument to null
                // this makes sense because a document is "in" itself. always.
                const bSave = this.ctx.corpus.isCurrentlyResolving;
                this.ctx.corpus.isCurrentlyResolving = true;
                super.removeAt(index);
                this.ctx.corpus.isCurrentlyResolving = bSave;
                return true;
            }
            return false;
        }
        else {
            return this.remove(object.name);
        }
    }
    removeAt(index) {
        if (index >= 0 && index < this.length) {
            this.remove(this.allItems[index].name);
        }
    }
    clear() {
        this.allItems.forEach((document) => {
            this.removeItemModifications(document.name);
        });
        super.clear();
    }
    checkAndAddItemModifications(document) {
        if (this.item(document.name) !== undefined) {
            internal_1.Logger.error(this.ctx, this.TAG, "checkAndAddItemModifications", document.atCorpusPath, internal_1.cdmLogCode.ErrDocAlreadyExist, document.name, this.owner.atCorpusPath != undefined ? this.owner.atCorpusPath : this.owner.name);
            return false;
        }
        if (document.owner !== undefined && document.owner !== this.owner) {
            // this is fun! the document is moving from one folder to another
            // it must be removed from the old folder for sure, but also now
            // there will be a problem with any corpus paths that are relative to that old folder location.
            // so, whip through the document and change any corpus paths to be relative to this folder
            document.localizeCorpusPaths(this.owner); // returns false if it fails, but ... who cares? we tried
            document.owner.documents.remove(document.name);
        }
        document.folderPath = this.owner.folderPath;
        document.owner = this.owner;
        document.namespace = this.owner.namespace;
        this.makeDocumentDirty(); // set the document to dirty so it will get saved in the new folder location if saved
        this.owner.corpus.addDocumentObjects(this.owner, document);
        return true;
    }
    removeItemModifications(documentName) {
        this.owner.corpus.removeDocumentObjects(this.owner, this.owner.documentLookup.get(documentName));
        this.owner.documentLookup.delete(documentName);
    }
}
exports.CdmDocumentCollection = CdmDocumentCollection;
//# sourceMappingURL=CdmDocumentCollection.js.map