"use strict";
// src/orchestrator/deploy/DeployAdapter.ts
// v1.18.0 — Pluggable deploy adapter interface.
//
// Same pattern as HostAdapter (v1.17). Each provider implements the
// 4 lifecycle methods: detect/preflight/deploy/rollback.
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.DeployRegistry = void 0;
class AdapterRegistry {
    adapters = new Map();
    register(a) { this.adapters.set(a.name, a); }
    get(name) { return this.adapters.get(name) || null; }
    list() { return Array.from(this.adapters.keys()); }
    ensureDefaults() {
        // eslint-disable-next-line @typescript-eslint/no-var-requires
        const { VercelAdapter } = require('./VercelAdapter');
        // eslint-disable-next-line @typescript-eslint/no-var-requires
        const { RailwayAdapter } = require('./RailwayAdapter');
        // eslint-disable-next-line @typescript-eslint/no-var-requires
        const { CloudflareAdapter } = require('./CloudflareAdapter');
        // eslint-disable-next-line @typescript-eslint/no-var-requires
        const { FlyAdapter } = require('./FlyAdapter');
        if (!this.adapters.has('vercel'))
            this.register(new VercelAdapter());
        if (!this.adapters.has('railway'))
            this.register(new RailwayAdapter());
        if (!this.adapters.has('cloudflare'))
            this.register(new CloudflareAdapter());
        if (!this.adapters.has('fly'))
            this.register(new FlyAdapter());
    }
}
const registry = new AdapterRegistry();
exports.DeployRegistry = {
    register(a) { registry.register(a); },
    get(name) {
        registry.ensureDefaults();
        return registry.get(name);
    },
    list() {
        registry.ensureDefaults();
        return registry.list();
    },
    /** Auto-detect the right provider based on which CLIs are installed + project markers. */
    async autoDetect(cwd) {
        registry.ensureDefaults();
        const fs = await Promise.resolve().then(() => __importStar(require('fs')));
        const path = await Promise.resolve().then(() => __importStar(require('path')));
        const root = cwd || process.cwd();
        // Vercel marker first (most common in Rafael's setup).
        if (fs.existsSync(path.join(root, 'vercel.json')) || fs.existsSync(path.join(root, '.vercel'))) {
            const v = registry.get('vercel');
            if (v && await v.detect())
                return 'vercel';
        }
        if (fs.existsSync(path.join(root, 'railway.toml')) || fs.existsSync(path.join(root, 'railway.json'))) {
            const r = registry.get('railway');
            if (r && await r.detect())
                return 'railway';
        }
        if (fs.existsSync(path.join(root, 'wrangler.toml')) || fs.existsSync(path.join(root, 'wrangler.jsonc'))) {
            const c = registry.get('cloudflare');
            if (c && await c.detect())
                return 'cloudflare';
        }
        if (fs.existsSync(path.join(root, 'fly.toml'))) {
            const f = registry.get('fly');
            if (f && await f.detect())
                return 'fly';
        }
        // Probe by CLI presence as a fallback.
        for (const name of registry.list()) {
            const a = registry.get(name);
            if (a && await a.detect())
                return name;
        }
        return null;
    },
    /** Exposed for tests. */
    _resetForTests() {
        registry['adapters'].clear();
    },
};
