"use strict";
/**
 * OpenRouter inline flow: validate key → save → fetch models → add as
 * voices. The HTTP shim that actually dispatches chat completions to
 * OpenRouter lives in `src/daemon/agents/openrouter.ts`; these routes
 * own the catalogue + secrets-table side.
 */
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.registerOpenRouterRoutes = registerOpenRouterRoutes;
const openrouter = __importStar(require("../openrouter.js"));
const api_response_js_1 = require("../api-response.js");
function registerOpenRouterRoutes(fastify) {
    fastify.post('/openrouter/validate', async (request) => {
        try {
            const apiKey = request.body?.apiKey;
            if (typeof apiKey !== 'string') {
                return (0, api_response_js_1.errorResponse)('validation', 'apiKey (string) is required');
            }
            const result = await openrouter.validateKey(apiKey);
            return (0, api_response_js_1.successResponse)(result);
        }
        catch (error) {
            const message = error instanceof Error ? error.message : 'Unknown error';
            return (0, api_response_js_1.errorResponse)('openrouter_error', message);
        }
    });
    fastify.post('/openrouter/save-key', async (request) => {
        try {
            const apiKey = request.body?.apiKey;
            if (typeof apiKey !== 'string') {
                return (0, api_response_js_1.errorResponse)('validation', 'apiKey (string) is required');
            }
            const result = await openrouter.saveKey(apiKey);
            return (0, api_response_js_1.successResponse)(result);
        }
        catch (error) {
            const message = error instanceof Error ? error.message : 'Unknown error';
            return (0, api_response_js_1.errorResponse)('openrouter_error', message);
        }
    });
    fastify.get('/openrouter/models', async () => {
        try {
            const models = await openrouter.listModels();
            return (0, api_response_js_1.successResponse)({ models });
        }
        catch (error) {
            const message = error instanceof Error ? error.message : 'Unknown error';
            return (0, api_response_js_1.errorResponse)('openrouter_error', message);
        }
    });
    fastify.post('/openrouter/voices', async (request) => {
        try {
            const modelIds = request.body?.modelIds;
            if (!Array.isArray(modelIds) || !modelIds.every((s) => typeof s === 'string')) {
                return (0, api_response_js_1.errorResponse)('validation', 'modelIds (string[]) is required');
            }
            // Optional apiKey lets a caller bypass the secrets table — useful
            // for one-off automation that already holds the key, and avoids
            // racing a concurrent save-key write.
            const apiKey = typeof request.body?.apiKey === 'string' ? request.body.apiKey : undefined;
            const result = await openrouter.addModelsAsVoices(modelIds, apiKey);
            return (0, api_response_js_1.successResponse)(result);
        }
        catch (error) {
            const message = error instanceof Error ? error.message : 'Unknown error';
            return (0, api_response_js_1.errorResponse)('openrouter_error', message);
        }
    });
}
//# sourceMappingURL=openrouter.js.map