"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.cronSchedulesEqual = cronSchedulesEqual;
exports.cronScheduleToString = cronScheduleToString;
exports.actionScheduleToDescriptiveString = actionScheduleToDescriptiveString;
exports.toScheduleInput = toScheduleInput;
exports.toCronSchedule = toCronSchedule;
const date_1 = require("./date");
function cronSchedulesEqual(a, b) {
    for (const propName of [
        'second',
        'minute',
        'hour',
        'dayOfMonth',
        'month',
        'dayOfWeek',
        'timeZoneName',
        'runnerId',
    ]) {
        if (a[propName] !== b[propName])
            return false;
    }
    return true;
}
function cronScheduleToString(schedule) {
    return [
        schedule.second,
        schedule.minute,
        schedule.hour,
        schedule.dayOfMonth,
        schedule.month,
        schedule.dayOfWeek,
    ].join(' ');
}
function actionScheduleToDescriptiveString(schedule) {
    const { minute, hour, dayOfMonth, dayOfWeek, timeZoneName } = schedule;
    const timeString = (0, date_1.timeToDisplayString)(Number(hour), Number(minute), timeZoneName);
    if (dayOfMonth !== '*') {
        return `every month on the ${(0, date_1.numberWithOrdinal)(Number(dayOfMonth))} at ${timeString}`;
    }
    if (dayOfWeek !== '*') {
        return `every week on ${date_1.DAY_NAMES[dayOfWeek]} at ${timeString}`;
    }
    if (hour !== '*') {
        return `every day at ${timeString}`;
    }
    return `every hour`;
}
/**
 * This assumes well-formed simple cron schedules generated by toCronSchedule below.
 */
function toScheduleInput(schedule) {
    const hours = Number(schedule.hour);
    const minutes = Number(schedule.minute);
    const dayOfWeek = Number(schedule.dayOfWeek);
    const dayOfMonth = Number(schedule.dayOfMonth);
    const schedulePeriod = !Number.isNaN(dayOfMonth)
        ? 'month'
        : !Number.isNaN(dayOfWeek)
            ? 'week'
            : !Number.isNaN(hours)
                ? 'day'
                : 'hour';
    const common = {
        schedulePeriod,
        timeZoneName: schedule.timeZoneName,
        runnerId: schedule.runnerId,
        notifyOnSuccess: schedule.notifyOnSuccess,
    };
    switch (schedulePeriod) {
        case 'hour':
            return common;
        case 'day':
            return {
                ...common,
                minutes,
                hours,
            };
        case 'week':
            return {
                ...common,
                minutes,
                hours,
                dayOfWeek,
            };
        case 'month':
            return {
                ...common,
                minutes,
                hours,
                dayOfMonth,
            };
    }
}
function toCronSchedule({ schedulePeriod, timeZoneName = 'UTC', hours, minutes, dayOfWeek, dayOfMonth, runnerId, notifyOnSuccess, }) {
    switch (schedulePeriod) {
        case undefined:
            return undefined;
        case 'hour': {
            return {
                second: '0',
                minute: '0',
                hour: '*',
                dayOfMonth: '*',
                month: '*',
                dayOfWeek: '*',
                timeZoneName,
                runnerId,
                notifyOnSuccess,
            };
        }
        case 'day': {
            return {
                second: '0',
                minute: minutes?.toString() ?? '0',
                hour: hours?.toString() ?? '0',
                dayOfMonth: '*',
                month: '*',
                dayOfWeek: '*',
                timeZoneName,
                runnerId,
                notifyOnSuccess,
            };
        }
        case 'week': {
            return {
                second: '0',
                minute: minutes?.toString() ?? '0',
                hour: hours?.toString() ?? '0',
                dayOfMonth: '*',
                month: '*',
                dayOfWeek: dayOfWeek?.toString() ?? '0',
                timeZoneName,
                runnerId,
                notifyOnSuccess,
            };
        }
        case 'month': {
            return {
                second: '0',
                minute: minutes?.toString() ?? '0',
                hour: hours?.toString() ?? '0',
                dayOfMonth: dayOfMonth?.toString() ?? '0',
                month: '*',
                dayOfWeek: '*',
                timeZoneName,
                runnerId,
                notifyOnSuccess,
            };
        }
    }
}
