"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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
var ScalewayBareMetalAdapter_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScalewayBareMetalAdapter = exports.BAREMETAL_ZONES = void 0;
const common_1 = require("@nestjs/common");
const axios_1 = __importDefault(require("axios"));
const baremetal_1 = require("./generated/baremetal");
exports.BAREMETAL_ZONES = [
    baremetal_1.CreateServerZoneEnum.FrPar1,
    baremetal_1.CreateServerZoneEnum.FrPar2,
    baremetal_1.CreateServerZoneEnum.NlAms1,
    baremetal_1.CreateServerZoneEnum.NlAms2,
    baremetal_1.CreateServerZoneEnum.PlWaw2,
    baremetal_1.CreateServerZoneEnum.PlWaw3,
];
let ScalewayBareMetalAdapter = ScalewayBareMetalAdapter_1 = class ScalewayBareMetalAdapter {
    constructor() {
        this.logger = new common_1.Logger(ScalewayBareMetalAdapter_1.name);
        this.baseUrl = 'https://api.scaleway.com';
        this.timeout = 30000;
    }
    createAxiosInstance() {
        return axios_1.default.create({
            timeout: this.timeout,
            headers: {
                Accept: 'application/json',
                'Content-Type': 'application/json',
                'User-Agent': 'Flui-Cloud-API/1.0',
            },
            validateStatus: (status) => status >= 200 && status < 300,
        });
    }
    createConfiguration(token) {
        return new baremetal_1.Configuration({
            basePath: this.baseUrl,
            baseOptions: {
                headers: {
                    'X-Auth-Token': token,
                    'User-Agent': 'Flui-Cloud-API/1.0',
                    'Content-Type': 'application/json',
                    Accept: 'application/json',
                },
            },
        });
    }
    createServersApi(token) {
        return new baremetal_1.ServersApi(this.createConfiguration(token), this.baseUrl, this.createAxiosInstance());
    }
    createServerActionsApi(token) {
        return new baremetal_1.ServerActionsApi(this.createConfiguration(token), this.baseUrl, this.createAxiosInstance());
    }
    createOffersApi(token) {
        return new baremetal_1.OffersApi(this.createConfiguration(token), this.baseUrl, this.createAxiosInstance());
    }
    async listServers(token, zone) {
        const api = this.createServersApi(token);
        const allServers = [];
        let page = 1;
        const pageSize = 50;
        while (true) {
            const response = await api.listServers(zone, page, pageSize);
            const servers = response.data.servers || [];
            allServers.push(...servers);
            const totalCount = response.data.total_count || 0;
            if (allServers.length >= totalCount || servers.length < pageSize) {
                break;
            }
            page++;
        }
        return allServers;
    }
    async getServer(token, zone, serverId) {
        const api = this.createServersApi(token);
        const response = await api.getServer(zone, serverId);
        return response.data || null;
    }
    async createServer(token, zone, payload) {
        const api = this.createServersApi(token);
        const response = await api.createServer(zone, payload);
        return response.data;
    }
    async installServer(token, zone, serverId, payload) {
        const api = this.createServersApi(token);
        const response = await api.installServer(zone, serverId, payload);
        return response.data;
    }
    async deleteServer(token, zone, serverId) {
        const api = this.createServersApi(token);
        await api.deleteServer(zone, serverId);
    }
    async startServer(token, zone, serverId) {
        const api = this.createServerActionsApi(token);
        const request = { boot_type: 'normal' };
        await api.startServer(zone, serverId, request);
    }
    async stopServer(token, zone, serverId) {
        const api = this.createServerActionsApi(token);
        await api.stopServer(zone, serverId, {});
    }
    async rebootServer(token, zone, serverId) {
        const api = this.createServerActionsApi(token);
        const request = { boot_type: 'normal' };
        await api.rebootServer(zone, serverId, request);
    }
    async updateServerTags(token, zone, serverId, tags) {
        const api = this.createServersApi(token);
        const request = { tags };
        await api.updateServer(zone, serverId, request);
    }
    async listOffers(token, zone) {
        const api = this.createOffersApi(token);
        const allOffers = [];
        let page = 1;
        const pageSize = 50;
        while (true) {
            const response = await api.listOffers(zone, page, pageSize);
            const offers = response.data.offers || [];
            allOffers.push(...offers);
            const totalCount = response.data.total_count || 0;
            if (allOffers.length >= totalCount || offers.length < pageSize) {
                break;
            }
            page++;
        }
        return allOffers;
    }
};
exports.ScalewayBareMetalAdapter = ScalewayBareMetalAdapter;
exports.ScalewayBareMetalAdapter = ScalewayBareMetalAdapter = ScalewayBareMetalAdapter_1 = __decorate([
    (0, common_1.Injectable)()
], ScalewayBareMetalAdapter);
