import _ from 'lodash';
import { FormComponentType, getRowItemsFromSectionItem } from '../types/index.js';
import { InputDataType } from '../types/index.js';
export const MASKED_SECRET = '<redacted>';
export function maskSecrets(datasourceConfig, plugin) {
    plugin?.datasourceTemplate?.sections.forEach((section) => {
        section.items.forEach((sectionItem) => {
            const rowItems = getRowItemsFromSectionItem(sectionItem);
            rowItems.forEach((item) => {
                if (item.dataType === InputDataType.PASSWORD) {
                    maskPassword(datasourceConfig, item);
                }
                else if (item.componentType === 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 = MASKED_SECRET;
            }
        });
    });
}
function maskPassword(datasourceConfig, section) {
    _.set(datasourceConfig, section.name, MASKED_SECRET);
}
export function unmaskSecrets(datasourceConfig, plugin, originalConfiguration) {
    plugin?.datasourceTemplate?.sections.forEach((section) => {
        section.items.forEach((sectionItem) => {
            const rowItems = getRowItemsFromSectionItem(sectionItem);
            rowItems.forEach((item) => {
                if (item.dataType === InputDataType.PASSWORD) {
                    unmaskPassword(datasourceConfig, item, originalConfiguration);
                }
                if (item.componentType === 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 = _.find(originalConfiguration[fieldList.name], (element) => element.key === secret, 0);
                    if (originalValue) {
                        config.value = originalValue.value;
                    }
                }
            });
        });
    }
}
function unmaskPassword(datasourceConfig, item, originalConfiguration) {
    if (_.get(datasourceConfig, item.name) === MASKED_SECRET) {
        _.set(datasourceConfig, item.name, _.get(originalConfiguration, item.name));
    }
}
//# sourceMappingURL=configuration.js.map