// #region Imports
import { tryGet } from '../Extensions/Extensions';
// #endregion
/**
 * A utility class for formatting times using format strings.
 *
 * Supported time format specifiers:
 * - `h`   : Hour (1-12) in 12-hour format without leading zero.
 * - `hh`  : Hour (01-12) in 12-hour format with leading zero.
 * - `H`   : Hour (0-23) in 24-hour format without leading zero.
 * - `HH`  : Hour (00-23) in 24-hour format with leading zero.
 * - `m`   : Minute (0-59) without leading zero.
 * - `mm`  : Minute (00-59) with leading zero.
 * - `s`   : Second (0-59) without leading zero.
 * - `ss`  : Second (00-59) with leading zero.
 * - `t`   : First character of the AM/PM designator (e.g., A or P).
 * - `tt`  : Full AM/PM designator (e.g., AM or PM).
 * - `:`   : Time separator (e.g., colon).
 * - `-`   : Custom separator (e.g., hyphen).
 *
 * @public
 */
export class TimeFormatter {
    static _validSpecifiers = [
        'h', 'hh', 'H', 'HH', 'm', 'mm', 's', 'ss', 't', 'tt', ':', '-'
    ];
    static _formatMapping = {
        h: {
            hour: 'numeric',
            hour12: true
        },
        hh: {
            hour: '2-digit',
            hour12: true
        },
        H: {
            hour: 'numeric',
            hour12: false
        },
        HH: {
            hour: '2-digit',
            hour12: false
        },
        m: { minute: 'numeric' },
        mm: { minute: '2-digit' },
        s: { second: 'numeric' },
        ss: { second: '2-digit' },
        t: {}, // AM/PM not directly supported
        tt: { hour12: true }
    };
    /**
     * Formats a time using a time-specific format string.
     *
     * @param date The date object containing the time to format.
     * @param format The format string.
     * @param locale The locale for Intl formatting (default: 'en-US').
     * @returns The formatted time string.
     */
    static format(date, format, locale = 'en-US') {
        const segments = format.match(/h{1,2}|H{1,2}|m{1,2}|s{1,2}|t{1,2}|[-: ]+/g);
        if (!segments) {
            throw new Error(`Unable to parse format string: "${format}".`);
        }
        const result = segments
            .map((segment) => {
            const intlOptions = tryGet(this._formatMapping, segment);
            if (intlOptions) {
                const formatted = new Intl.DateTimeFormat(locale, intlOptions)
                    .format(date);
                return formatted;
            }
            else if ((/[-: ]+/).test(segment)) {
                return segment; // Return separator as-is
            }
            throw new Error(`Unknown time format segment: "${segment}".`);
        })
            .join('');
        return result;
    }
}
//# sourceMappingURL=TimeFormatter.js.map