"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MASKED_SECRET = void 0;
exports.maskSecrets = maskSecrets;
exports.unmaskSecrets = unmaskSecrets;
const lodash_1 = __importDefault(require("lodash"));
const index_js_1 = require("../types/index.js");
const index_js_2 = require("../types/index.js");
exports.MASKED_SECRET = '<redacted>';
function maskSecrets(datasourceConfig, plugin) {
    plugin?.datasourceTemplate?.sections.forEach((section) => {
        section.items.forEach((sectionItem) => {
            const rowItems = (0, index_js_1.getRowItemsFromSectionItem)(sectionItem);
            rowItems.forEach((item) => {
                if (item.dataType === index_js_2.InputDataType.PASSWORD) {
                    maskPassword(datasourceConfig, item);
                }
                else if (item.componentType === index_js_1.FormComponentType.FIELD_LIST) {
                    maskFieldList(datasourceConfig, item);
                }
            });
        });
    });
}
function maskFieldList(datasourceConfig, section) {
    section.secretsNames?.forEach((secret) => {
        datasourceConfig[section.name].forEach((item) => {
            if (item.key?.toUpperCase() === secret?.toUpperCase()) {
                item.value = exports.MASKED_SECRET;
            }
        });
    });
}
function maskPassword(datasourceConfig, section) {
    lodash_1.default.set(datasourceConfig, section.name, exports.MASKED_SECRET);
}
function unmaskSecrets(datasourceConfig, plugin, originalConfiguration) {
    plugin?.datasourceTemplate?.sections.forEach((section) => {
        section.items.forEach((sectionItem) => {
            const rowItems = (0, index_js_1.getRowItemsFromSectionItem)(sectionItem);
            rowItems.forEach((item) => {
                if (item.dataType === index_js_2.InputDataType.PASSWORD) {
                    unmaskPassword(datasourceConfig, item, originalConfiguration);
                }
                if (item.componentType === index_js_1.FormComponentType.FIELD_LIST) {
                    unmaskFieldList(datasourceConfig, item, originalConfiguration);
                }
            });
        });
    });
}
function unmaskFieldList(datasourceConfig, fieldList, originalConfiguration) {
    if (fieldList.secretsNames) {
        fieldList.secretsNames.forEach((secret) => {
            datasourceConfig[fieldList.name].forEach((config) => {
                if (config.key === secret) {
                    const originalValue = lodash_1.default.find(originalConfiguration[fieldList.name], (element) => element.key === secret, 0);
                    if (originalValue) {
                        config.value = originalValue.value;
                    }
                }
            });
        });
    }
}
function unmaskPassword(datasourceConfig, item, originalConfiguration) {
    if (lodash_1.default.get(datasourceConfig, item.name) === exports.MASKED_SECRET) {
        lodash_1.default.set(datasourceConfig, item.name, lodash_1.default.get(originalConfiguration, item.name));
    }
}
//# sourceMappingURL=configuration.js.map