"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.every = exports.cron = exports.parseTimezonedCron = exports.embedTimezone = exports.isValidTimezone = void 0;
const cron_parser_1 = __importDefault(require("cron-parser"));
function isValidTimezone(tz) {
    try {
        const expr = cron_parser_1.default.parseExpression("* * * * *", {
            tz,
        });
        expr.next();
        return true;
    }
    catch (error) {
        return false;
    }
}
exports.isValidTimezone = isValidTimezone;
function embedTimezone(cronExpression, tz) {
    if (!isValidTimezone(tz)) {
        throw new Error("Invalid timezone " + tz);
    }
    return cronExpression + ";" + tz;
}
exports.embedTimezone = embedTimezone;
function parseTimezonedCron(cronExpression) {
    const [cron, tz = "Etc/UTC"] = cronExpression.split(";");
    return [cron, tz];
}
exports.parseTimezonedCron = parseTimezonedCron;
function cron(lastDate, cronExpression) {
    const [cron, tz] = parseTimezonedCron(cronExpression);
    const expr = cron_parser_1.default.parseExpression(cron, {
        currentDate: lastDate,
        tz,
    });
    const nextExecution = expr.next().toDate();
    return nextExecution;
}
exports.cron = cron;
function every(lastDate, scheduleMeta) {
    return new Date(+lastDate + +scheduleMeta);
}
exports.every = every;
//# sourceMappingURL=repeat.js.map