"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.adlsModelJsonTestHelper = void 0;
const fs = require("fs");
const adlsTestHelper_1 = require("./adlsTestHelper");
const testHelper_1 = require("./testHelper");
var adlsModelJsonTestHelper;
(function (adlsModelJsonTestHelper) {
    adlsModelJsonTestHelper.hostnamePlaceholder = '<HOSTNAME>';
    adlsModelJsonTestHelper.rootPathPlaceholder = '/<ROOTPATH>';
    adlsModelJsonTestHelper.actualFolderName = 'Actual';
    adlsModelJsonTestHelper.expectedFolderName = 'Expected';
    adlsModelJsonTestHelper.inputFolderName = 'Input';
    function getActualSubFolderPath(testSubPath, testName, subFolderName) {
        return `${testHelper_1.testHelper.getActualOutputFolderPath(testSubPath, testName)}/${subFolderName}`;
    }
    adlsModelJsonTestHelper.getActualSubFolderPath = getActualSubFolderPath;
    function getExpectedFileContent(testSubPath, testName, fileName) {
        return fs.readFileSync(`${testHelper_1.testHelper.getActualOutputFolderPath(testSubPath, testName)}/${adlsModelJsonTestHelper.expectedFolderName}/${fileName}`)
            .toString();
    }
    adlsModelJsonTestHelper.getExpectedFileContent = getExpectedFileContent;
    function saveModelJsonToActualOutput(testSubPath, testName, fileName, content) {
        const actualFolderPath = getActualSubFolderPath(testSubPath, testName, adlsModelJsonTestHelper.actualFolderName);
        if (!fs.existsSync(actualFolderPath)) {
            fs.mkdirSync(actualFolderPath);
        }
        fs.writeFileSync(`${actualFolderPath}/${fileName}`, content);
    }
    adlsModelJsonTestHelper.saveModelJsonToActualOutput = saveModelJsonToActualOutput;
    function updateInputAndExpectedAndSaveToActualSubFolder(testSubPath, testName, rootRelativePath, fileName, stringToAddSuffix, suffix) {
        // Prepare the root path.
        let rootPath = process.env['ADLS_ROOTPATH'];
        rootPath = adlsTestHelper_1.adlsTestHelper.getFullRootPath(rootPath, rootRelativePath);
        if (!rootPath.startsWith('/')) {
            rootPath = `/${rootPath}`;
        }
        if (rootPath.endsWith('/')) {
            rootPath = rootPath.substring(0, rootPath.length - 1);
        }
        rootPath = encodeURIComponent(rootPath)
            .replace(/%2F/g, '/');
        // Update input content and save to ActualOutput/Input
        let inputContent = testHelper_1.testHelper.getInputFileContent(testSubPath, testName, fileName);
        inputContent = replacePlaceholder(inputContent, rootPath, stringToAddSuffix, suffix);
        const inputFolderPath = getActualSubFolderPath(testSubPath, testName, adlsModelJsonTestHelper.inputFolderName);
        if (!fs.existsSync(inputFolderPath)) {
            fs.mkdirSync(inputFolderPath);
        }
        fs.writeFileSync(`${inputFolderPath}/${fileName}`, inputContent);
        // Update expected content and save to ActualOutput/Expected
        let expectedContent = testHelper_1.testHelper.getExpectedOutputFileContent(testSubPath, testName, fileName, true);
        expectedContent = replacePlaceholder(expectedContent, rootPath, stringToAddSuffix, suffix);
        const expectedFolderPath = getActualSubFolderPath(testSubPath, testName, adlsModelJsonTestHelper.expectedFolderName);
        if (!fs.existsSync(expectedFolderPath)) {
            fs.mkdirSync(expectedFolderPath);
        }
        fs.writeFileSync(`${expectedFolderPath}/${fileName}`, expectedContent);
    }
    adlsModelJsonTestHelper.updateInputAndExpectedAndSaveToActualSubFolder = updateInputAndExpectedAndSaveToActualSubFolder;
    function replacePlaceholder(content, root, stringToAddSuffix, suffix) {
        content = content.replace(new RegExp(adlsModelJsonTestHelper.hostnamePlaceholder, 'g'), process.env['ADLS_HOSTNAME']);
        content = content.replace(new RegExp(adlsModelJsonTestHelper.rootPathPlaceholder, 'g'), root);
        content = content.replace(new RegExp(stringToAddSuffix, 'g'), `${stringToAddSuffix}-${suffix}`);
        return content;
    }
    adlsModelJsonTestHelper.replacePlaceholder = replacePlaceholder;
})(adlsModelJsonTestHelper = exports.adlsModelJsonTestHelper || (exports.adlsModelJsonTestHelper = {}));
;
//# sourceMappingURL=adlsModelJsonTestHelper.js.map