import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { START_DATE, useDatepicker } from "@datepicker-react/hooks";
import { Calendar } from "@truststack/icons-ui";
import { useEffect, useMemo, useState } from "react";
import { KeyboardAvoidingView } from "react-native";
import { Adapt, ScrollView, XStack, YStack } from "tamagui";
import { CalendarPicker } from "./CalendarPicker.native.js";
import { DatepickerProvider } from "./DatePickerProvider.native.js";
import { Dialog } from "./Dialog.native.js";
import { Sheet } from "./Sheet.native.js";
import { TextField } from "./TextField.native.js";
import { TimePicker } from "./TimePicker.native.js";
import { getLocaleDateTime, isDateValid } from "./dateHelper.native.js";
function DateTimePicker(param) {
  var {
      value,
      onChange,
      ...props
    } = param,
    [time, setTime] = useState(value),
    {
      activeMonths,
      firstDayOfWeek,
      ...context
    } = useDatepicker({
      startDate: value,
      endDate: null,
      focusedInput: START_DATE,
      onDatesChange: handleDateChange,
      numberOfMonths: 1
    });
  function handleDateChange(data) {
    onChange && onChange(data.startDate);
  }
  var inputValue = useMemo(function () {
    return getLocaleDateTime({
      date: new Date(value)
    });
  }, [value]);
  return useEffect(function () {
    if (!(!value || !time)) {
      var year = value.getFullYear(),
        month = value.getMonth(),
        day = value.getDate(),
        hours = time.getHours(),
        minutes = time.getMinutes(),
        seconds = time.getSeconds(),
        milliseconds = time.getMilliseconds(),
        newDate = new Date(year, month, day, hours, minutes, seconds, milliseconds);
      newDate.getTime() !== value.getTime() && onChange && onChange(isDateValid(newDate) ? newDate : /* @__PURE__ */new Date());
    }
  }, [time]), /* @__PURE__ */_jsx(DatepickerProvider, {
    ...context,
    children: /* @__PURE__ */_jsxs(Dialog, {
      modal: !0,
      children: [/* @__PURE__ */_jsx(Dialog.Trigger, {
        fg: 1,
        disabled: props.disabled,
        padding: 0,
        bc: "transparent",
        bw: 0,
        children: /* @__PURE__ */_jsx(TextField, {
          ...props,
          pointerEvents: "none",
          cursor: "pointer",
          value: inputValue,
          placeholder: "DD/MM/YYYY",
          trailingIcon: Calendar
        })
      }), /* @__PURE__ */_jsx(Adapt, {
        when: "sm",
        platform: "touch",
        children: /* @__PURE__ */_jsxs(Sheet, {
          modal: !0,
          dismissOnSnapToBottom: !0,
          snapPoints: [80],
          moveOnKeyboardChange: !0,
          animation: "quick",
          children: [/* @__PURE__ */_jsx(Sheet.Overlay, {}), /* @__PURE__ */_jsx(Sheet.Handle, {}), /* @__PURE__ */_jsx(Sheet.Frame, {
            p: 16,
            pt: 20,
            children: (/* We need to repropagate the context for this to work on Android */
            /* @__PURE__ */_jsx(DatepickerProvider, {
              ...context,
              children: /* @__PURE__ */_jsx(Adapt.Contents, {})
            }))
          })]
        })
      }), /* @__PURE__ */_jsxs(Dialog.Portal, {
        children: [/* @__PURE__ */_jsx(Dialog.Overlay, {}, "date-time-picker-overlay"), /* @__PURE__ */_jsx(Dialog.Content, {
          fullscreen: !1,
          backgroundColor: "$surfaceContainerLowest",
          children: (
          /* ScrollView helps the content render properly
          when on-screen keyboard pops up on mobile */
          /* @__PURE__ */
          _jsx(ScrollView, {
            children: /* @__PURE__ */_jsxs(YStack, {
              gap: 16,
              children: [/* @__PURE__ */_jsx(CalendarPicker, {
                col: "$onPrimaryContainer",
                activeMonths,
                firstDayOfWeek
              }), /* @__PURE__ */_jsx(XStack, {
                justifyContent: "center",
                children: /* @__PURE__ */_jsx(KeyboardAvoidingView, {
                  behavior: "padding",
                  children: /* @__PURE__ */_jsx(TimePicker, {
                    value,
                    onChange: function (time2) {
                      return setTime(new Date(time2 || /* @__PURE__ */new Date()));
                    }
                  })
                })
              })]
            })
          }))
        }, "date-time-picker-content")]
      }, "date-time-picker-portal")]
    })
  });
}
export { DateTimePicker };
//# sourceMappingURL=DateTimePicker.native.js.map
