import { join } from "node:path";
export function convertRoutes(routesConfig) {
    const base = generateBaseOpenAPIConfig();
    const paths = base.paths;
    routesConfig.routes.forEach((route) => {
        const pathObjectKey = fullPath(route, routesConfig);
        let pathObject;
        if (!paths[pathObjectKey]) {
            pathObject = {};
            pathObject["x-zuplo-path"] = {
                pathMode: "url-pattern",
            };
            paths[pathObjectKey] = pathObject;
        }
        pathObject = paths[pathObjectKey];
        const operationKey = route.methods.map((m) => m.toLowerCase()).join(",");
        const operation = {};
        if (route.excludeFromOpenApi) {
            operation["x-internal"] = true;
        }
        operation["summary"] = route.summary;
        operation["description"] = route.description;
        operation["operationId"] = route.operationId;
        const routeWithOpenApi = route;
        if (routeWithOpenApi.parameters) {
            operation["parameters"] = routeWithOpenApi.parameters;
        }
        if (routeWithOpenApi.responses) {
            operation["responses"] = routeWithOpenApi.responses;
        }
        if (routeWithOpenApi.tags) {
            operation["tags"] = routeWithOpenApi.tags;
        }
        const xZuploRoute = {};
        if (route.corsPolicy)
            xZuploRoute["corsPolicy"] = route.corsPolicy;
        if (route.custom)
            xZuploRoute["custom"] = route.custom;
        if (route.policies)
            xZuploRoute["policies"] = route.policies;
        if (route.key)
            xZuploRoute["key"] = route.key;
        if (route.label)
            xZuploRoute["label"] = route.label;
        xZuploRoute["handler"] = route.handler;
        operation["x-zuplo-route"] = xZuploRoute;
        pathObject[operationKey] = operation;
    });
    return base;
}
export function convertPolicies(routes) {
    const policies = routes.policies;
    const corsPolicies = routes.corsPolicies;
    return {
        policies: policies ? [...policies] : [],
        corsPolicies: corsPolicies ? [...corsPolicies] : [],
    };
}
function generateBaseOpenAPIConfig() {
    return {
        openapi: "3.1.0",
        info: {
            title: "Converted from config/routes.json",
            version: "1.0.0",
        },
        paths: {},
    };
}
function fullPath(path, context) {
    const versionPrefix = context.versions.filter((v) => v.name === path.version)[0].pathPrefix;
    return join(versionPrefix, path.path);
}
//# sourceMappingURL=engine.js.map