"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.testInterval = exports.getTimeParts = exports.tzIdentifierToOffset = void 0;
const DELIM = '_';
const DAYS_DELIM = ',';
const TIME_DELIM = ':';
/**
 * Return the offset in minutes between the timezone and UTC
 *
 * @param tzIdentifier International timezone identifier
 * @returns the diff in minutes between the timezone and UTC
 */
function tzIdentifierToOffset(tzIdentifier) {
    if (!tzIdentifier) {
        // TODO: Maybe fallback to worker tz.
        return null;
    }
    const options = {
        timeZone: tzIdentifier,
        timeZoneName: 'shortOffset',
    };
    const dateParts = new Intl.DateTimeFormat('en-US', options).formatToParts(new Date());
    const offsetString = dateParts.find(part => part.type === 'timeZoneName')?.value;
    if (offsetString) {
        const offset = parseInt(offsetString.replace(/[a-z+]/gi, ''));
        return offset * 60;
    }
    return null;
}
exports.tzIdentifierToOffset = tzIdentifierToOffset;
/**
 * returning like  https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/formatToParts
 *
 * @param currentTimestamp UTC timestamp in milliseconds
 * @param timzone Time zone offset in seconds
 * @returns Return an array of objet with the parts of the time
 */
function getTimeParts(currentTimestamp, timezone) {
    const options = {
        timeZone: timezone,
        weekday: 'long',
        hour: 'numeric',
        minute: 'numeric',
        hour12: false,
    };
    const currentDateParts = Intl.DateTimeFormat('en-US', options).formatToParts(new Date(currentTimestamp));
    const result = {};
    currentDateParts
        .filter(val => val.type !== 'literal')
        .forEach(val => {
        result[val.type] = val.value.toLowerCase();
    });
    return result;
}
exports.getTimeParts = getTimeParts;
/**
 * Deserializes a time interval
 *
 * @param serialized {string} ex: '10:00_15:00_monday,tuesday,wednesday'
 * @return { { start_time: string, end_time: string, days: Array } }
 * @private
 */
function deserialize(serialized) {
    var parts = serialized.split(DELIM);
    if (parts.length !== 3) {
        throw new Error('Invalid time and day string ' + serialized);
    }
    var days = parts[2].split(DAYS_DELIM);
    return {
        start_time: parts[0],
        end_time: parts[1],
        days: days,
    };
}
/**
 * Takes a 24h time string 'HH:MM' and converts it to minutes
 *
 * @param {string} timeStr
 * @return {number}
 * @private
 */
function timeStringToMinutes(timeStr) {
    var parts = timeStr.split(TIME_DELIM);
    if (parts.length !== 2) {
        throw new Error('optly.timeAndDayInterval.timeStringToMinutes: Invalid time string ' +
            timeStr);
    }
    return parseInt(parts[0], 10) * 60 + parseInt(parts[1], 10);
}
function testInterval(intervalStr, timeParts) {
    var interval = deserialize(intervalStr);
    var startTime = timeStringToMinutes(interval.start_time);
    var endTime = timeStringToMinutes(interval.end_time);
    var currentTime = parseInt(timeParts['hour']) * 60 + parseInt(timeParts['minute']);
    var currentDay = timeParts['weekday'];
    return (currentTime >= startTime &&
        currentTime <= endTime &&
        interval.days.includes(currentDay));
}
exports.testInterval = testInterval;
exports.default = {
    tzIdentifierToOffset,
    getTimeParts,
    testInterval,
};
