"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const zod_to_json_schema_1 = __importDefault(require("zod-to-json-schema"));
// Add ts-node registration before dynamic imports
// eslint-disable-next-line @typescript-eslint/no-require-imports
require('ts-node').register({
    transpileOnly: true,
    compilerOptions: { module: 'commonjs' },
});
function isZodSchema(value) {
    return Boolean(value && typeof value.safeParse === 'function' && value._def);
}
async function getConfig(filePath) {
    try {
        // eslint-disable-next-line @typescript-eslint/no-require-imports
        const module = require(path_1.default.resolve(filePath));
        // Check if the specified function exists in the module
        if (!module.config) {
            throw new Error(`Config not found in module ${filePath}`);
        }
        if (isZodSchema(module.config.input)) {
            module.config.input = (0, zod_to_json_schema_1.default)(module.config.input);
        }
        else if (isZodSchema(module.config.bodySchema)) {
            module.config.bodySchema = (0, zod_to_json_schema_1.default)(module.config.bodySchema);
        }
        if (module.config.responseSchema) {
            for (const [status, schema] of Object.entries(module.config.responseSchema)) {
                if (isZodSchema(schema)) {
                    module.config.responseSchema[status] = (0, zod_to_json_schema_1.default)(schema);
                }
            }
        }
        if (isZodSchema(module.config.schema)) {
            module.config.schema = (0, zod_to_json_schema_1.default)(module.config.schema);
        }
        process.send?.(module.config);
        process.exit(0);
    }
    catch (error) {
        console.error('Error running TypeScript module:', error);
        process.exit(1);
    }
}
const [, , filePath] = process.argv;
if (!filePath) {
    console.error('Usage: node get-config.js <file-path>');
    process.exit(1);
}
getConfig(filePath).catch((err) => {
    console.error('Error:', err);
    process.exit(1);
});
