"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 });
exports.ProjectionOMTestUtil = void 0;
const internal_1 = require("../../../internal");
const LocalAdapter_1 = require("../../../Storage/LocalAdapter");
const testHelper_1 = require("../../testHelper");
const projectionTestUtils_1 = require("../../Utilities/projectionTestUtils");
/**
 * Utility class to help create object model based tests
 */
class ProjectionOMTestUtil {
    /**
     * Utility class constructor
     */
    constructor(className, testName) {
        this.foundationJsonPath = 'cdm:/foundations.cdm.json';
        this.localOutputStorageNS = 'output';
        this.manifestName = 'default';
        this.manifestDocName = `${this.manifestName}.manifest.cdm.json`;
        this.allImportsName = '_allImports';
        this.allImportsDocName = `${this.allImportsName}.cdm.json`;
        this.className = className;
        this.testName = testName;
        this.testsSubpath = `Cdm/Projection/${this.className}`;
        this.inputPath = testHelper_1.testHelper.getInputFolderPath(this.testsSubpath, this.testName);
        this.expectedOutputPath = testHelper_1.testHelper.getExpectedOutputFolderPath(this.testsSubpath, this.testName);
        this.actualOutputPath = testHelper_1.testHelper.getActualOutputFolderPath(this.testsSubpath, this.testName);
        this.corpus = testHelper_1.testHelper.getLocalCorpus(this.testsSubpath, this.testName);
        this.corpus.storage.mount(this.localOutputStorageNS, new LocalAdapter_1.LocalAdapter(this.actualOutputPath));
        this.corpus.storage.defaultNamespace = this.localOutputStorageNS;
        this.localStorageRoot = this.corpus.storage.fetchRootFolder(this.localOutputStorageNS);
        this.defaultManifest = this.createDefaultManifest();
        this.allImports = this.createAndInitializeAllImportsFile();
    }
    get className() {
        return this._className;
    }
    set className(value) {
        this._className = value;
    }
    get testName() {
        return this._testName;
    }
    set testName(value) {
        this._testName = value;
    }
    get corpus() {
        return this._corpus;
    }
    set corpus(value) {
        this._corpus = value;
    }
    get inputPath() {
        return this._inputPath;
    }
    set inputPath(value) {
        this._inputPath = value;
    }
    get expectedOutputPath() {
        return this._expectedOutputPath;
    }
    set expectedOutputPath(value) {
        this._expectedOutputPath = value;
    }
    get actualOutputPath() {
        return this._actualOutputPath;
    }
    set actualOutputPath(value) {
        this._actualOutputPath = value;
    }
    get localStorageRoot() {
        return this._localStorageRoot;
    }
    set localStorageRoot(value) {
        this._localStorageRoot = value;
    }
    get defaultManifest() {
        return this._defaultManifest;
    }
    set defaultManifest(value) {
        this._defaultManifest = value;
    }
    get allImports() {
        return this._allImports;
    }
    set allImports(value) {
        this._allImports = value;
    }
    Dispose() {
        this.corpus = null;
    }
    /**
     * Create a default manifest
     */
    createDefaultManifest() {
        const manifestDefault = this.corpus.MakeObject(internal_1.cdmObjectType.manifestDef, this.manifestName);
        this.localStorageRoot.documents.push(manifestDefault, this.manifestDocName);
        return manifestDefault;
    }
    /**
     * Create and initialize _allImports file
     */
    createAndInitializeAllImportsFile() {
        const allImportsDoc = new internal_1.CdmDocumentDefinition(this.corpus.ctx, this.allImportsName);
        const allImportsDocDef = this.localStorageRoot.documents.push(allImportsDoc, this.allImportsDocName);
        allImportsDocDef.imports.push(this.foundationJsonPath);
        return allImportsDocDef;
    }
    /**
     * Create a simple entity called 'TestSource' with a single attribute
     */
    CreateBasicEntity(entityName, attributesParams) {
        const entity = this.corpus.MakeObject(internal_1.cdmObjectType.entityDef, entityName);
        for (const attributesParam of attributesParams) {
            const attribute = this.corpus.MakeObject(internal_1.cdmObjectType.typeAttributeDef, attributesParam.attributeName, false);
            attribute.dataType = this.corpus.MakeRef(internal_1.cdmObjectType.dataTypeRef, attributesParam.attributeDataType, true);
            attribute.purpose = this.corpus.MakeRef(internal_1.cdmObjectType.purposeRef, attributesParam.attributePurpose, true);
            attribute.displayName = attributesParam.attributeName;
            entity.attributes.push(attribute);
        }
        const entityDoc = this.corpus.MakeObject(internal_1.cdmObjectType.documentDef, `${entityName}.cdm.json`, false);
        entityDoc.imports.push(this.allImportsDocName);
        entityDoc.definitions.push(entity);
        this.localStorageRoot.documents.push(entityDoc, entityDoc.name);
        this.defaultManifest.entities.push(entity);
        this.allImports.imports.push(entity.inDocument.name);
        return entity;
    }
    /**
     * Function to valid the entity
     */
    validateBasicEntity(entity, entityName, attributesParams) {
        // Assert.IsNotNull(entity, $'ValidateBasicEntity: ${entityName} failed!');
        // Assert.AreEqual(entity.Attributes.Count, attributesParams.Count, $'ValidateBasicEntity: Attribute count for ${entityName} failed!');
    }
    /**
     * Create a simple projection object
     */
    createProjection(projectionSourceName) {
        const projection = this.corpus.MakeObject(internal_1.cdmObjectType.projectionDef);
        projection.source = this.corpus.MakeObject(internal_1.cdmObjectType.entityRef, projectionSourceName, true);
        return projection;
    }
    /**
     * Create an inline entity reference for a projection
     */
    createProjectionInlineEntityReference(projection) {
        const projectionInlineEntityRef = this.corpus.MakeObject(internal_1.cdmObjectType.entityRef, null);
        projectionInlineEntityRef.explicitReference = projection;
        return projectionInlineEntityRef;
    }
    /**
     * Create an Input Attribute Operation
     */
    createOperationInputAttribute(projection, includeAttributes) {
        // IncludeAttributes Operation
        const includeAttributesOp = new internal_1.CdmOperationIncludeAttributes(this.corpus.ctx);
        includeAttributesOp.includeAttributes = includeAttributes === null || includeAttributes === void 0 ? void 0 : includeAttributes.slice();
        projection.operations.push(includeAttributesOp);
        return includeAttributesOp;
    }
    /**
     * Create a Combine Attribute Operation
     */
    createOperationCombineAttributes(projection, selectedAttributes, mergeIntoAttribute) {
        // CombineAttributes Operation
        const combineAttributesOp = new internal_1.CdmOperationCombineAttributes(this.corpus.ctx);
        combineAttributesOp.select = selectedAttributes;
        combineAttributesOp.mergeInto = mergeIntoAttribute;
        projection.operations.push(combineAttributesOp);
        return combineAttributesOp;
    }
    /**
     * Create a Type Attribute
     */
    createTypeAttribute(attributeName, dataType, purpose) {
        const dataTypeRef = this.corpus.MakeRef(internal_1.cdmObjectType.dataTypeRef, dataType, false);
        const purposeRef = this.corpus.MakeRef(internal_1.cdmObjectType.purposeRef, purpose, false);
        const attribute = this.corpus.MakeObject(internal_1.cdmObjectType.typeAttributeDef, attributeName, false);
        attribute.dataType = dataTypeRef;
        attribute.purpose = purposeRef;
        return attribute;
    }
    /**
     * Create an entity attribute
     */
    createEntityAttribute(entityAttributeName, projectionSourceEntityRef) {
        const entityAttribute = this.corpus.MakeObject(internal_1.cdmObjectType.entityAttributeDef, entityAttributeName, false);
        entityAttribute.entity = projectionSourceEntityRef;
        return entityAttribute;
    }
    getAndValidateResolvedEntity(entity, resOpts) {
        return __awaiter(this, void 0, void 0, function* () {
            const resolvedEntity = yield projectionTestUtils_1.projectionTestUtils.getResolvedEntity(this.corpus, entity, resOpts);
            expect(resolvedEntity)
                .toBeDefined();
            return resolvedEntity;
        });
    }
}
exports.ProjectionOMTestUtil = ProjectionOMTestUtil;
//# sourceMappingURL=ProjectionOMTestUtil.js.map