import { jsx } from "react/jsx-runtime";
import react from "react";
import { connect, mapProps, mapReadPretty } from "@tachybase/schema";
import { DatePicker } from "antd";
import dayjs from "dayjs";
import { useTranslation } from "react-i18next";
import { ReadPretty } from "./ReadPretty.mjs";
import { getDateRanges, mapDatePicker, mapRangePicker } from "./util.mjs";
const DatePickerContext = /*#__PURE__*/ react.createContext({
    utc: true
});
const useDatePickerContext = ()=>react.useContext(DatePickerContext);
const DatePickerProvider = DatePickerContext.Provider;
const InternalDatePicker = connect(DatePicker, mapProps(mapDatePicker()), mapReadPretty(ReadPretty.DatePicker));
const InternalRangePicker = connect(DatePicker.RangePicker, mapProps(mapRangePicker()), mapReadPretty(ReadPretty.DateRangePicker));
const DatePicker_DatePicker = (props)=>{
    const { utc = true } = useDatePickerContext();
    const value = Array.isArray(props.value) ? props.value[0] : props.value;
    props = {
        utc,
        ...props
    };
    return /*#__PURE__*/ jsx(InternalDatePicker, {
        ...props,
        value: value
    });
};
DatePicker_DatePicker.ReadPretty = ReadPretty.DatePicker;
DatePicker_DatePicker.RangePicker = function(props) {
    const { t } = useTranslation();
    const { utc = true } = useDatePickerContext();
    if ('range' === props.valueType) {
        const originOnChange = props.onChange;
        const originValue = props.value;
        props = {
            ...props
        };
        if (originValue) props.value = [
            originValue[0].value,
            dayjs(originValue[1].value).add(-1, 'd').toISOString()
        ];
        props.onChange = (value)=>{
            if (!value || !value.length) return void originOnChange(null);
            originOnChange([
                {
                    value: dayjs(value[0]).toISOString(),
                    inclusive: true
                },
                {
                    value: dayjs(value[1]).add(1, 'd').toISOString(),
                    inclusive: false
                }
            ]);
        };
    }
    const rangesValue = getDateRanges();
    const presets = [
        {
            label: t('Today'),
            value: rangesValue.today
        },
        {
            label: t('Last week'),
            value: rangesValue.lastWeek
        },
        {
            label: t('This week'),
            value: rangesValue.thisWeek
        },
        {
            label: t('Next week'),
            value: rangesValue.nextWeek
        },
        {
            label: t('Last month'),
            value: rangesValue.lastMonth
        },
        {
            label: t('This month'),
            value: rangesValue.thisMonth
        },
        {
            label: t('Next month'),
            value: rangesValue.nextMonth
        },
        {
            label: t('Last quarter'),
            value: rangesValue.lastQuarter
        },
        {
            label: t('This quarter'),
            value: rangesValue.thisQuarter
        },
        {
            label: t('Next quarter'),
            value: rangesValue.nextQuarter
        },
        {
            label: t('Last year'),
            value: rangesValue.lastYear
        },
        {
            label: t('This year'),
            value: rangesValue.thisYear
        },
        {
            label: t('Next year'),
            value: rangesValue.nextYear
        },
        {
            label: t('Last 7 days'),
            value: rangesValue.last7Days
        },
        {
            label: t('Next 7 days'),
            value: rangesValue.next7Days
        },
        {
            label: t('Last 30 days'),
            value: rangesValue.last30Days
        },
        {
            label: t('Next 30 days'),
            value: rangesValue.next30Days
        },
        {
            label: t('Last 90 days'),
            value: rangesValue.last90Days
        },
        {
            label: t('Next 90 days'),
            value: rangesValue.next90Days
        }
    ];
    props = {
        utc,
        presets,
        ...props
    };
    return /*#__PURE__*/ jsx(InternalRangePicker, {
        ...props
    });
};
const date_picker_DatePicker = DatePicker_DatePicker;
export { DatePicker_DatePicker as DatePicker, DatePickerProvider, date_picker_DatePicker as default, useDatePickerContext };
