"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 });
const cdmObjectType_1 = require("../../../Enums/cdmObjectType");
const internal_1 = require("../../../internal");
const Storage_1 = require("../../../Storage");
const CdmCollectionHelperFunctions_1 = require("./CdmCollectionHelperFunctions");
describe('Cdm/CdmCollection/CdmCollection', () => {
    it('TestCdmCollectionAddMethod', () => {
        const cdmCorpus = new internal_1.CdmCorpusDefinition();
        cdmCorpus.storage.defaultNamespace = 'local';
        const ctx = cdmCorpus.ctx;
        const cdmDocument = new internal_1.CdmDocumentDefinition(ctx, 'NameOfDocument');
        const collection = new internal_1.CdmCollection(ctx, cdmDocument, cdmObjectType_1.cdmObjectType.attributeContextDef);
        const addedAttribute = collection.push('nameOfNewAttribute');
        expect(collection.allItems.length)
            .toEqual(1);
        expect(collection.allItems[0].name)
            .toEqual('nameOfNewAttribute');
        expect(collection.allItems[0].owner)
            .toEqual(cdmDocument);
        expect(collection.allItems[0].ctx)
            .toEqual(ctx);
        expect(collection.allItems[0])
            .toEqual(addedAttribute);
    });
    it('TestCdmCollectionRemoveMethod', () => {
        const cdmCorpus = new internal_1.CdmCorpusDefinition();
        cdmCorpus.storage.defaultNamespace = 'local';
        cdmCorpus.storage.mount('local', new Storage_1.LocalAdapter('CdmCorpus/LocalPath'));
        const ctx = cdmCorpus.ctx;
        const cdmDocument = new internal_1.CdmDocumentDefinition(ctx, 'NameOfDocument');
        const collection = new internal_1.CdmCollection(ctx, cdmDocument, cdmObjectType_1.cdmObjectType.attributeContextDef);
        const addedDocument = collection.push('nameOfNewDocument');
        const addedDocument2 = collection.push('otherDocument');
        expect(collection.length)
            .toEqual(2);
        let removed = collection.remove(addedDocument);
        expect(removed)
            .toBeTruthy();
        // try to remove a second time
        removed = collection.remove(addedDocument);
        expect(removed)
            .toEqual(false);
        expect(collection.length)
            .toEqual(1);
    });
    it('TestCdmCollectionRemoveAt', () => {
        const cdmCorpus = new internal_1.CdmCorpusDefinition();
        cdmCorpus.storage.defaultNamespace = 'local';
        cdmCorpus.storage.mount('local', new Storage_1.LocalAdapter('CdmCorpus/LocalPath'));
        const ctx = cdmCorpus.ctx;
        const cdmDocument = new internal_1.CdmDocumentDefinition(ctx, 'NameOfDocument');
        const collection = new internal_1.CdmCollection(ctx, cdmDocument, cdmObjectType_1.cdmObjectType.attributeContextDef);
        const addedDocument = collection.push('nameOfNewDocument');
        const addedDocument2 = collection.push('otherDocument');
        expect(collection.length)
            .toEqual(2);
        collection.removeAt(0);
        expect(collection.length)
            .toEqual(1);
        expect(collection.allItems[0])
            .toEqual(addedDocument2);
        collection.removeAt(1);
        expect(collection.length)
            .toEqual(1);
        expect(collection.allItems[0])
            .toEqual(addedDocument2);
        collection.removeAt(0);
        expect(collection.length)
            .toEqual(0);
    });
    it('TestCdmCollectionAddingList', () => __awaiter(void 0, void 0, void 0, function* () {
        const cdmCorpus = new internal_1.CdmCorpusDefinition();
        cdmCorpus.storage.defaultNamespace = 'local';
        cdmCorpus.storage.mount('local', new Storage_1.LocalAdapter('CdmCorpus/LocalPath'));
        const ctx = cdmCorpus.ctx;
        const cdmDocument = new internal_1.CdmDocumentDefinition(ctx, 'NameOfDocument');
        const collection = new internal_1.CdmCollection(ctx, cdmDocument, cdmObjectType_1.cdmObjectType.attributeContextDef);
        const entityList = [];
        for (let i = 0; i < 2; i++) {
            const entity = new internal_1.CdmEntityDefinition(cdmCorpus.ctx, `entityName_${i}`, undefined);
            CdmCollectionHelperFunctions_1.createDocumentForEntity(cdmCorpus, entity);
            const entityDeclaration = cdmCorpus.MakeObject(cdmObjectType_1.cdmObjectType.localEntityDeclarationDef, entity.entityName, false);
            entityDeclaration.owner = entity.owner;
            entityDeclaration.entityPath = `${entity.owner.atCorpusPath}/${entity.entityName}`;
            entityList.push(entityDeclaration);
        }
        expect(collection.length)
            .toEqual(0);
        collection.concat(entityList);
        expect(entityList.length)
            .toEqual(2);
        expect(collection.allItems.length)
            .toEqual(2);
        for (let i = 0; i < 2; i++) {
            expect(collection.allItems[i].entityName)
                .toEqual(`entityName_${i}`);
        }
    }));
    it('TestCdmCollectionChangeMakesDocumentDirty', () => {
        const manifest = CdmCollectionHelperFunctions_1.generateManifest('C:\\Nothing');
        const collection = new internal_1.CdmCollection(manifest.ctx, manifest, cdmObjectType_1.cdmObjectType.entityRef);
        manifest.isDirty = false;
        collection.push(new internal_1.CdmEntityReference(manifest.ctx, 'name', false));
        expect(manifest.isDirty)
            .toBeTruthy();
        manifest.isDirty = false;
        collection.push('theName');
        expect(manifest.isDirty)
            .toBeTruthy();
        const entity = new internal_1.CdmEntityReference(manifest.ctx, 'otherEntity', false);
        const entityList = [entity];
        manifest.isDirty = false;
        collection.concat(entityList);
        expect(manifest.isDirty)
            .toBeTruthy();
        manifest.isDirty = false;
        const entity2 = new internal_1.CdmEntityReference(manifest.ctx, 'otherEntity2', false);
        collection.insert(0, entity2);
        expect(manifest.isDirty)
            .toBeTruthy();
        manifest.isDirty = false;
        collection.remove(entity);
        expect(manifest.isDirty)
            .toBeTruthy();
        manifest.isDirty = false;
        collection.removeAt(0);
        expect(manifest.isDirty)
            .toBeTruthy();
        manifest.isDirty = false;
        collection.clear();
        expect(manifest.isDirty)
            .toBeTruthy();
    });
});
//# sourceMappingURL=CdmCollection.test.js.map