import { jsxs as P, jsx as i } from "react/jsx-runtime";
import { useRef as D, useCallback as l } from "react";
import { DATE_FORMAT_DEFAULT as J, DATE_PICKER_CALENDAR_ID as Q, DATE_PICKER_TRIGGER_ID as T } from "./constants/index.js";
import W from "./hooks/useDatePicker.js";
import X from "./utils/cloneTriggerElement.js";
import { Popover as Z, PopoverAnchor as $, PopoverContent as ee } from "../popover/PopoverPrimitive.js";
import ne from "../input-group/InputGroup.js";
import te from "../input-group/components/input-group-input/InputGroupInput.js";
import re from "../input-group/components/input-group-addon/InputGroupAddon.js";
import oe from "../input-group/components/input-group-icon/InputGroupIcon.js";
import ae from "./components/date-picker-dropdown/DatePickerDropdown.js";
import ie from "../../utils/pickFieldAriaProps.js";
import "./utils/applyFormatFromParts.js";
import "./utils/formatDateInput.js";
import "./utils/formatInputAsTyping.js";
import "./utils/parseFormatParts.js";
import "./utils/validateDayForMonth.js";
const le = (d) => {
  const {
    placeholder: x,
    isDisabled: o = !1,
    id: I,
    disableDate: A,
    format: f = J,
    header: R,
    hasInput: m = !1,
    triggerContent: h,
    footer: w,
    captionLayout: F = "dropdown",
    fromYear: K,
    toYear: O,
    isError: _,
    size: G = "default"
  } = d, g = D(null), { isOpen: a, setIsOpen: t, inputValue: y, parsedInputDate: C, defaultMonth: B, handleCalendarChange: N, handleInputChange: b, handleInputBlur: r, handleClear: q } = W(d, f), u = D(null), k = D(null), p = l(() => {
    const e = k.current;
    if (!e) return !1;
    const n = e.querySelector('button.rdp-day_button[tabindex="0"]') ?? e.querySelector('button:not([disabled]):not([tabindex="-1"]), input:not([disabled]), [tabindex="0"]');
    return n ? (n.focus(), !0) : !1;
  }, []), L = l(() => {
    requestAnimationFrame(() => {
      var n;
      const e = document.activeElement;
      (n = u.current) != null && n.contains(e) || e != null && e.closest("[data-radix-popper-content-wrapper]") || r();
    });
  }, [r]), j = C ? C.getTime() : Q, M = ie(d), S = l(() => {
    o || t(!0);
  }, [o, t]), c = l(() => {
    o || t((e) => !e);
  }, [o, t]), Y = l(
    (e) => {
      var n, s;
      e.key === "Enter" && (e.preventDefault(), r(), (n = g.current) == null || n.blur()), e.key === "Escape" && (t(!1), (s = g.current) == null || s.blur()), e.key === "Tab" && !e.shiftKey && a && p() && e.preventDefault();
    },
    [r, t, a, p]
  ), z = l(
    (e) => {
      var v, E;
      const n = e.relatedTarget;
      (v = n == null ? void 0 : n.closest) != null && v.call(n, "[data-radix-popper-content-wrapper]") || (E = u.current) != null && E.contains(n) || (m ? r() : t(!1));
    },
    [m, r, t]
  ), U = l(
    (e) => {
      if (e.key === "Enter" || e.key === " ") {
        e.key === " " && e.preventDefault(), c();
        return;
      }
      e.key === "Escape" && t(!1), e.key === "Tab" && !e.shiftKey && a && p() && e.preventDefault();
    },
    [c, t, a, p]
  ), V = X(h, {
    id: I,
    defaultId: T,
    isOpen: a,
    isDisabled: o,
    onTriggerClick: c,
    onEscape: () => t(!1)
  }), H = l(
    (e) => {
      e || (r(), t(!1));
    },
    [r, t]
  );
  return /* @__PURE__ */ P(Z, { open: a, onOpenChange: H, children: [
    /* @__PURE__ */ i($, { asChild: !0, children: /* @__PURE__ */ i("div", { ref: u, className: "flex relative self-start w-fit", onBlur: z, children: h ? (
      /* Custom trigger wrapper — no visual styling, user owns the appearance */
      V || /* Non-element custom trigger wrapper — no visual styling, user owns the appearance */
      /* @__PURE__ */ i(
        "div",
        {
          onClick: c,
          onKeyDown: U,
          role: "button",
          tabIndex: o ? -1 : 0,
          "aria-expanded": a,
          "aria-haspopup": "dialog",
          "aria-disabled": o,
          className: "group/date-filter w-fit",
          children: h
        }
      )
    ) : (
      /* Default input trigger */
      /* @__PURE__ */ P(ne, { isError: _, size: G, children: [
        /* @__PURE__ */ i(
          te,
          {
            id: I || T,
            ref: g,
            disabled: o,
            placeholder: x || f,
            value: y,
            onChange: b,
            onFocus: S,
            onKeyDown: Y,
            ...M
          }
        ),
        /* @__PURE__ */ i(re, { align: "inline-right", children: /* @__PURE__ */ i(oe, { name: "Calendar" }) })
      ] })
    ) }) }),
    a && /* @__PURE__ */ i(
      ee,
      {
        ref: k,
        className: "!bg-transparent !p-0 !shadow-none w-auto",
        align: "start",
        sideOffset: 4,
        avoidCollisions: !0,
        collisionPadding: 8,
        onOpenAutoFocus: (e) => e.preventDefault(),
        onCloseAutoFocus: (e) => e.preventDefault(),
        onInteractOutside: (e) => {
          var n;
          (n = u.current) != null && n.contains(e.target) && e.preventDefault();
        },
        onBlur: (e) => {
          var s;
          const n = e.relatedTarget;
          e.currentTarget.contains(n) || (s = u.current) != null && s.contains(n) || n != null && n.closest("[data-radix-popper-content-wrapper]") || (r(), t(!1));
        },
        children: /* @__PURE__ */ i(
          ae,
          {
            header: R,
            hasInput: m,
            footer: w,
            dateFormat: f,
            inputValue: y,
            parsedInputDate: C,
            defaultMonth: B,
            calendarKey: j,
            disableDate: A,
            onInputChange: b,
            onInputBlur: L,
            onCalendarChange: N,
            onApply: r,
            onClear: () => {
              q(), t(!1);
            },
            captionLayout: F,
            fromYear: K,
            toYear: O
          }
        )
      }
    )
  ] });
};
le.displayName = "DatePicker";
export {
  le as default
};
//# sourceMappingURL=DatePicker.js.map
