"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 });
const internal_1 = require("../../../internal");
const testHelper_1 = require("../../testHelper");
/**
 * Unit test for SearchStructure functions
 * E.g.: Given the following tree
 *
 *     11  12  13 =====      14    15  17
 *     |   |   |       |     |     |   |
 *     |   |   |       |     |     |   |
 *     7   8   9 ==    10    16    7   8
 *     |   |   |   |   |     |     |   |
 *     |   |   |   |   |     |     |   |
 *     2   1   4   5   6     1     2   1
 *     |                           |
 *     |                           |
 *     1                           1
 *
 * Leaf Node Searches:
 * - Search for 11's leaf nodes would be 1
 * - Search for 10's leaf nodes would be 6
 * - Search for 9's leaf nodes would be 4, 5
 * - Search for 4's leaf nodes would be 4
 *
 * Top Node Searches:
 * - Search for 1's top node would be 12 14 17
 * - Search for 13's top node would be 13
 * - Search for 5's top node would be 13
 * - Search for 2's top node would be 11 15
 */
describe('Cdm/Projection/SearchStructureUnitTest', () => {
    /**
     * The path between TestDataPath and TestName.
     */
    const testsSubpath = 'Cdm/Projection/TestSearchStructure';
    /**
     * Unit test for building a tree
     */
    it('TestBuildTree', () => {
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'TestBuildTree');
        const pc = buildFakeTree(corpus);
        expect(searchTreeTraversal(pc, '1'))
            .toEqual('1 ==>> [ 12 14 17 .. 1 ..  1]');
        expect(searchTreeTraversal(pc, '8'))
            .toEqual('8 ==>> [ 12 17 .. 8 ..  1]');
        expect(searchTreeTraversal(pc, '13'))
            .toEqual('13 ==>> [ 13 .. 13 ..  4 5 6]');
        expect(searchTreeTraversal(pc, '9'))
            .toEqual('9 ==>> [ 13 .. 9 ..  4 5]');
        expect(searchTreeTraversal(pc, '5'))
            .toEqual('5 ==>> [ 13 .. 5 ..  5]');
        expect(searchTreeTraversal(pc, '7'))
            .toEqual('7 ==>> [ 11 15 .. 7 ..  1]');
    });
    function buildFakeTree(corpus) {
        const projDir = new internal_1.ProjectionDirective(new internal_1.resolveOptions(), undefined);
        const pc = new internal_1.ProjectionContext(projDir, undefined);
        const p1 = new internal_1.ProjectionAttributeState(corpus.ctx);
        p1.currentResolvedAttribute = new internal_1.ResolvedAttribute(projDir.resOpt, new internal_1.CdmTypeAttributeDefinition(corpus.ctx, '1'), '1', undefined);
        const p2 = new internal_1.ProjectionAttributeState(corpus.ctx);
        p2.currentResolvedAttribute = new internal_1.ResolvedAttribute(projDir.resOpt, new internal_1.CdmTypeAttributeDefinition(corpus.ctx, '2'), '2', undefined);
        const p4 = new internal_1.ProjectionAttributeState(corpus.ctx);
        p4.currentResolvedAttribute = new internal_1.ResolvedAttribute(projDir.resOpt, new internal_1.CdmTypeAttributeDefinition(corpus.ctx, '4'), '4', undefined);
        const p5 = new internal_1.ProjectionAttributeState(corpus.ctx);
        p5.currentResolvedAttribute = new internal_1.ResolvedAttribute(projDir.resOpt, new internal_1.CdmTypeAttributeDefinition(corpus.ctx, '5'), '5', undefined);
        const p6 = new internal_1.ProjectionAttributeState(corpus.ctx);
        p6.currentResolvedAttribute = new internal_1.ResolvedAttribute(projDir.resOpt, new internal_1.CdmTypeAttributeDefinition(corpus.ctx, '6'), '6', undefined);
        const p7 = new internal_1.ProjectionAttributeState(corpus.ctx);
        p7.currentResolvedAttribute = new internal_1.ResolvedAttribute(projDir.resOpt, new internal_1.CdmTypeAttributeDefinition(corpus.ctx, '7'), '7', undefined);
        const p8 = new internal_1.ProjectionAttributeState(corpus.ctx);
        p8.currentResolvedAttribute = new internal_1.ResolvedAttribute(projDir.resOpt, new internal_1.CdmTypeAttributeDefinition(corpus.ctx, '8'), '8', undefined);
        const p9 = new internal_1.ProjectionAttributeState(corpus.ctx);
        p9.currentResolvedAttribute = new internal_1.ResolvedAttribute(projDir.resOpt, new internal_1.CdmTypeAttributeDefinition(corpus.ctx, '9'), '9', undefined);
        const p10 = new internal_1.ProjectionAttributeState(corpus.ctx);
        p10.currentResolvedAttribute = new internal_1.ResolvedAttribute(projDir.resOpt, new internal_1.CdmTypeAttributeDefinition(corpus.ctx, '10'), '10', undefined);
        const p11 = new internal_1.ProjectionAttributeState(corpus.ctx);
        p11.currentResolvedAttribute = new internal_1.ResolvedAttribute(projDir.resOpt, new internal_1.CdmTypeAttributeDefinition(corpus.ctx, '11'), '11', undefined);
        const p12 = new internal_1.ProjectionAttributeState(corpus.ctx);
        p12.currentResolvedAttribute = new internal_1.ResolvedAttribute(projDir.resOpt, new internal_1.CdmTypeAttributeDefinition(corpus.ctx, '12'), '12', undefined);
        const p13 = new internal_1.ProjectionAttributeState(corpus.ctx);
        p13.currentResolvedAttribute = new internal_1.ResolvedAttribute(projDir.resOpt, new internal_1.CdmTypeAttributeDefinition(corpus.ctx, '13'), '13', undefined);
        const p14 = new internal_1.ProjectionAttributeState(corpus.ctx);
        p14.currentResolvedAttribute = new internal_1.ResolvedAttribute(projDir.resOpt, new internal_1.CdmTypeAttributeDefinition(corpus.ctx, '14'), '14', undefined);
        const p15 = new internal_1.ProjectionAttributeState(corpus.ctx);
        p15.currentResolvedAttribute = new internal_1.ResolvedAttribute(projDir.resOpt, new internal_1.CdmTypeAttributeDefinition(corpus.ctx, '15'), '15', undefined);
        const p16 = new internal_1.ProjectionAttributeState(corpus.ctx);
        p16.currentResolvedAttribute = new internal_1.ResolvedAttribute(projDir.resOpt, new internal_1.CdmTypeAttributeDefinition(corpus.ctx, '16'), '16', undefined);
        const p17 = new internal_1.ProjectionAttributeState(corpus.ctx);
        p17.currentResolvedAttribute = new internal_1.ResolvedAttribute(projDir.resOpt, new internal_1.CdmTypeAttributeDefinition(corpus.ctx, '17'), '17', undefined);
        p1.previousStateList = [];
        p2.previousStateList = [];
        p4.previousStateList = [];
        p5.previousStateList = [];
        p6.previousStateList = [];
        p7.previousStateList = [];
        p8.previousStateList = [];
        p9.previousStateList = [];
        p10.previousStateList = [];
        p11.previousStateList = [];
        p12.previousStateList = [];
        p13.previousStateList = [];
        p14.previousStateList = [];
        p15.previousStateList = [];
        p16.previousStateList = [];
        p17.previousStateList = [];
        p11.previousStateList.push(p7);
        p7.previousStateList.push(p2);
        p2.previousStateList.push(p1);
        p12.previousStateList.push(p8);
        p8.previousStateList.push(p1);
        p13.previousStateList.push(p9);
        p9.previousStateList.push(p4);
        p9.previousStateList.push(p5);
        p13.previousStateList.push(p10);
        p10.previousStateList.push(p6);
        p14.previousStateList.push(p16);
        p16.previousStateList.push(p1);
        p15.previousStateList.push(p7);
        p17.previousStateList.push(p8);
        pc.currentAttributeStateSet.add(p11);
        pc.currentAttributeStateSet.add(p12);
        pc.currentAttributeStateSet.add(p13);
        pc.currentAttributeStateSet.add(p14);
        pc.currentAttributeStateSet.add(p15);
        pc.currentAttributeStateSet.add(p17);
        return pc;
    }
    function searchTreeTraversal(pc, val) {
        let result = new internal_1.SearchResult();
        for (const top of pc.currentAttributeStateSet.states) {
            let st = new internal_1.SearchStructure();
            st = internal_1.SearchStructure.buildStructure(top, top, val, st, false, 0);
            if ((st === null || st === void 0 ? void 0 : st.result.foundFlag) === true) {
                if (result.foundFlag === false) {
                    result = st.result;
                }
                else if (result.foundDepth > (st === null || st === void 0 ? void 0 : st.result.foundDepth)) {
                    result = st.result;
                }
                else if (result.foundDepth === (st === null || st === void 0 ? void 0 : st.result.foundDepth)) {
                    for (const newTops of st === null || st === void 0 ? void 0 : st.result.top) {
                        result.top.push(newTops);
                    }
                }
            }
        }
        return result.foundFlag ? getResult(val, result) : '';
    }
    function getResult(val, result) {
        let foundOrLeaf = '';
        if (result.leaf.length > 0) {
            for (const leaf of result.leaf) {
                foundOrLeaf = foundOrLeaf + ` ${leaf.currentResolvedAttribute.resolvedName}`;
            }
        }
        else {
            foundOrLeaf = 'N/A';
        }
        let foundOrTop = '';
        if (result.top.length > 0) {
            for (const top of result.top) {
                foundOrTop = foundOrTop + ` ${top.currentResolvedAttribute.resolvedName}`;
            }
        }
        else {
            foundOrTop = 'N/A';
        }
        return (`${val} ==>> [${foundOrTop} .. ${result.found.currentResolvedAttribute.resolvedName} .. ${foundOrLeaf}]`);
    }
});
//# sourceMappingURL=SearchStructure.test.js.map