export var ITimeSpan;
(function (ITimeSpan) {
    function parse(value, locale) {
        locale ??= 'default'; // Default to 'default' locale
        const [hours, minutes] = value.split(':').map((x) => parseInt(x, 10));
        return {
            hours,
            minutes,
            toString: () => value
        };
    }
    ITimeSpan.parse = parse;
    function tryParse(value, locale) {
        try {
            return parse(value, locale);
        }
        catch {
            return null;
        }
    }
    ITimeSpan.tryParse = tryParse;
    function compare(x, y) {
        if (x === y) {
            return 0;
        }
        if (x === null || x === undefined) {
            return -1;
        }
        if (y === null || y === undefined) {
            return 1;
        }
        if (x.hours < y.hours) {
            return -1;
        }
        if (x.hours > y.hours) {
            return 1;
        }
        if (x.minutes < y.minutes) {
            return -1;
        }
        if (x.minutes > y.minutes) {
            return 1;
        }
        return 0;
    }
    ITimeSpan.compare = compare;
    function equals(x, y) {
        return compare(x, y) === 0;
    }
    ITimeSpan.equals = equals;
    function repeat(from, to, interval) {
        const fromMinutes = (from.hours * 60) + from.minutes;
        // eslint-disable-next-line @typescript-eslint/no-shadow
        const toMinutes = (to.hours * 60) + to.minutes;
        const intervalMinutes = interval;
        // eslint-disable-next-line no-unreachable-loop
        for (let minute = fromMinutes; minute <= toMinutes; minute += intervalMinutes) {
            const hours = Math.floor(minute / 60);
            const minutes = minute % 60;
            const timeSpan = {
                hours,
                minutes,
                toString: () => `${hours}:${minutes}`
            };
            return timeSpan;
        }
        return from;
    }
    ITimeSpan.repeat = repeat;
    function create(hours, minutes, locale) {
        locale ??= 'default'; // Default to 'default' locale
        return {
            hours,
            minutes,
            toString: () => formatTime({
                hours,
                minutes
            }, locale)
        };
    }
    ITimeSpan.create = create;
    /**
     * Type-guard to check if the given value is an ITimeSpan object.
     *
     * @param value - The value to check.
     * @returns
     */
    function isTimeSpan(value) {
        return (typeof value === 'object' &&
            value !== null &&
            'hours' in value &&
            'minutes' in value &&
            typeof value.hours === 'number' &&
            typeof value.minutes === 'number');
    }
    ITimeSpan.isTimeSpan = isTimeSpan;
    /**
     * Calculates the time span (hours and minutes) between two Date objects.
     *
     * @public
     * @param start - The start Date.
     * @param end - The end Date.
     * @param locale - Optional locale string for formatting.
     * @returns An ITimeSpan object representing the difference.
     */
    function fromDates(start, end, locale) {
        if (!(start instanceof Date) || !(end instanceof Date)) {
            throw new TypeError('Both start and end must be valid Date objects.');
        }
        const diffMs = Math.max(0, end.getTime() - start.getTime()); // Prevent negative span
        const totalMinutes = Math.floor(diffMs / 60000); // Convert ms to minutes
        const hours = Math.floor(totalMinutes / 60);
        const minutes = totalMinutes % 60;
        return create(hours, minutes, locale);
    }
    ITimeSpan.fromDates = fromDates;
    /**
     * Converts an ITimeSpan object to a total number of minutes.
     *
     * @public
     * @param span - The ITimeSpan object to convert.
     */
    function toMinutes(span) {
        return (span.hours * 60) + span.minutes;
    }
    ITimeSpan.toMinutes = toMinutes;
})(ITimeSpan || (ITimeSpan = {}));
/**
 * Generates time slots.
 *
 * @private
 */
export function generateTimeSlots(interval, locale, min, max) {
    interval ??= 60; // Default to 60 minutes
    locale ??= 'default'; // Default to 'default' locale
    min ??= {
        hours: 0,
        minutes: 0
    }; // Default to 00:00
    max ??= {
        hours: 23,
        minutes: 59
    }; // Default to 23:59
    const startTime = (min.hours * 60) + min.minutes; // Convert start time to minutes
    const endTime = (max.hours * 60) + max.minutes; // Convert end time to minutes
    const times = [];
    for (let minute = startTime; minute <= endTime; minute += interval) {
        const hours = Math.floor(minute / 60);
        const minutes = minute % 60;
        const timeSpan = {
            hours,
            minutes,
            // eslint-disable-next-line func-names
            toString: function () {
                return formatTime(this, locale);
            }
        };
        times.push(timeSpan);
    }
    return times;
}
function formatTime(time, locale) {
    // Create a Date object at the base date (e.g., 2000-01-01) with the specific hours and minutes
    const date = new Date(2000, 0, 1, time.hours, time.minutes);
    // Use Intl.DateTimeFormat to format the time according to the locale and automatically detect 12 or 24-hour format
    const formatter = new Intl.DateTimeFormat(locale, {
        hour: 'numeric',
        minute: 'numeric',
        hour12: undefined // Let the locale decide the hour12 setting
    });
    try {
        return formatter.format(date);
    }
    catch (_) {
        return '';
    }
}
//# sourceMappingURL=TimeSpan.js.map