"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
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 __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiClient = exports.ApiError = void 0;
const axios_1 = __importDefault(require("axios"));
const https = __importStar(require("node:https"));
const common_1 = require("@nestjs/common");
class ApiError extends Error {
    constructor(message, statusCode, details) {
        super(message);
        this.name = 'ApiError';
        this.statusCode = statusCode;
        this.details = details;
    }
}
exports.ApiError = ApiError;
let ApiClient = class ApiClient {
    constructor(config) {
        this.baseUrl = config.baseUrl;
        this.client = axios_1.default.create({
            baseURL: config.baseUrl,
            timeout: config.timeout || 30000,
            headers: {
                'Content-Type': 'application/json',
                ...(config.apiKey ? { Authorization: `Bearer ${config.apiKey}` } : {}),
            },
            httpsAgent: new https.Agent({ rejectUnauthorized: false }),
        });
        // Add response interceptor for error handling
        this.client.interceptors.response.use((response) => response, (error) => {
            return Promise.reject(this.normalizeError(error));
        });
    }
    /**
     * Normalize Axios errors to ApiError format
     */
    normalizeError(error) {
        if (error.response) {
            const data = error.response.data;
            return new ApiError(data?.message || error.message, error.response.status, data);
        }
        else if (error.request) {
            return new ApiError('API server not reachable. Please check the API URL and network connection.', undefined, error.message);
        }
        else {
            return new ApiError(error.message);
        }
    }
    /**
     * GET request
     */
    async get(path) {
        const response = await this.client.get(path);
        return response.data;
    }
    /**
     * POST request
     */
    async post(path, data) {
        const response = await this.client.post(path, data);
        return response.data;
    }
    /**
     * PUT request
     */
    async put(path, data) {
        const response = await this.client.put(path, data);
        return response.data;
    }
    /**
     * PATCH request
     */
    async patch(path, data) {
        const response = await this.client.patch(path, data);
        return response.data;
    }
    /**
     * DELETE request
     */
    async delete(path, config) {
        const response = await this.client.delete(path, config);
        return response.data;
    }
    /**
     * Get base URL
     */
    getBaseUrl() {
        return this.baseUrl;
    }
    /**
     * Get available node sizes/server types for a provider
     */
    async getNodeSizes(provider, region, skipCache = false) {
        const params = new URLSearchParams();
        if (region) {
            params.append('region', region);
        }
        if (skipCache) {
            params.append('skipCache', 'true');
        }
        const queryString = params.toString();
        const qs = queryString ? `?${queryString}` : '';
        const path = `/management/providers/${provider}/node-sizes${qs}`;
        return this.get(path);
    }
    /**
     * Clear node sizes cache for a provider
     */
    async clearNodeSizesCache(provider) {
        const path = `/management/cache/providers/${provider}/node-sizes`;
        return this.delete(path);
    }
};
exports.ApiClient = ApiClient;
exports.ApiClient = ApiClient = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [Object])
], ApiClient);
