"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CliCredentialProviderService = void 0;
const common_1 = require("@nestjs/common");
const cloud_provider_enum_1 = require("../../../src/modules/providers/enums/cloud-provider.enum");
const config_storage_1 = require("./config-storage");
/**
 * CLI-specific credential provider that uses file-based ConfigStorage
 * instead of TypeORM repositories.
 *
 * This allows the CLI to work without a database connection while
 * maintaining compatibility with the existing provider architecture.
 */
let CliCredentialProviderService = class CliCredentialProviderService {
    constructor() {
        this.configStorage = new config_storage_1.ConfigStorage();
    }
    /**
     * Get active API token for a cloud provider from CLI config file
     */
    async getActiveApiToken(provider) {
        const providerName = this.mapProviderEnumToString(provider);
        // Scaleway authenticates HTTP calls with the Secret Key (sent as
        // X-Auth-Token). Most code paths in the provider services treat this as
        // a single "API token" string, so return the secretKey here while keeping
        // the access/secret pair available via getActiveAccessKeyPair().
        if (provider === cloud_provider_enum_1.CloudProvider.SCALEWAY) {
            const creds = this.configStorage.getCredentials(providerName);
            if (!creds?.secretKey) {
                throw new common_1.NotFoundException(`No credentials configured for ${providerName}. ` +
                    `Run: flui config set ${providerName}`);
            }
            return creds.secretKey;
        }
        const token = this.configStorage.getToken(providerName);
        if (!token) {
            throw new common_1.NotFoundException(`No API token configured for ${providerName}. ` +
                `Run: flui config set ${providerName} YOUR_TOKEN`);
        }
        return token;
    }
    /**
     * Get the access/secret key pair for providers that authenticate with two values
     * (e.g. Scaleway IAM API keys).
     */
    async getActiveAccessKeyPair(provider) {
        const providerName = this.mapProviderEnumToString(provider);
        const creds = this.configStorage.getCredentials(providerName);
        if (!creds?.accessKey || !creds?.secretKey) {
            throw new common_1.NotFoundException(`No credentials configured for ${providerName}. ` +
                `Run: flui config set ${providerName}`);
        }
        return { accessKey: creds.accessKey, secretKey: creds.secretKey };
    }
    async getActiveBearerToken(provider) {
        throw new Error('Bearer token authentication is not supported in CLI. ' +
            'Please use API token authentication instead.');
    }
    /**
     * Map CloudProvider enum to lowercase string for config storage
     */
    mapProviderEnumToString(provider) {
        switch (provider) {
            case cloud_provider_enum_1.CloudProvider.HETZNER:
                return 'hetzner';
            case cloud_provider_enum_1.CloudProvider.SCALEWAY:
                return 'scaleway';
            default:
                throw new Error(`Unsupported provider: ${provider}`);
        }
    }
};
exports.CliCredentialProviderService = CliCredentialProviderService;
exports.CliCredentialProviderService = CliCredentialProviderService = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [])
], CliCredentialProviderService);
