"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SecretProviderService = void 0;
/* eslint-disable max-len */
const generate_password_ts_1 = __importDefault(require("generate-password-ts"));
const AWSSecretManagerProvider_1 = require("./providers/AWSSecretManagerProvider");
const AzureKeyVaultSecretProvider_1 = require("./providers/AzureKeyVaultSecretProvider");
const TrusysSecretsVaultService_1 = require("./TrusysSecretsVaultService");
const logger_1 = __importDefault(require("../../logger"));
class SecretProviderService {
    constructor(secretProviderConfig) {
        this.trusysVault = null;
        this.get = async (secretName, secretKey) => {
            return this.tryTrusysVault(() => this.trusysVault.get(secretName, secretKey), () => this.secretProvider.get(secretName, secretKey));
        };
        this.getAll = async (secretName, cacheSecret = true) => {
            return this.tryTrusysVault(() => this.trusysVault.getAll(secretName), () => this.secretProvider.getAll(secretName, cacheSecret));
        };
        this.exist = async (secretName) => {
            if (!this.useTrusysVault || !this.trusysVault) {
                return this.secretProvider.exist(secretName);
            }
            try {
                const exists = await this.trusysVault.exist(secretName);
                if (exists) {
                    return true;
                }
            }
            catch (error) {
                logger_1.default.warn(`TrusysVault exist check failed: ${error}, falling back to cloud provider`);
            }
            return this.secretProvider.exist(secretName);
        };
        this.delete = async (secretName) => {
            if (!this.useTrusysVault || !this.trusysVault) {
                return this.secretProvider.delete(secretName);
            }
            try {
                await this.trusysVault.delete(secretName);
                return;
            }
            catch (error) {
                logger_1.default.warn(`TrusysVault delete failed: ${error}, falling back to cloud provider`);
            }
            return this.secretProvider.delete(secretName);
        };
        this.create = async (secretName, secrets, options) => {
            return this.tryTrusysVault(() => this.trusysVault.create(secretName, secrets), () => this.secretProvider.create(secretName, secrets, options));
        };
        this.deleteMany = async (searchString) => {
            if (!this.useTrusysVault || !this.trusysVault) {
                return this.secretProvider.deleteMany(searchString);
            }
            try {
                await this.trusysVault.deleteMany(searchString);
                return;
            }
            catch (error) {
                logger_1.default.warn(`TrusysVault deleteMany failed: ${error}, falling back to cloud provider`);
            }
            return this.secretProvider.deleteMany(searchString);
        };
        this.update = async (secretName, secrets, options) => {
            return this.tryTrusysVault(() => this.trusysVault.update(secretName, secrets), () => this.secretProvider.update(secretName, secrets, options));
        };
        this.generatePassword = async (length = 22) => {
            const password = generate_password_ts_1.default.generate({
                length,
                numbers: true,
                symbols: false,
                uppercase: true,
                lowercase: true,
                strict: true,
            });
            return password;
        };
        this.existenceCheck = async () => {
            // For existenceCheck, always use cloud provider as it's provider-specific
            return this.secretProvider.existenceCheck();
        };
        this.cloudProvider = secretProviderConfig?.cloudProvider || process.env.CLOUD_PROVIDER || 'azure';
        // Check if MASTER_SECRET_KEY is present in environment
        const masterSecretKey = process.env.MASTER_SECRET_KEY;
        this.useTrusysVault = !!masterSecretKey;
        if (this.useTrusysVault) {
            try {
                this.trusysVault = new TrusysSecretsVaultService_1.TrusysSecretsVaultService(masterSecretKey);
                logger_1.default.info('TrusysSecretsVaultService initialized with MASTER_SECRET_KEY');
            }
            catch (error) {
                logger_1.default.warn(`Failed to initialize TrusysSecretsVaultService: ${error}, falling back to cloud provider`);
                this.useTrusysVault = false;
            }
        }
        // Initialize cloud provider as fallback
        if (this.cloudProvider === 'azure') {
            this.secretProvider = new AzureKeyVaultSecretProvider_1.AzureKeyVaultSecretProvider(secretProviderConfig);
        }
        else if (this.cloudProvider === 'aws') {
            this.secretProvider = new AWSSecretManagerProvider_1.AWSSecretManagerProvider(secretProviderConfig);
        }
        else {
            throw new Error(`Cloud provider ${this.cloudProvider} is not supported.`);
        }
    }
    async tryTrusysVault(operation, fallback) {
        if (!this.useTrusysVault || !this.trusysVault) {
            return fallback();
        }
        try {
            return await operation();
        }
        catch (error) {
            logger_1.default.warn(`TrusysVault operation failed: ${error}, falling back to cloud provider`);
            return fallback();
        }
    }
}
exports.SecretProviderService = SecretProviderService;
//# sourceMappingURL=SecretProviderService.js.map