"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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const profile_router_1 = require("./profile-router");
const proxy_daemon_paths_1 = require("./proxy-daemon-paths");
const proxy_server_1 = require("./server/proxy-server");
const config_loader_facade_1 = require("../config/config-loader-facade");
function parseArgs(argv) {
    let port = proxy_daemon_paths_1.OPENAI_COMPAT_PROXY_DEFAULT_PORT;
    let host = '127.0.0.1';
    let profileName = '';
    let settingsPath = '';
    let authToken = '';
    let authTokenFile = '';
    let insecure = false;
    for (let i = 0; i < argv.length; i++) {
        const arg = argv[i];
        if (arg === '--port' && argv[i + 1]) {
            port = Number.parseInt(argv[++i] || '', 10) || port;
            continue;
        }
        if (arg === '--host' && argv[i + 1]) {
            host = argv[++i] || host;
            continue;
        }
        if (arg === '--profile' && argv[i + 1]) {
            profileName = argv[++i] || '';
            continue;
        }
        if (arg === '--settings-path' && argv[i + 1]) {
            settingsPath = argv[++i] || '';
            continue;
        }
        if (arg === '--auth-token' && argv[i + 1]) {
            authToken = argv[++i] || '';
            continue;
        }
        if (arg === '--auth-token-file' && argv[i + 1]) {
            authTokenFile = argv[++i] || '';
            continue;
        }
        if (arg === '--insecure') {
            insecure = true;
        }
    }
    return { port, host, profileName, settingsPath, authToken, authTokenFile, insecure };
}
function readAuthToken(options) {
    if (options.authTokenFile) {
        try {
            const token = fs.readFileSync(options.authTokenFile, 'utf8').trim();
            fs.unlinkSync(options.authTokenFile);
            return token;
        }
        catch (error) {
            throw new Error(`Failed to read local proxy auth token file: ${error.message}`);
        }
    }
    return options.authToken;
}
function startRuntime(options) {
    const authToken = readAuthToken(options);
    if (!authToken.trim()) {
        throw new Error('Missing local proxy auth token');
    }
    const settings = (0, config_loader_facade_1.loadSettings)(options.settingsPath);
    const profile = (0, profile_router_1.resolveOpenAICompatProfileConfig)(options.profileName, options.settingsPath, settings.env || {});
    if (!profile) {
        throw new Error(`Profile "${options.profileName}" is not an OpenAI-compatible settings profile`);
    }
    const server = (0, proxy_server_1.startOpenAICompatProxyServer)({
        profile,
        host: options.host,
        port: options.port,
        authToken,
        insecure: options.insecure,
    });
    server.once('error', (error) => {
        console.error(error.message);
        process.exit(1);
    });
    const shutdown = () => server.close();
    process.on('SIGTERM', shutdown);
    process.on('SIGINT', shutdown);
}
if (require.main === module) {
    startRuntime(parseArgs(process.argv.slice(2)));
}
//# sourceMappingURL=proxy-daemon-entry.js.map