"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
Object.defineProperty(exports, "__esModule", { value: true });
exports.LEGACY_AGENT_API_SUNSET = exports.LEGACY_AGENT_API_BASE = exports.AGENT_API_V1_BASE = void 0;
exports.markLegacyApi = markLegacyApi;
exports.markLegacyAgentApi = markLegacyAgentApi;
exports.rejectLegacyAgentApi = rejectLegacyAgentApi;
const legacyApiTelemetry_1 = require("../services/legacyApiTelemetry");
exports.AGENT_API_V1_BASE = '/api/agent/v1';
exports.LEGACY_AGENT_API_BASE = '/api/agent';
exports.LEGACY_AGENT_API_SUNSET = 'Wed, 30 Jun 2027 00:00:00 GMT';
function markLegacyApi(successor, message) {
    return (req, res, next) => {
        (0, legacyApiTelemetry_1.recordLegacyApiUsage)(req);
        res.setHeader('Deprecation', 'true');
        res.setHeader('Sunset', exports.LEGACY_AGENT_API_SUNSET);
        res.setHeader('Link', `<${successor}>; rel="successor-version"`);
        res.setHeader('Warning', `299 - "${message}"`);
        next();
    };
}
function markLegacyAgentApi(req, res, next) {
    (0, legacyApiTelemetry_1.recordLegacyApiUsage)(req);
    res.setHeader('Deprecation', 'true');
    res.setHeader('Sunset', exports.LEGACY_AGENT_API_SUNSET);
    res.setHeader('Link', `<${exports.AGENT_API_V1_BASE}>; rel="successor-version"`);
    res.setHeader('Warning', '299 - "Legacy agent API is deprecated. Migrate to /api/agent/v1"');
    next();
}
function mapLegacyPathToSuccessor(req) {
    const fullPath = String(req.originalUrl || req.url || '').split('?')[0] || exports.LEGACY_AGENT_API_BASE;
    if (fullPath.startsWith('/api/agent/llm')) {
        return `${exports.AGENT_API_V1_BASE}/analyze`;
    }
    if (fullPath.startsWith(exports.LEGACY_AGENT_API_BASE)) {
        return `${exports.AGENT_API_V1_BASE}${fullPath.slice(exports.LEGACY_AGENT_API_BASE.length)}`;
    }
    return exports.AGENT_API_V1_BASE;
}
function rejectLegacyAgentApi(req, res, next) {
    if (req.path === '/v1' || req.path.startsWith('/v1/')) {
        next();
        return;
    }
    (0, legacyApiTelemetry_1.recordLegacyApiUsage)(req);
    const successorPath = mapLegacyPathToSuccessor(req);
    res.setHeader('Deprecation', 'true');
    res.setHeader('Sunset', exports.LEGACY_AGENT_API_SUNSET);
    res.setHeader('Link', `<${exports.AGENT_API_V1_BASE}>; rel="successor-version"`);
    res.setHeader('Warning', '299 - "Legacy agent API has been removed. Use /api/agent/v1"');
    res.status(410).json({
        success: false,
        error: 'Legacy agent API has been removed',
        message: `Please migrate this request to ${successorPath}`,
        migration: {
            successor: successorPath,
            root: exports.AGENT_API_V1_BASE,
            analyze: `${exports.AGENT_API_V1_BASE}/analyze`,
        },
    });
}
//# sourceMappingURL=legacyAgentApi.js.map