"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNextDateWithinBusinessHours = exports.isDateBetweenBusinessHours = void 0;
const date_fns_1 = require("date-fns");
const date_fns_tz_1 = require("date-fns-tz");
const START_HOUR_WORKING_DAY = 9; // 9 AM
const END_HOUR_WORKING_DAY = 17; // 5 PM
const isDateBetweenBusinessHours = (date, // assuming all dates we receive are dates in UTC
targetTimeZone = 'UTC') => {
    const referenceDate = (0, date_fns_tz_1.utcToZonedTime)(date, targetTimeZone);
    const hoursOfDate = referenceDate.getHours();
    if (hoursOfDate >= START_HOUR_WORKING_DAY &&
        hoursOfDate < END_HOUR_WORKING_DAY) {
        return true;
    }
    return false;
};
exports.isDateBetweenBusinessHours = isDateBetweenBusinessHours;
/**
 * Adjusts the given ISO date string to the next available time within business hours,
 * taking into account the specified time zone.
 *
 * @param date - The date to adjust.
 * @param targetTimeZone - The IANA time zone identifier string, such as "America/New_York".
 * @returns The next date between business hours in UTC
 */
const getNextDateWithinBusinessHours = (date, // assuming all dates we receive are dates in UTC
targetTimeZone = 'UTC') => {
    // Convert date to the specified timezone
    const referenceDateInTimeZone = (0, date_fns_tz_1.utcToZonedTime)(date, targetTimeZone);
    const hours = referenceDateInTimeZone.getHours();
    // If before 9 AM in the specified timezone, adjust to 9 AM of the same day in that timezone, then convert back to UTC
    if (hours < START_HOUR_WORKING_DAY) {
        return (0, date_fns_tz_1.zonedTimeToUtc)((0, date_fns_1.setHours)((0, date_fns_1.startOfDay)(referenceDateInTimeZone), START_HOUR_WORKING_DAY), targetTimeZone);
    }
    // If after 5 PM in the specified timezone, adjust to 9 AM of the next day in that timezone, then convert back to UTC
    else if (hours >= END_HOUR_WORKING_DAY) {
        return (0, date_fns_tz_1.zonedTimeToUtc)((0, date_fns_1.setHours)((0, date_fns_1.startOfDay)((0, date_fns_1.addDays)(referenceDateInTimeZone, 1)), START_HOUR_WORKING_DAY), targetTimeZone);
    }
    // If between 9 AM and 5 PM in the specified timezone, return the reference date itself, already in UTC
    return date;
};
exports.getNextDateWithinBusinessHours = getNextDateWithinBusinessHours;
//# sourceMappingURL=getNextDateWithinBusinessHours.js.map