"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.AttributeExpectedValue = exports.AttributeContextExpectedValue = exports.ObjectValidator = void 0;
const internal_1 = require("../../internal");
/**
 * Helper class that supports validation of the actual object wrt expected object.
 */
class ObjectValidator {
    static validateAttributeContext(expected, actual) {
        if (!expected || !actual) {
            expect(expected)
                .toBeFalsy();
            expect(actual)
                .toBeFalsy();
            return;
        }
        expect(internal_1.cdmAttributeContextType[actual.type].toLowerCase())
            .toEqual(expected.type.toLowerCase());
        expect(actual.name)
            .toEqual(expected.name);
        if (actual.parent) {
            expect(actual.parent.namedReference)
                .toEqual(expected.parent);
        }
        if (expected.definition && actual.definition) {
            expect(actual.definition.namedReference)
                .toEqual(expected.definition);
        }
        let expCount = 0;
        if (expected.contexts && expected.contexts.length > 0) {
            expCount += expected.contexts.length;
        }
        if (expected.contextStrings && expected.contextStrings.length > 0) {
            expCount += expected.contextStrings.length;
        }
        expect(actual.contents.length)
            .toEqual(expCount);
        let ac = 0;
        let acs = 0;
        for (let i = 0; i < actual.contents.length; i++) {
            if (actual.contents.allItems[i] instanceof internal_1.CdmAttributeContext) {
                this.validateAttributeContext(expected.contexts[ac++], actual.contents.allItems[i]);
            }
            else if (actual.contents.allItems[i] instanceof internal_1.CdmAttributeReference) {
                const exp = expected.contextStrings[acs++];
                const act = actual.contents.allItems[i];
                expect(act.namedReference)
                    .toEqual(exp);
            }
            else {
                throw new Error('validateAttributeContext: instanceof Unknown');
            }
        }
    }
    static validateAttributesCollection(expected, actual) {
        expect(actual.length)
            .toEqual(expected.length);
        for (let i = 0; i < actual.allItems.length; i++) {
            if (actual.allItems[i] instanceof internal_1.CdmTypeAttributeDefinition) {
                const exp = expected[i];
                const act = actual.allItems[i];
                this.validateTypeAttributeDefinition(exp, act);
            }
            else if (actual.allItems[i] instanceof internal_1.CdmAttributeGroupReference) {
                const exp = expected[i];
                const act = actual.allItems[i];
                this.validateAttributeGroupRef(exp, act);
            }
        }
    }
    static validateTypeAttributeDefinition(expected, actual) {
        expect(internal_1.cdmDataFormat[actual.dataFormat].toLowerCase())
            .toEqual(expected.dataFormat.toLowerCase());
        expect(actual.dataType)
            .toEqual(expected.dataType);
        expect(actual.description)
            .toEqual(expected.description);
        expect(actual.displayName)
            .toEqual(expected.displayName);
        expect(actual.explanation)
            .toEqual(expected.explanation);
        expect(actual.isNullable)
            .toEqual(expected.isNullable);
        expect(actual.isPrimaryKey)
            .toEqual(expected.isPrimaryKey);
        expect(actual.isReadOnly)
            .toEqual(expected.isReadOnly);
        expect(actual.maximumLength)
            .toEqual(expected.maximumLength);
        expect(actual.maximumValue)
            .toEqual(expected.maximumValue);
        expect(actual.minimumValue)
            .toEqual(expected.minimumValue);
        expect(actual.name)
            .toEqual(expected.name);
        expect(actual.purpose)
            .toEqual(expected.purpose);
        expect(actual.sourceName)
            .toEqual(expected.sourceName);
        if (actual.sourceOrdering) {
            expect(actual.sourceOrdering)
                .toEqual(expected.sourceOrdering);
        }
    }
    static validateAttributeGroupRef(expected, actual) {
        if (expected.attributeGroupName !== undefined || expected.members !== undefined) {
            if (actual.explicitReference) {
                const actualObj = actual.explicitReference;
                if (expected.attributeGroupName) {
                    expect(actualObj.attributeGroupName)
                        .toEqual(expected.attributeGroupName);
                }
                if (expected.attributeContext) {
                    expect(actualObj.attributeContext.namedReference)
                        .toEqual(expected.attributeContext);
                }
                if (expected.members) {
                    expect(actualObj.members.length)
                        .toEqual(expected.members.length);
                    for (let i = 0; i < actualObj.members.length; i++) {
                        if (actualObj.members.allItems[i] instanceof internal_1.CdmTypeAttributeDefinition) {
                            const exp = expected.members[i];
                            const act = actualObj.members.allItems[i];
                            this.validateTypeAttributeDefinition(exp, act);
                        }
                        else if (actualObj.members.allItems[i] instanceof internal_1.CdmAttributeGroupReference) {
                            const exp = expected.members[i];
                            const act = actualObj.members.allItems[i];
                            this.validateAttributeGroupRef(exp, act);
                        }
                        else {
                            throw new Error('Unknown type!');
                        }
                    }
                }
            }
        }
    }
}
exports.ObjectValidator = ObjectValidator;
/***
 * Class to contain AttributeContext's expected values.
 */
class AttributeContextExpectedValue {
}
exports.AttributeContextExpectedValue = AttributeContextExpectedValue;
/***
 * Class to contain Attribute's expected values.
 */
class AttributeExpectedValue {
    constructor() {
        this.isNullable = false;
        this.isPrimaryKey = false;
        this.isReadOnly = false;
    }
}
exports.AttributeExpectedValue = AttributeExpectedValue;
//# sourceMappingURL=ObjectValidator.js.map