import { IANAZone } from 'luxon';
import { useCallback } from 'react';
import { getLocalizedDateString, getLocalizedDateTimeString, } from '../../UI/widgets/utils/dateUtils';
import { useAuthedUser } from '../../utilities/auth/useAuthedUser';
/**
 * Gets a function that can be used to convert a date or
 * ISO 8601 date string (EX: yyyy-MM-ddThh:mm:ss[Z|offset])
 * to a localized date string without time in the specified IANA time zone.
 * @param timeZone An IANA time zone to convert the date to before returning as a string.
 * @returns A function to convert a date to a localized string.
 */
export const useGetLocalizedDateString = (timeZone) => {
    const user = useAuthedUser();
    const getDateString = useCallback((date) => {
        var _a;
        const validTimeZone = timeZone !== 'local' &&
            (timeZone === ((_a = user === null || user === void 0 ? void 0 : user.additionalProperties) === null || _a === void 0 ? void 0 : _a.activeUtilityTimeZone) ||
                IANAZone.isValidZone(timeZone))
            ? timeZone
            : 'local'; // IANAZone.isValidZone is very slow, skip it if known valid timeZone passed
        return getLocalizedDateString(date, validTimeZone);
    }, [user, timeZone]);
    return getDateString;
};
/**
 * Gets a function that can be used to convert a date or
 * ISO 8601 date string (EX: yyyy-MM-ddThh:mm:ss[Z|offset])
 * to a localized date time string in the specified IANA time zone.
 * @param timeZone The IANA time zone to convert the date before converting to
 * a localized string.
 * @param includeTimeZoneOffset If true, include the time zone offset in the localized string.
 * @returns A function to convert a date to a localized string.
 */
export const useGetLocalizedDateTimeString = (timeZone, includeTimeZoneOffset = false) => {
    const user = useAuthedUser();
    const getDateTimeString = useCallback((date, delimiter = null) => {
        var _a;
        const validTimeZone = timeZone !== 'local' &&
            (timeZone === ((_a = user === null || user === void 0 ? void 0 : user.additionalProperties) === null || _a === void 0 ? void 0 : _a.activeUtilityTimeZone) ||
                IANAZone.isValidZone(timeZone))
            ? timeZone
            : 'local'; // IANAZone.isValidZone is very slow, skip it if known valid timeZone passed
        return getLocalizedDateTimeString(date, delimiter, validTimeZone, includeTimeZoneOffset);
    }, [user, timeZone, includeTimeZoneOffset]);
    return getDateTimeString;
};
/**
 * Gets a function that can be used to convert a date or
 * ISO 8601 date string (EX: yyyy-MM-ddThh:mm:ss[Z|offset])
 * to a localized date time string in the current user's time zone.
 * @param includeTimeZoneOffset If true, include the time zone offset in the localized string.
 * @returns A function to convert a date to a localized string.
 */
export const useGetLocalizedUserDateTimeString = (includeTimeZoneOffset = false) => {
    const user = useAuthedUser();
    const getDateTimeString = useCallback((date, delimiter = null) => {
        var _a;
        return getLocalizedDateTimeString(date, delimiter, (_a = user === null || user === void 0 ? void 0 : user.additionalProperties) === null || _a === void 0 ? void 0 : _a.timeZone, includeTimeZoneOffset);
    }, [user, includeTimeZoneOffset]);
    return getDateTimeString;
};
//# sourceMappingURL=dateLocalizationHooks.js.map