// #region Imports
import { tryGet } from '../Extensions/Extensions';
// #endregion
/**
 * A utility class for formatting dates using format strings.
 *
 * Supported date format specifiers:
 * - `d`   : Day of the month (1-31) without leading zero.
 * - `dd`  : Day of the month (01-31) with leading zero.
 * - `ddd` : Abbreviated name of the day of the week (e.g., Mon, Tue).
 * - `dddd`: Full name of the day of the week (e.g., Monday, Tuesday).
 * - `M`   : Month (1-12) without leading zero.
 * - `MM`  : Month (01-12) with leading zero.
 * - `MMM` : Abbreviated name of the month (e.g., Jan, Feb).
 * - `MMMM`: Full name of the month (e.g., January, February).
 * - `y`   : Year as the last two digits without leading zero (e.g., 9 for 2009).
 * - `yy`  : Year as the last two digits with leading zero (e.g., 09 for 2009).
 * - `yyy` : Year with a minimum of three digits (e.g., 009, 2009).
 * - `yyyy`: Year as a four-digit number (e.g., 2009).
 * - `-`   : Custom separator (e.g., hyphen).
 * - `/`   : Date separator (e.g., slash).
 * - `.`   : Custom separator (e.g., dot).
 *
 * @public
 */
export class DateFormatter {
    static _validSpecifiers = [
        'd', 'dd', 'ddd', 'dddd', 'M', 'MM', 'MMM', 'MMMM', 'y', 'yy', 'yyy', 'yyyy', '-', '/', '.'
    ];
    static _formatMapping = {
        d: { day: 'numeric' },
        dd: { day: '2-digit' },
        ddd: { weekday: 'short' },
        dddd: { weekday: 'long' },
        M: { month: 'numeric' },
        MM: { month: '2-digit' },
        MMM: { month: 'short' },
        MMMM: { month: 'long' },
        y: { year: '2-digit' },
        yy: { year: '2-digit' },
        yyy: { year: 'numeric' },
        yyyy: { year: 'numeric' }
    };
    /**
     * Formats a date using a date-specific format string.
     *
     * @param date The date to format.
     * @param format The format string.
     * @param locale The locale for Intl formatting (default: 'en-US').
     * @returns The formatted date string.
     */
    static format(date, format, locale = 'en-US') {
        if (typeof date === 'string') {
            date = new Date(date);
        }
        const segments = format.match(/d{1,4}|y{1,4}|M{1,4}|[-/. ]+/g);
        if (!segments) {
            throw new Error(`Unable to parse format string: "${format}".`);
        }
        return segments
            .map((segment) => {
            const intlOptions = tryGet(this._formatMapping, segment);
            if (intlOptions) {
                return new Intl.DateTimeFormat(locale, intlOptions).format(date);
            }
            else if ((/[-/. ]+/).test(segment)) {
                return segment; // Return separator as-is
            }
            throw new Error(`Unknown date format segment: "${segment}".`);
        })
            .join('');
    }
}
//# sourceMappingURL=DateFormatter.js.map