"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);
};
var ProviderDefinitionsService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProviderDefinitionsService = void 0;
const common_1 = require("@nestjs/common");
const cloud_provider_enum_1 = require("../../providers/enums/cloud-provider.enum");
const capabilities_provider_factory_1 = require("../../providers/core/factories/capabilities-provider.factory");
let ProviderDefinitionsService = ProviderDefinitionsService_1 = class ProviderDefinitionsService {
    constructor(capabilitiesFactory) {
        this.capabilitiesFactory = capabilitiesFactory;
        this.logger = new common_1.Logger(ProviderDefinitionsService_1.name);
    }
    async getAllProviders() {
        const supportedProviders = this.capabilitiesFactory.getSupportedProviders();
        const providerDefinitions = await Promise.all(supportedProviders.map(async (provider) => {
            return this.getProvider(provider);
        }));
        return providerDefinitions.filter(Boolean).filter((p) => p.enabled);
    }
    async getProvider(provider) {
        if (!this.capabilitiesFactory.isProviderSupported(provider)) {
            return undefined;
        }
        try {
            const capabilitiesService = this.capabilitiesFactory.getCapabilitiesService(provider);
            const providerInfo = await capabilitiesService.getProviderInfo();
            const capabilities = capabilitiesService.getStaticCapabilities();
            return {
                id: provider,
                name: providerInfo.name,
                displayName: providerInfo.displayName,
                description: providerInfo.description,
                logoUrl: providerInfo.logoUrl,
                websiteUrl: providerInfo.websiteUrl,
                documentationUrl: providerInfo.documentationUrl,
                accessKeyDocumentationUrl: providerInfo.accessKeyDocumentationUrl,
                supportUrl: providerInfo.supportUrl,
                pricingUrl: providerInfo.pricingUrl,
                enabled: this.isProviderEnabled(provider),
                capabilities,
                credentialFields: providerInfo.credentialFields,
                dnsZoneDelegation: providerInfo.dnsZoneDelegation,
            };
        }
        catch (error) {
            this.logger.error(`Error loading provider ${provider}:`, error);
            return undefined;
        }
    }
    async getProviderCapabilities(provider) {
        if (!this.capabilitiesFactory.isProviderSupported(provider)) {
            return undefined;
        }
        try {
            const capabilitiesService = this.capabilitiesFactory.getCapabilitiesService(provider);
            return await capabilitiesService.getCapabilities();
        }
        catch (error) {
            this.logger.error(`Error loading capabilities for ${provider}:`, error);
            return undefined;
        }
    }
    async getProviderRegions(provider) {
        if (!this.capabilitiesFactory.isProviderSupported(provider)) {
            return [];
        }
        try {
            const capabilitiesService = this.capabilitiesFactory.getCapabilitiesService(provider);
            return await capabilitiesService.getAvailableRegions();
        }
        catch (error) {
            this.logger.error(`Error loading regions for ${provider}:`, error);
            return [];
        }
    }
    async getProviderInstanceTypes(provider) {
        if (!this.capabilitiesFactory.isProviderSupported(provider)) {
            return [];
        }
        try {
            const capabilitiesService = this.capabilitiesFactory.getCapabilitiesService(provider);
            return await capabilitiesService.getSupportedInstanceTypes();
        }
        catch (error) {
            this.logger.error(`Error loading instance types for ${provider}:`, error);
            return [];
        }
    }
    getProviderLogo(provider) {
        if (!this.capabilitiesFactory.isProviderSupported(provider)) {
            throw new Error(`Provider ${provider} not found`);
        }
        const svc = this.capabilitiesFactory.getCapabilitiesService(provider);
        return { data: svc.getLogo(), contentType: svc.getLogoContentType() };
    }
    getSupportedProviders() {
        return this.capabilitiesFactory.getSupportedProviders();
    }
    isProviderSupported(provider) {
        return this.capabilitiesFactory.isProviderSupported(provider);
    }
    /**
     * Check if a provider is enabled and available for use
     * Hetzner: enabled
     * Contabo: disabled (under development)
     */
    isProviderEnabled(provider) {
        switch (provider) {
            case cloud_provider_enum_1.CloudProvider.HETZNER:
                return true;
            case cloud_provider_enum_1.CloudProvider.CONTABO:
                return false;
            case cloud_provider_enum_1.CloudProvider.SCALEWAY:
                return true;
            default:
                return false;
        }
    }
};
exports.ProviderDefinitionsService = ProviderDefinitionsService;
exports.ProviderDefinitionsService = ProviderDefinitionsService = ProviderDefinitionsService_1 = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [capabilities_provider_factory_1.CapabilitiesProviderFactory])
], ProviderDefinitionsService);
