"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const ai_providers_1 = require("../../cliproxy/ai-providers");
const auth_middleware_1 = require("../middleware/auth-middleware");
const router = (0, express_1.Router)();
router.use((req, res, next) => {
    if ((0, auth_middleware_1.requireLocalAccessWhenAuthDisabled)(req, res, 'AI provider endpoints require localhost access when dashboard auth is disabled.')) {
        next();
    }
});
function isAiProviderFamilyId(value) {
    return ai_providers_1.AI_PROVIDER_FAMILY_IDS.includes(value);
}
function parseFamily(req, res) {
    const family = req.params.family?.trim();
    if (!family || !isAiProviderFamilyId(family)) {
        res.status(400).json({ error: 'Invalid AI provider family' });
        return null;
    }
    return family;
}
function parseEntryId(req, res) {
    const entryId = req.params.entryId?.trim();
    if (!entryId) {
        res.status(400).json({ error: 'Invalid entry id' });
        return null;
    }
    return entryId;
}
function parseInput(body) {
    const payload = typeof body === 'object' && body !== null ? body : {};
    return {
        name: typeof payload.name === 'string' ? payload.name : undefined,
        baseUrl: typeof payload.baseUrl === 'string' ? payload.baseUrl : undefined,
        proxyUrl: typeof payload.proxyUrl === 'string' ? payload.proxyUrl : undefined,
        prefix: typeof payload.prefix === 'string' ? payload.prefix : undefined,
        headers: Array.isArray(payload.headers)
            ? payload.headers
                .filter((item) => typeof item === 'object' && item !== null)
                .map((item) => ({
                key: typeof item.key === 'string' ? item.key : '',
                value: typeof item.value === 'string' ? item.value : '',
            }))
            : undefined,
        excludedModels: Array.isArray(payload.excludedModels)
            ? payload.excludedModels.filter((item) => typeof item === 'string')
            : undefined,
        models: Array.isArray(payload.models)
            ? payload.models
                .filter((item) => typeof item === 'object' && item !== null)
                .map((item) => ({
                name: typeof item.name === 'string' ? item.name : '',
                alias: typeof item.alias === 'string' ? item.alias : '',
            }))
            : undefined,
        apiKey: typeof payload.apiKey === 'string' ? payload.apiKey : undefined,
        apiKeys: Array.isArray(payload.apiKeys)
            ? payload.apiKeys.filter((item) => typeof item === 'string')
            : undefined,
        preserveSecrets: payload.preserveSecrets === true,
    };
}
router.get('/', async (_req, res) => {
    try {
        res.json(await (0, ai_providers_1.listAiProviders)());
    }
    catch (error) {
        res.status(500).json({ error: error.message });
    }
});
router.post('/:family', async (req, res) => {
    const family = parseFamily(req, res);
    if (!family)
        return;
    try {
        await (0, ai_providers_1.createAiProviderEntry)(family, parseInput(req.body));
        res.status(201).json({ success: true });
    }
    catch (error) {
        const message = error.message;
        res.status(message === 'Entry not found' ? 404 : 400).json({ error: message });
    }
});
router.put('/:family/:entryId', async (req, res) => {
    const family = parseFamily(req, res);
    if (!family)
        return;
    const entryId = parseEntryId(req, res);
    if (!entryId)
        return;
    try {
        await (0, ai_providers_1.updateAiProviderEntry)(family, entryId, parseInput(req.body));
        res.json({ success: true });
    }
    catch (error) {
        const message = error.message;
        res.status(message === 'Entry not found' ? 404 : 400).json({ error: message });
    }
});
router.delete('/:family/:entryId', async (req, res) => {
    const family = parseFamily(req, res);
    if (!family)
        return;
    const entryId = parseEntryId(req, res);
    if (!entryId)
        return;
    try {
        await (0, ai_providers_1.deleteAiProviderEntry)(family, entryId);
        res.json({ success: true });
    }
    catch (error) {
        const message = error.message;
        res.status(message === 'Entry not found' ? 404 : 400).json({ error: message });
    }
});
exports.default = router;
//# sourceMappingURL=ai-provider-routes.js.map