import { jsxs as R, jsx as o } from "react/jsx-runtime";
import { useRef as F, useMemo as y, useCallback as l } from "react";
import { DATE_RANGE_PICKER_TRIGGER_ID as A } from "./constants/index.js";
import { DATE_FORMAT_DEFAULT as ie } from "../date-picker/constants/index.js";
import { useDateRangePicker as se } from "./hooks/useDateRangePicker.js";
import { DATE_RANGE_PRESETS as x } from "./utils/dateRangePresetHelpers.js";
import pe from "../date-picker/utils/cloneTriggerElement.js";
import { Popover as ue, PopoverAnchor as de, PopoverContent as ce } from "../popover/PopoverPrimitive.js";
import fe from "../input-group/InputGroup.js";
import me from "../input-group/components/input-group-input/InputGroupInput.js";
import he from "../input-group/components/input-group-addon/InputGroupAddon.js";
import ge from "../input-group/components/input-group-icon/InputGroupIcon.js";
import Ie from "./components/date-range-picker-dropdown/DateRangePickerDropdown.js";
import Ce from "../../utils/pickFieldAriaProps.js";
const De = (c) => {
  const {
    isDisabled: t = !1,
    id: g,
    disableDate: b,
    format: i = ie,
    placeholder: O,
    header: w,
    hasInput: s = !1,
    triggerContent: f,
    footer: B,
    captionLayout: G = "dropdown",
    fromYear: _,
    toYear: N,
    isError: V,
    size: K = "default"
  } = c, {
    isOpen: p,
    setIsOpen: r,
    selectedPreset: M,
    inputValue: j,
    parsedFromDate: I,
    parsedToDate: C,
    parsedFromDateInput: D,
    parsedToDateInput: P,
    defaultMonth: L,
    fromInputValue: S,
    toInputValue: Y,
    handlePresetClick: $,
    handleCalendarChange: z,
    handleInputChange: U,
    handleInputBlur: q,
    handleInputFocus: H,
    handleFromInputChange: J,
    handleToInputChange: Q,
    handleFromInputBlur: W,
    handleToInputBlur: X,
    handleClear: Z,
    handleApply: m,
    handleResetToPropValue: a
  } = se(c, i), u = F(null), ee = y(() => {
    const e = s ? D : I, n = s ? P : C;
    if (!(!e && !n))
      return { from: e ?? void 0, to: n ?? void 0 };
  }, [s, I, C, D, P]), v = F(null), ne = Ce(c), re = l(
    (e) => {
      var n;
      e.key === "Enter" && (e.preventDefault(), m()), e.key === "Escape" && (r(!1), (n = v.current) == null || n.blur());
    },
    [m, r]
  ), d = l(() => {
    t || r((e) => !e);
  }, [t, r]), oe = l(
    (e) => {
      var k, E;
      const n = e.relatedTarget;
      (k = n == null ? void 0 : n.closest) != null && k.call(n, "[data-radix-popper-content-wrapper]") || (E = u.current) != null && E.contains(n) || (a(), r(!1));
    },
    [a, r]
  ), te = l(
    (e) => {
      if (e.key === "Escape") {
        r(!1);
        return;
      }
      (e.key === "Enter" || e.key === " ") && (e.key === " " && e.preventDefault(), d());
    },
    [d, r]
  ), T = y(() => x.findIndex((e) => !e.getValue), []), ae = pe(f, {
    id: g,
    defaultId: A,
    isOpen: p,
    isDisabled: t,
    onTriggerClick: d,
    onEscape: () => r(!1)
  }), le = l(
    (e) => {
      e || (a(), r(!1));
    },
    [a, r]
  );
  return /* @__PURE__ */ R(ue, { open: p, onOpenChange: le, children: [
    /* @__PURE__ */ o(de, { asChild: !0, children: /* @__PURE__ */ o("div", { ref: u, className: "flex relative", onBlur: oe, children: f ? ae || /* Non-element custom trigger wrapper — no visual styling, user owns the appearance */
    /* @__PURE__ */ o(
      "div",
      {
        onClick: d,
        onKeyDown: te,
        role: "button",
        tabIndex: t ? -1 : 0,
        "aria-expanded": p,
        "aria-haspopup": "dialog",
        "aria-disabled": t,
        className: "group/date-filter w-fit",
        children: f
      }
    ) : (
      /* Default input trigger */
      /* @__PURE__ */ R(fe, { isError: V, size: K, children: [
        /* @__PURE__ */ o(
          me,
          {
            ref: v,
            id: g || A,
            disabled: t,
            placeholder: O || `${i} - ${i}`,
            value: j,
            onChange: U,
            onFocus: H,
            onBlur: q,
            onKeyDown: re,
            ...ne
          }
        ),
        /* @__PURE__ */ o(he, { align: "inline-right", children: /* @__PURE__ */ o(ge, { name: "Calendar" }) })
      ] })
    ) }) }),
    p && /* @__PURE__ */ o(
      ce,
      {
        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 h;
          const n = e.relatedTarget;
          e.currentTarget.contains(n) || (h = u.current) != null && h.contains(n) || n != null && n.closest("[data-radix-popper-content-wrapper]") || (a(), r(!1));
        },
        children: /* @__PURE__ */ o(
          Ie,
          {
            header: w,
            hasInput: s,
            footer: B,
            dateFormat: i,
            calendarValue: ee,
            defaultMonth: L,
            selectedPreset: M ?? T,
            individualPresetIndex: T,
            fromInputValue: S,
            toInputValue: Y,
            disableDate: b,
            onPresetClick: (e) => $(e, x),
            onCalendarChange: z,
            onFromInputChange: J,
            onToInputChange: Q,
            onFromInputBlur: W,
            onToInputBlur: X,
            onClear: Z,
            onApply: m,
            captionLayout: G,
            fromYear: _,
            toYear: N
          }
        )
      }
    )
  ] });
};
De.displayName = "DateRangePicker";
export {
  De as default
};
//# sourceMappingURL=DateRangePicker.js.map
