import { jsxs as i, jsx as l } from "@emotion/react/jsx-runtime";
import { useModuleDictionary as Ke } from "@m4l/core";
import { DateTimeCreatedOutlined as ke } from "@m4l/icons";
import v from "clsx";
import { forwardRef as ve, useRef as h } from "react";
import { useForkRef as G } from "@mui/material/utils";
import { DateRangeCalendarPicker as Ge } from "../DateRangeCalendarPicker/DateRangeCalendarPicker.js";
import { TextField as Ve } from "../extended/mui/TextField/TextField.js";
import { DATE_TIME_RANGE_PICKER_RANGE_SEPARATOR as V, DATE_TIME_RANGE_PICKER_KEY_COMPONENT as t, DATE_TIME_RANGE_PICKER_OPEN_BUTTON_LABEL as we, DATE_TIME_RANGE_PICKER_CANCEL_LABEL as Ue, DATE_TIME_RANGE_PICKER_CONFIRM_LABEL as $e, DATE_TIME_RANGE_PICKER_ENTER_SHORTCUT_IGNORE_TARGET_SELECTOR as ze, DATE_TIME_RANGE_PICKER_ENTER_SHORTCUT_CONFIRM_TARGET_SELECTOR as He } from "./constants.js";
import { resolveDateTimeRangePickerDictionaryLabel as R, DICTIONARY as C } from "./dictionary.js";
import { DateTimeRangePickerSlots as r } from "./slots/DateTimeRangePickerEnum.js";
import { getPropDataTestId as a } from "../../test/getNameDataTestId.js";
import { getComponentClasses as je } from "../../utils/getComponentSlotRoot.js";
import { useDateTimeRangePicker as Ye } from "./hooks/useDateTimeRangePicker.js";
import { RootStyled as Je, EndAdornmentStyled as qe, PopoverStyled as Qe, ContentStyled as We, ScrollAreaStyled as Xe, SummaryStyled as Ze, SummaryValueStyled as w, SummarySeparatorStyled as et, BodyStyled as tt, ShortcutsStyled as rt, ShortcutItemStyled as ot, CalendarStyled as at, FooterStyled as nt, SelectedDaysStyled as lt, FooterActionsStyled as it, ActionCancelStyled as dt, ActionIntroStyled as st } from "./slots/DateTimeRangePickerSlots.js";
import { IconButton as ct } from "../extended/mui/IconButton/IconButton.js";
function ut(s) {
  return !!s.closest(ze);
}
function mt(s) {
  return !!s.closest(He);
}
const pt = ve(
  function(U, $) {
    const {
      calendars: z,
      className: _,
      compact: H,
      dataTestId: o,
      defaultCalendarMonth: j,
      defaultValue: Y,
      disabled: c = !1,
      error: S = !1,
      getDayBadgeContent: J,
      InputProps: m,
      inputProps: P,
      inputRef: q,
      maxDate: D,
      maxDays: A,
      minDate: I,
      onBlur: Q,
      onChange: W,
      onFocus: X,
      onKeyDown: Z,
      onMouseUp: ee,
      openOnFocus: g = !0,
      placeholder: te,
      readOnly: re = !1,
      referenceDate: oe,
      shouldDisableDate: N,
      shortcuts: ae,
      size: d = "medium",
      timeDisplayVariant: O,
      value: ne,
      variant: b = "outlined",
      enableTimeRange: le = !1,
      ...ie
    } = U, p = h(null), F = h(null), f = h(null), { getLabel: y } = Ke(), de = G(p, $), se = G(F, q), ce = je(t, r), u = re || !!m?.readOnly, ue = R(
      y,
      C.OPEN_BUTTON_LABEL,
      we
    ), me = R(
      y,
      C.ACTION_CANCEL,
      Ue
    ), pe = R(
      y,
      C.ACTION_CONFIRM,
      $e
    ), {
      anchorEl: fe,
      draftValue: ye,
      effectiveCalendars: B,
      fieldValue: Ee,
      handleCancel: Te,
      handleClose: he,
      handleConfirm: Re,
      handleFieldBlur: Ce,
      handleFieldInputChange: _e,
      handleFieldKeyDown: Se,
      handleFieldFocus: Pe,
      handleFieldMouseUp: De,
      handleInputPaste: Ae,
      handleOpen: L,
      handlePickerChange: Ie,
      handlePopoverEnterShortcut: ge,
      handleShortcutSelect: Ne,
      isConfirmDisabled: x,
      isPopoverEnterShortcutArmed: Oe,
      open: M,
      ownerState: n,
      resolvedCompact: K,
      resolvedDisplayVariant: be,
      resolvedShortcuts: Fe,
      selectedShortcutIds: Be,
      selectedDaysText: Le,
      summaryText: k
    } = Ye(
      {
        calendars: z,
        compact: H,
        defaultValue: Y,
        disabled: c,
        error: S,
        maxDate: D,
        maxDays: A,
        minDate: I,
        onChange: W,
        readOnly: u,
        shouldDisableDate: N,
        shortcuts: ae,
        size: d,
        timeDisplayVariant: O,
        value: ne,
        variant: b
      },
      F,
      f
    ), [xe = k, E = ""] = k.split(V);
    function Me(e) {
      if (e.key !== "Enter" || e.altKey || e.ctrlKey || e.metaKey || e.shiftKey)
        return;
      const T = e.target;
      if (T instanceof HTMLElement && !(ut(T) || !mt(T))) {
        if (x) {
          e.preventDefault(), e.stopPropagation();
          return;
        }
        Oe && (e.preventDefault(), e.stopPropagation(), ge());
      }
    }
    return /* @__PURE__ */ i(
      Je,
      {
        ref: de,
        className: v(_),
        ownerState: n,
        ...a(t, r.root, o),
        children: [
          /* @__PURE__ */ l(
            Ve,
            {
              ...ie,
              ...a(t, r.textField, o),
              className: v(_),
              disabled: c,
              error: S,
              inputMode: be === "ampm" ? "text" : "numeric",
              inputRef: se,
              inputProps: {
                ...P,
                /**
                 * Allow paste-driven full-value parsing before delegating to any consumer handler.
                 */
                onPaste: (e) => {
                  Ae(e), P?.onPaste?.(e);
                },
                spellCheck: !1
              },
              InputProps: {
                ...m,
                endAdornment: /* @__PURE__ */ i(
                  qe,
                  {
                    ownerState: n,
                    ...a(
                      t,
                      r.endAdornment,
                      o
                    ),
                    children: [
                      m?.endAdornment,
                      /* @__PURE__ */ l(
                        ct,
                        {
                          "aria-label": ue,
                          dataTestId: a(
                            t,
                            r.iconButton,
                            o
                          )["data-testid"],
                          disabled: c || u,
                          icon: /* @__PURE__ */ l(ke, {}),
                          onClick: (e) => L(p.current ?? e.currentTarget),
                          size: d
                        }
                      )
                    ]
                  }
                ),
                readOnly: u
              },
              onBlur: (e) => {
                Ce(), Q?.(e);
              },
              onChange: (e) => {
                _e(e.target.value);
              },
              onFocus: (e) => {
                Pe(e), g && !M && L(p.current ?? e.currentTarget), X?.(e);
              },
              onKeyDown: (e) => {
                Se(e), Z?.(e);
              },
              onMouseUp: (e) => {
                De(e), ee?.(e);
              },
              placeholder: te,
              size: d,
              value: Ee,
              variant: b
            }
          ),
          /* @__PURE__ */ l(
            Qe,
            {
              ...a(t, r.popover, o),
              anchorEl: fe,
              className: ce.popover,
              anchorOrigin: {
                horizontal: "left",
                vertical: "bottom"
              },
              disableRestoreFocus: g,
              disableScrollLock: !0,
              onClose: he,
              open: M,
              ownerState: n,
              slotProps: {
                paper: {
                  ref: f
                }
              },
              transformOrigin: {
                horizontal: "left",
                vertical: "top"
              },
              children: /* @__PURE__ */ i(
                We,
                {
                  onKeyDownCapture: Me,
                  ownerState: n,
                  ...a(t, r.content, o),
                  children: [
                    /* @__PURE__ */ i(
                      Xe,
                      {
                        ownerState: n,
                        ...a(
                          t,
                          r.scrollArea,
                          o
                        ),
                        children: [
                          /* @__PURE__ */ i(
                            Ze,
                            {
                              color: "primary.semanticText",
                              ownerState: n,
                              variant: "paragraphDens",
                              fontFamily: "Jura",
                              ...a(
                                t,
                                r.summary,
                                o
                              ),
                              children: [
                                /* @__PURE__ */ l(
                                  w,
                                  {
                                    ownerState: n,
                                    ...a(
                                      t,
                                      r.summaryValue,
                                      `${o ?? "default"}-start`
                                    ),
                                    children: xe
                                  }
                                ),
                                E && !K ? /* @__PURE__ */ l(
                                  et,
                                  {
                                    "aria-hidden": "true",
                                    ownerState: n,
                                    ...a(
                                      t,
                                      r.summarySeparator,
                                      o
                                    ),
                                    children: V
                                  }
                                ) : null,
                                E ? /* @__PURE__ */ l(
                                  w,
                                  {
                                    ownerState: n,
                                    ...a(
                                      t,
                                      r.summaryValue,
                                      `${o ?? "default"}-end`
                                    ),
                                    children: E
                                  }
                                ) : null
                              ]
                            }
                          ),
                          /* @__PURE__ */ i(
                            tt,
                            {
                              ownerState: n,
                              ...a(
                                t,
                                r.body,
                                o
                              ),
                              children: [
                                /* @__PURE__ */ l(
                                  rt,
                                  {
                                    ownerState: n,
                                    ...a(
                                      t,
                                      r.shortcuts,
                                      o
                                    ),
                                    children: Fe.map((e) => /* @__PURE__ */ l(
                                      ot,
                                      {
                                        dataTestId: a(
                                          t,
                                          r.shortcutItem,
                                          `${o ?? "default"}-${e.id}`
                                        )["data-testid"],
                                        label: e.label,
                                        onClick: () => Ne(e),
                                        ownerState: n,
                                        selected: Be.includes(e.id),
                                        size: d
                                      },
                                      e.id
                                    ))
                                  }
                                ),
                                /* @__PURE__ */ l(
                                  at,
                                  {
                                    ownerState: n,
                                    ...a(
                                      t,
                                      r.calendar,
                                      o
                                    ),
                                    children: /* @__PURE__ */ l(
                                      Ge,
                                      {
                                        calendars: B,
                                        dataTestId: a(
                                          t,
                                          r.calendar,
                                          o
                                        )["data-testid"],
                                        defaultCalendarMonth: j,
                                        disabled: c,
                                        getDayBadgeContent: J,
                                        maxDate: D,
                                        maxDays: A,
                                        minDate: I,
                                        onChange: (e) => {
                                          Ie(e);
                                        },
                                        readOnly: u,
                                        referenceDate: oe,
                                        shouldDisableDate: N,
                                        size: d,
                                        slotProps: {
                                          yearPopover: {
                                            container: () => f.current
                                          }
                                        },
                                        enableTimeRange: le,
                                        timeDisplayVariant: O,
                                        value: ye,
                                        variant: K || B === 1 ? "column" : "default"
                                      }
                                    )
                                  }
                                )
                              ]
                            }
                          )
                        ]
                      }
                    ),
                    /* @__PURE__ */ i(
                      nt,
                      {
                        ownerState: n,
                        ...a(t, r.footer, o),
                        children: [
                          /* @__PURE__ */ l(
                            lt,
                            {
                              color: "text.secondary",
                              ownerState: n,
                              variant: "bodyStandard",
                              ...a(
                                t,
                                r.selectedDays,
                                o
                              ),
                              children: Le
                            }
                          ),
                          /* @__PURE__ */ i(
                            it,
                            {
                              ownerState: n,
                              ...a(
                                t,
                                r.footerActions,
                                o
                              ),
                              children: [
                                /* @__PURE__ */ l(
                                  dt,
                                  {
                                    label: me,
                                    onClick: Te,
                                    ownerState: n,
                                    size: d,
                                    variant: "text",
                                    type: "button",
                                    ...a(
                                      t,
                                      r.actionCancel,
                                      o
                                    )
                                  }
                                ),
                                /* @__PURE__ */ l(
                                  st,
                                  {
                                    label: pe,
                                    onClick: Re,
                                    disabled: x,
                                    ownerState: n,
                                    size: d,
                                    variant: "text",
                                    type: "button",
                                    ...a(
                                      t,
                                      r.actionIntro,
                                      o
                                    )
                                  }
                                )
                              ]
                            }
                          )
                        ]
                      }
                    )
                  ]
                }
              )
            }
          )
        ]
      }
    );
  }
);
pt.displayName = "DateTimeRangePicker";
export {
  pt as DateTimeRangePicker
};
