"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.CdmEntityCollection = void 0;
const util_1 = require("util");
const internal_1 = require("../internal");
const cdmObjectTypeGuards_1 = require("../Utilities/cdmObjectTypeGuards");
class CdmEntityCollection extends internal_1.CdmCollection {
    constructor(ctx, owner) {
        super(ctx, owner, internal_1.cdmObjectType.localEntityDeclarationDef);
        this.TAG = CdmEntityCollection.name;
    }
    push(obj, entityPath = false, simpleRef = false) {
        if (util_1.isBoolean(entityPath)) {
            simpleRef = entityPath;
        }
        if (util_1.isString(obj)) {
            const createdObj = super.push(obj, simpleRef);
            if (util_1.isString(entityPath)) {
                createdObj.entityPath = entityPath;
            }
            return createdObj;
        }
        else {
            let entityDeclaration;
            if (cdmObjectTypeGuards_1.isEntityDefinition(obj)) {
                const entity = obj;
                if (!entity.owner) {
                    internal_1.Logger.error(entity.ctx, this.TAG, this.push.name, entity.atCorpusPath, internal_1.cdmLogCode.ErrEntityCreationFailed);
                    return undefined;
                }
                entityDeclaration =
                    this.ctx.corpus.MakeObject(this.defaultType, entity.entityName, simpleRef);
                entityDeclaration.entityPath = this.ctx.corpus.storage.createRelativeCorpusPath(`${entity.owner.atCorpusPath}/${entity.entityName}`, this.owner.inDocument);
                entityDeclaration.explanation = entity.explanation;
            }
            else {
                entityDeclaration = obj;
            }
            if (util_1.isString(entityPath)) {
                entityDeclaration.entityPath = entityPath;
            }
            return super.push(entityDeclaration);
        }
    }
    remove(entity) {
        if (entity instanceof internal_1.CdmEntityDefinition) {
            for (let index = 0; index < this.length; index = index + 1) {
                if (this.allItems[index].entityName === entity.entityName) {
                    super.removeAt(index);
                    return true;
                }
            }
            return false;
        }
        return super.remove(entity);
    }
    concat(list) {
        for (const elem of list) {
            this.push(elem);
        }
    }
}
exports.CdmEntityCollection = CdmEntityCollection;
//# sourceMappingURL=CdmEntityCollection.js.map