"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.findFirstDayOfWeek = findFirstDayOfWeek;
exports.getWeekdayInfo = getWeekdayInfo;
exports.dateArgToISO = dateArgToISO;
exports.toISO = toISO;
exports.toLocalISO = toLocalISO;
exports.fromISO = fromISO;
exports.offsetISO = offsetISO;
const _1 = require(".");
function findFirstDayOfWeek(localeName) {
    // weekInfo only exists on some browsers, so we default to Sunday otherwise
    // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/weekInfo
    const locale = new Intl.Locale(localeName);
    if (locale.weekInfo) {
        return locale.weekInfo.firstDay;
    }
    return 0;
}
function getWeekdayInfo(localeName) {
    localeName = (0, _1.localeDefault)(localeName);
    const locale = (0, _1.getLocale)(localeName);
    const firstDayOfWeek = locale.w;
    const weekdayLabelFormatter = new Intl.DateTimeFormat(localeName, {
        weekday: "short",
    });
    const weekday = new Date(2022, 9, 2 + firstDayOfWeek); // October 2, 2022 was a Sunday
    const weekdayLabels = [...Array(7)].map(() => {
        const dayLabel = weekdayLabelFormatter.format(weekday);
        weekday.setDate(weekday.getDate() + 1);
        return dayLabel;
    });
    return { firstDayOfWeek, weekdayLabels };
}
function dateArgToISO(arg) {
    if (!arg)
        return undefined;
    if (/^\d\d\d\d-\d\d-\d\d$/g.test(arg))
        return arg;
    const date = toISO(new Date(arg));
    return /^\d\d\d\d-\d\d-\d\d$/g.test(date) ? date : undefined;
}
/**
 * ISO 8601 date format (YYYY-MM-DD) in **UTC** timezone
 */
function toISO(date) {
    return date.toISOString().slice(0, 10);
}
/**
 * ISO 8601 date format (YYYY-MM-DD) in **local** timezone
 */
function toLocalISO(date) {
    // This works because Canada uses the YYYY-MM-DD format
    return date.toLocaleDateString("en-CA");
}
function fromISO(iso) {
    return new Date(iso);
}
function offsetISO(iso, days) {
    const date = fromISO(iso);
    date.setUTCDate(date.getUTCDate() + days);
    return toISO(date);
}
