"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 __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;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateScalewayCredentials = validateScalewayCredentials;
/**
 * Local Scaleway credential validator.
 *
 * Used by the CLI before storing credentials so the user gets immediate
 * feedback. Mirrors the probe used by ScalewayCapabilitiesService.validateCredentials:
 * call IAM listSSHKeys with X-Auth-Token. Anything other than 401/403 is treated
 * as a transient error.
 */
const https = __importStar(require("node:https"));
async function validateScalewayCredentials(accessKey, secretKey) {
    if (!accessKey || !secretKey) {
        return {
            success: false,
            message: 'Both Access Key ID and Secret Key are required',
        };
    }
    return new Promise((resolve) => {
        const req = https.request({
            hostname: 'api.scaleway.com',
            path: '/iam/v1alpha1/ssh-keys?page_size=1',
            method: 'GET',
            headers: { 'X-Auth-Token': secretKey },
            timeout: 10_000,
        }, (res) => {
            res.resume();
            const status = res.statusCode ?? 0;
            if (status >= 200 && status < 300) {
                resolve({ success: true, message: 'Credentials are valid' });
                return;
            }
            if (status === 401 || status === 403) {
                resolve({ success: false, message: 'Invalid Secret Key' });
                return;
            }
            resolve({
                success: false,
                message: `Scaleway IAM probe returned HTTP ${status}`,
            });
        });
        req.on('timeout', () => {
            req.destroy(new Error('timeout'));
        });
        req.on('error', (err) => {
            resolve({
                success: false,
                message: `Validation failed: ${err.message}`,
            });
        });
        req.end();
    });
}
