import { writeFileSync } from "node:fs";
import { readFile } from "node:fs/promises";
import { join, relative } from "node:path";
import { format } from "prettier";
import { logger } from "../common/logger.js";
import { printCriticalFailureToConsoleAndExit } from "../common/output.js";
import { convertPolicies, convertRoutes } from "./engine.js";
export async function convert(argv) {
    try {
        const dir = argv.dir;
        const normalizedDir = join(relative(process.cwd(), dir));
        const rawRoutes = await readFile(join(normalizedDir, "config", "routes.json"));
        const routes = JSON.parse(rawRoutes.toString());
        const openApi = convertRoutes(routes);
        const openAPIFilePath = join(normalizedDir, "config", "routes.oas.json");
        const formattedOpenAPI = await format(JSON.stringify(openApi), {
            parser: "json-stringify",
        });
        writeFileSync(openAPIFilePath, formattedOpenAPI, {
            flag: "w",
        });
        const policies = convertPolicies(routes);
        const policiesFilePath = join(normalizedDir, "config", "policies.json");
        const formattedPolicies = await format(JSON.stringify(policies), {
            parser: "json-stringify",
        });
        writeFileSync(policiesFilePath, formattedPolicies, {
            flag: "w",
        });
    }
    catch (err) {
        logger.error(err);
        await printCriticalFailureToConsoleAndExit(err);
    }
}
//# sourceMappingURL=handler.js.map