import { DateTime, IANAZone } from 'luxon';
import { useMemo } from 'react';
import { getDateFromString } from '../../UI/widgets/utils/dateUtils';
import { GridExcelStyleEnums } from '../../enums/unitySystemEnums';
import { useAuthedUser } from '../../utilities/auth/useAuthedUser';
import { useGetLocalizedDateString, useGetLocalizedDateTimeString } from './dateLocalizationHooks';
export const useDateStringColumnDef = (includeTime = false, timeZone = null, includeTimeZoneOffset = false) => {
    const user = useAuthedUser();
    const zone = useMemo(() => {
        var _a, _b;
        var _c;
        const tz = (_c = timeZone !== null && timeZone !== void 0 ? timeZone : (_a = user === null || user === void 0 ? void 0 : user.additionalProperties) === null || _a === void 0 ? void 0 : _a.timeZone) !== null && _c !== void 0 ? _c : 'local';
        const validTimeZone = tz !== 'local' &&
            (tz === ((_b = user === null || user === void 0 ? void 0 : user.additionalProperties) === null || _b === void 0 ? void 0 : _b.timeZone) || IANAZone.isValidZone(tz))
            ? tz
            : 'local'; // IANAZone.isValidZone is very slow, skip it if known valid timeZone passed
        return validTimeZone;
    }, [timeZone, user]);
    const getLocalizedDateTimeString = useGetLocalizedDateTimeString(zone, includeTimeZoneOffset);
    const getLocalizedDateString = useGetLocalizedDateString(zone);
    const columnDef = useMemo(() => {
        return !user
            ? {}
            : ({
                valueFormatter: (params) => {
                    if (!params) {
                        return '';
                    }
                    if (includeTime) {
                        return getLocalizedDateTimeString(params.value);
                    }
                    else {
                        return getLocalizedDateString(params.value);
                    }
                },
                cellClass: includeTime
                    ? GridExcelStyleEnums.DateTimeShortWithSeconds
                    : GridExcelStyleEnums.DateShort,
                filter: 'agDateColumnFilter',
                filterParams: {
                    comparator: function (filterLocalDateAtMidnight, cellValue) {
                        const cellDate = getDateFromString(cellValue);
                        if (!cellDate) {
                            return null;
                        }
                        // Assume date picker is in the same time zone that we're displaying dates in so that the results make sense to the user
                        const filterDateTimeMidnight = DateTime.fromJSDate(filterLocalDateAtMidnight).setZone(zone, { keepLocalTime: true });
                        const cellDateTimeMidnight = DateTime.fromJSDate(cellDate, { zone: zone }).set({
                            hour: 0,
                            minute: 0,
                            second: 0,
                            millisecond: 0,
                        });
                        if (cellDateTimeMidnight < filterDateTimeMidnight) {
                            return -1;
                        }
                        else if (cellDateTimeMidnight > filterDateTimeMidnight) {
                            return 1;
                        }
                        else {
                            return 0;
                        }
                    },
                    buttons: ['apply', 'reset'],
                    closeOnApply: true,
                    suppressAndOrCondition: true,
                    filterOptions: ['equals', 'lessThan', 'greaterThan', 'inRange'],
                },
            });
    }, [includeTime, getLocalizedDateString, getLocalizedDateTimeString, user, zone]);
    return columnDef;
};
//# sourceMappingURL=useDateStringColumnDef.js.map