"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.CommonTest = void 0;
const path = require("path");
const fs = require("fs");
const internal_1 = require("../../../internal");
const Storage_1 = require("../../../Storage");
const testHelper_1 = require("../../testHelper");
/**
 * Base class for all the new resolution guidance tests.
 */
// tslint:disable:variable-name
class CommonTest {
    /**
     * This method runs the tests with a set expected attributes & attribute context values and validated the actual result.
     */
    static runTestWithValues(testName, sourceEntityName, expectedContext_default, expectedContext_normalized, expectedContext_referenceOnly, expectedContext_structured, expectedContext_normalized_structured, expectedContext_referenceOnly_normalized, expectedContext_referenceOnly_structured, expectedContext_referenceOnly_normalized_structured, expected_default, expected_normalized, expected_referenceOnly, expected_structured, expected_normalized_structured, expected_referenceOnly_normalized, expected_referenceOnly_structured, expected_referenceOnly_normalized_structured) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const testInputPath = testHelper_1.testHelper.getInputFolderPath(this.testsSubpath, testName);
                let testActualPath = testHelper_1.testHelper.getActualOutputFolderPath(this.testsSubpath, testName);
                let testExpectedPath = testHelper_1.testHelper.getExpectedOutputFolderPath(this.testsSubpath, testName);
                const corpusPath = testInputPath.substring(0, testInputPath.length - '/Input'.length);
                testActualPath = path.resolve(testActualPath);
                testExpectedPath = path.resolve(testExpectedPath);
                const corpus = new internal_1.CdmCorpusDefinition();
                corpus.setEventCallback(() => { }, internal_1.cdmStatusLevel.warning);
                corpus.storage.mount('local', new Storage_1.LocalAdapter(corpusPath));
                corpus.storage.mount('cdm', new Storage_1.LocalAdapter(this.schemaDocsPath));
                corpus.storage.defaultNamespace = 'local';
                const outFolderPath = `${corpus.storage.adapterPathToCorpusPath(testActualPath)}/`; // interesting 'bug'
                const outFolder = yield corpus.fetchObjectAsync(outFolderPath);
                const srcEntityDef = yield corpus.fetchObjectAsync(`local:/Input/${sourceEntityName}.cdm.json/${sourceEntityName}`);
                expect(srcEntityDef)
                    .toBeTruthy();
                let resOpt;
                let resolvedEntityDef;
                let outputEntityName = '';
                let outputEntityFileName = '';
                let entityFileName = '';
                if (expectedContext_default && expected_default) {
                    entityFileName = 'd';
                    resOpt = new internal_1.resolveOptions(srcEntityDef.inDocument, new internal_1.AttributeResolutionDirectiveSet(new Set()));
                    outputEntityName = `${sourceEntityName}_R_${entityFileName}`;
                    outputEntityFileName = `${outputEntityName}.cdm.json`;
                    resolvedEntityDef = yield srcEntityDef.createResolvedEntityAsync(outputEntityName, resOpt, outFolder);
                    yield this.saveActualEntityAndValidateWithExpected(path.join(testExpectedPath, outputEntityFileName), resolvedEntityDef);
                }
                if (expectedContext_normalized && expected_normalized) {
                    entityFileName = 'n';
                    resOpt = new internal_1.resolveOptions(srcEntityDef.inDocument, new internal_1.AttributeResolutionDirectiveSet(new Set(['normalized'])));
                    outputEntityName = `${sourceEntityName}_R_${entityFileName}`;
                    outputEntityFileName = `${outputEntityName}.cdm.json`;
                    resolvedEntityDef = yield srcEntityDef.createResolvedEntityAsync(outputEntityName, resOpt, outFolder);
                    yield this.saveActualEntityAndValidateWithExpected(path.join(testExpectedPath, outputEntityFileName), resolvedEntityDef);
                }
                if (expectedContext_referenceOnly && expected_referenceOnly) {
                    entityFileName = 'ro';
                    resOpt = new internal_1.resolveOptions(srcEntityDef.inDocument, new internal_1.AttributeResolutionDirectiveSet(new Set(['referenceOnly'])));
                    outputEntityName = `${sourceEntityName}_R_${entityFileName}`;
                    outputEntityFileName = `${outputEntityName}.cdm.json`;
                    resolvedEntityDef = yield srcEntityDef.createResolvedEntityAsync(outputEntityName, resOpt, outFolder);
                    yield this.saveActualEntityAndValidateWithExpected(path.join(testExpectedPath, outputEntityFileName), resolvedEntityDef);
                }
                if (expectedContext_structured && expected_structured) {
                    entityFileName = 's';
                    resOpt = new internal_1.resolveOptions(srcEntityDef.inDocument, new internal_1.AttributeResolutionDirectiveSet(new Set(['structured'])));
                    outputEntityName = `${sourceEntityName}_R_${entityFileName}`;
                    outputEntityFileName = `${outputEntityName}.cdm.json`;
                    resolvedEntityDef = yield srcEntityDef.createResolvedEntityAsync(outputEntityName, resOpt, outFolder);
                    yield this.saveActualEntityAndValidateWithExpected(path.join(testExpectedPath, outputEntityFileName), resolvedEntityDef);
                }
                if (expectedContext_normalized_structured && expected_normalized_structured) {
                    entityFileName = 'n_s';
                    resOpt = new internal_1.resolveOptions(srcEntityDef.inDocument, new internal_1.AttributeResolutionDirectiveSet(new Set(['normalized', 'structured'])));
                    outputEntityName = `${sourceEntityName}_R_${entityFileName}`;
                    outputEntityFileName = `${outputEntityName}.cdm.json`;
                    resolvedEntityDef = yield srcEntityDef.createResolvedEntityAsync(outputEntityName, resOpt, outFolder);
                    yield this.saveActualEntityAndValidateWithExpected(path.join(testExpectedPath, outputEntityFileName), resolvedEntityDef);
                }
                if (expectedContext_referenceOnly_normalized && expected_referenceOnly_normalized) {
                    entityFileName = 'ro_n';
                    resOpt = new internal_1.resolveOptions(srcEntityDef.inDocument, new internal_1.AttributeResolutionDirectiveSet(new Set(['referenceOnly', 'normalized'])));
                    outputEntityName = `${sourceEntityName}_R_${entityFileName}`;
                    outputEntityFileName = `${outputEntityName}.cdm.json`;
                    resolvedEntityDef = yield srcEntityDef.createResolvedEntityAsync(outputEntityName, resOpt, outFolder);
                    yield this.saveActualEntityAndValidateWithExpected(path.join(testExpectedPath, outputEntityFileName), resolvedEntityDef);
                }
                if (expectedContext_referenceOnly_structured && expected_referenceOnly_structured) {
                    entityFileName = 'ro_s';
                    resOpt = new internal_1.resolveOptions(srcEntityDef.inDocument, new internal_1.AttributeResolutionDirectiveSet(new Set(['referenceOnly', 'structured'])));
                    outputEntityName = `${sourceEntityName}_R_${entityFileName}`;
                    outputEntityFileName = `${outputEntityName}.cdm.json`;
                    resolvedEntityDef = yield srcEntityDef.createResolvedEntityAsync(outputEntityName, resOpt, outFolder);
                    yield this.saveActualEntityAndValidateWithExpected(path.join(testExpectedPath, outputEntityFileName), resolvedEntityDef);
                }
                if (expectedContext_referenceOnly_normalized_structured && expected_referenceOnly_normalized_structured) {
                    entityFileName = 'ro_n_s';
                    resOpt = new internal_1.resolveOptions(srcEntityDef.inDocument, new internal_1.AttributeResolutionDirectiveSet(new Set(['referenceOnly', 'normalized', 'structured'])));
                    outputEntityName = `${sourceEntityName}_R_${entityFileName}`;
                    outputEntityFileName = `${outputEntityName}.cdm.json`;
                    resolvedEntityDef = yield srcEntityDef.createResolvedEntityAsync(outputEntityName, resOpt, outFolder);
                    yield this.saveActualEntityAndValidateWithExpected(path.join(testExpectedPath, outputEntityFileName), resolvedEntityDef);
                }
            }
            catch (err) {
                expect(true)
                    .toEqual(false);
            }
        });
    }
    /**
     * Runs validation to test actual output vs expected output for attributes collection vs attribute context.
     */
    static saveActualEntityAndValidateWithExpected(expectedPath, actualResolvedEntityDef) {
        return __awaiter(this, void 0, void 0, function* () {
            const options = new internal_1.copyOptions();
            options.saveConfigFile = false;
            yield actualResolvedEntityDef.inDocument.saveAsAsync(actualResolvedEntityDef.inDocument.name, false, options);
            const actualPath = actualResolvedEntityDef.ctx.corpus.storage.corpusPathToAdapterPath(actualResolvedEntityDef.inDocument.atCorpusPath);
            const actualCtx = JSON.parse(fs.readFileSync(actualPath, 'utf8'));
            const expectedCtx = JSON.parse(fs.readFileSync(expectedPath, 'utf8'));
            testHelper_1.testHelper.assertObjectContentEquality(expectedCtx, actualCtx);
        });
    }
}
exports.CommonTest = CommonTest;
/**
 * The path of the SchemaDocs project.
 */
CommonTest.schemaDocsPath = testHelper_1.testHelper.schemaDocumentsPath;
/**
 * The test's data path.
 */
CommonTest.testsSubpath = 'Cdm/ResolutionGuidance';
//# sourceMappingURL=CommonTest.js.map