"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AzureManagementProvider = void 0;
/* eslint-disable max-len */
const logger_1 = require("../../../logger");
const node_cache_1 = require("../../node-cache");
const identity_1 = require("@azure/identity");
class AzureManagementProvider {
    constructor(azureConfig, logger = logger_1.logger) {
        this.spydraCache = node_cache_1.NodeCache.getInstance();
        this.logger = logger_1.logger;
        // https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/identity/identity/samples/AzureIdentityExamples.md#chaining-credentials
        /**
         * Fetches the secret automatically based on the configured provider.
         * If the provider is 'aws', it retrieves the secret from AWS Secret Manager.
         * If the provider is 'azure', it retrieves the secret from Azure Key Vault.
         * Throws an error if the required parameters are not provided.
         */
        this.autoFetchSecret = async () => {
            throw new Error('Method not implemented.');
        };
        this.verify = async () => {
            if (!this.applicationId) {
                throw new Error('Application ID is required for azure credential provider');
            }
            if (!this.applicationSecret) {
                throw new Error('Application Secret is required for azure credential provider');
            }
            if (!this.tenantId) {
                throw new Error('Tenant ID is required for azure credential provider');
            }
            const credential = new identity_1.ClientSecretCredential(this.tenantId, this.applicationId, this.applicationSecret);
            const response = {
                apiError: null,
                apiResponse: null,
            };
            try {
                response.apiResponse = await credential.getToken('https://management.azure.com//.default');
            }
            catch (error) {
                this.logger.error(`Unable to verify azure credentials: ${error}`);
                response.apiError = error;
            }
            return response;
        };
        this.vaultName = azureConfig.vault;
        this.region = azureConfig.region;
        this.accessKey = azureConfig.accessKey;
        this.secretKey = azureConfig.secretKey;
        this.shouldAutoFetchSecret = azureConfig.autoFetchSecret;
        this.autoFetchSecretProvider = azureConfig.autoFetchSecretProvider;
        this.autoFetchSecretVault = azureConfig.autoFetchSecretVault;
        this.autoFetchSecretName = azureConfig.autoFetchSecretName;
        this.logger = logger;
        this.applicationId = azureConfig.applicationId;
        this.applicationSecret = azureConfig.applicationSecret;
        this.tenantId = azureConfig.tenantId;
    }
}
exports.AzureManagementProvider = AzureManagementProvider;
//# sourceMappingURL=AzureManagementProvider.js.map