"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 internal_1 = require("../../../internal");
const testHelper_1 = require("../../testHelper");
describe('Cdm/Resolution/TraitProfile', () => {
    const testsSubPath = 'Cdm/Resolution/TraitProfileTest';
    const schemaDocsPath = testHelper_1.testHelper.schemaDocumentsPath;
    const fixProfiles = (key, value) => {
        if (key === 'trId') {
            return undefined;
        }
        else {
            if (value instanceof Map) {
                let obj = {};
                value.forEach((v, k) => {
                    obj[k] = v;
                });
                return obj;
            }
            return value;
        }
    };
    const dumpAndValidateProfiles = (consolidate, shareCache) => __awaiter(void 0, void 0, void 0, function* () {
        const cdmCorpus = testHelper_1.testHelper.getLocalCorpus(testsSubPath, 'ConsCache');
        const srcManifest = yield cdmCorpus.fetchObjectAsync('local:/miniContact.manifest.cdm.json');
        const resManifest = yield srcManifest.createResolvedManifestAsync('_miniContact', '_{n}.cdm.json');
        let profileCache = undefined;
        if (shareCache) {
            profileCache = new internal_1.traitProfileCache();
        }
        let allProfiles = '{';
        for (const locEntDec of resManifest.entities) {
            const entDef = yield cdmCorpus.fetchObjectAsync(locEntDec.entityPath, resManifest);
            const resOpt = new internal_1.resolveOptions(entDef);
            let entTraitInfos = entDef.fetchTraitProfiles(resOpt, profileCache);
            if (consolidate) {
                entTraitInfos = internal_1.traitProfile.consolidateList(entTraitInfos, profileCache);
            }
            let entProfileDump = JSON.stringify(entTraitInfos, fixProfiles);
            let attProfiles = "";
            for (const att of entDef.attributes) {
                let attTraitInfos = att.fetchTraitProfiles(resOpt, profileCache);
                if (consolidate) {
                    attTraitInfos = internal_1.traitProfile.consolidateList(attTraitInfos);
                }
                let attProfileDump = JSON.stringify(attTraitInfos, fixProfiles);
                attProfiles += `${(attProfiles == '' ? '' : ',')}\"${att.name}\":${attProfileDump}`;
            }
            allProfiles += `${(allProfiles == '{' ? '' : ',')}\"${entDef.entityName}\":{\"entityProfile\":${entProfileDump}, \"attProfiles\":{${attProfiles}}}`;
        }
        allProfiles += '}';
        const outName = `ProfileDump${(consolidate ? 'Cons' : '')}${(shareCache ? 'Cache' : '')}.json`;
        testHelper_1.testHelper.writeActualOutputFileContent(testsSubPath, 'ConsCache', outName, allProfiles);
        const refProfiles = testHelper_1.testHelper.getExpectedOutputFileContent(testsSubPath, 'ConsCache', outName);
        testHelper_1.testHelper.assertSameObjectWasSerialized(allProfiles, refProfiles);
    });
    /**
     * get profiles for entities and attributes of entities in manifest.
     * leave profiles in 'deep' form
     * no shared cache
     **/
    it('originalProfileNoCache', () => __awaiter(void 0, void 0, void 0, function* () {
        yield dumpAndValidateProfiles(false, false);
    }));
    /**
     * get profiles for entities and attributes of entities in manifest.
     * leave profiles in 'deep' form
     * use a shared cache of profile objects for all
     **/
    it('consolidatedProfileNoCache', () => __awaiter(void 0, void 0, void 0, function* () {
        yield dumpAndValidateProfiles(true, false);
    }));
    /**
     * get profiles for entities and attributes of entities in manifest.
     * consolidate the profiles
     * no shared cache
     **/
    it('originalProfileSharedCache', () => __awaiter(void 0, void 0, void 0, function* () {
        yield dumpAndValidateProfiles(false, true);
    }));
    /**
     * get profiles for entities and attributes of entities in manifest.
     * consolidate the profiles
     * use a shared cache of profile objects for all
     **/
    it('consolidatedProfileSharedCache', () => __awaiter(void 0, void 0, void 0, function* () {
        yield dumpAndValidateProfiles(true, true);
    }));
});
//# sourceMappingURL=TraitProfile.test.js.map