"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.detectedJobsToRouteScheduleManifest = exports.RouteScheduleManifestSchema = exports.detectCron = exports.formatDetectedJobsAsTable = void 0;
const cron_detector_1 = require("../cron-detector");
const z = __importStar(require("zod"));
const index_1 = require("../../client/index");
const beatiful_json_1 = require("../beatiful-json");
const easy_table_1 = __importDefault(require("easy-table"));
function formatDetectedJobsAsTable(jobs) {
    return easy_table_1.default.print(jobs.map((j) => ({
        Route: j.route,
        Schedule: j.isValid ? j.schedule : j.schedule + " (invalid, skipping)",
        Timezone: j.timezone
    })));
}
exports.formatDetectedJobsAsTable = formatDetectedJobsAsTable;
async function detectCron(cwd) {
    const detector = new cron_detector_1.CronDetector(cwd);
    await detector.awaitReady();
    const jobs = detector.getDetectedJobs();
    await detector.close();
    return jobs;
}
exports.detectCron = detectCron;
exports.RouteScheduleManifestSchema = z.array(z.object({ route: z.string(), schedule: index_1.cronExpression, timezone: z.string() }));
function detectedJobsToRouteScheduleManifest(jobs) {
    return jobs
        .filter((j) => j.isValid)
        .map((j) => ({ route: j.route, schedule: j.schedule, timezone: j.timezone }));
}
exports.detectedJobsToRouteScheduleManifest = detectedJobsToRouteScheduleManifest;
function registerDetectCron(program) {
    program
        .command("detect-cron [cwd]")
        .description("Detects cron jobs.")
        .option("--json", "Output as JSON instead of a human-readable table.", false)
        .action(async (cwd = process.cwd(), { json }) => {
        const jobs = await detectCron(cwd);
        if (json) {
            console.log((0, beatiful_json_1.stringifyBeautiful)(detectedJobsToRouteScheduleManifest(jobs)));
        }
        else {
            console.log(formatDetectedJobsAsTable(jobs));
        }
    });
}
exports.default = registerDetectCron;
//# sourceMappingURL=detect-cron.js.map