import { jsx as r, jsxs as f, Fragment as s } from "react/jsx-runtime";
import { U as C, V as N, X as w, J as d, $ as R, Y as E, B as j, Z as l, _ as O, a0 as k } from "./index-T-3isYi5.js";
import { useState as B, useMemo as F } from "react";
function I({
  startDate: o,
  endDate: e,
  clickedDate: t
}) {
  return !o || e ? { startDate: t, endDate: void 0 } : t < o ? { startDate: t, endDate: o } : { startDate: o, endDate: t };
}
function H({
  className: o,
  dates: e,
  minDate: t,
  maxDate: m,
  onDatesChange: h,
  disabled: b,
  withoutPopover: v,
  "data-testid": p,
  strictlyBottom: D,
  allowPastDates: g = !1
}) {
  const [a, i] = B(void 0);
  function $(n) {
    const M = I({
      startDate: e.startDate,
      endDate: e.endDate,
      clickedDate: n
    });
    h(M), i(void 0);
  }
  function y(n) {
    e.startDate && !e.endDate && i(n);
  }
  function L() {
    i(void 0);
  }
  const x = g && t === null ? void 0 : t ?? /* @__PURE__ */ new Date(), c = F(() => {
    if (!(!e.startDate || e.endDate || !a) && !C(e.startDate, a))
      return N(a, e.startDate) ? { from: a, to: e.startDate } : { from: e.startDate, to: a };
  }, [e.startDate, e.endDate, a]), u = /* @__PURE__ */ r(
    w,
    {
      initialFocus: !0,
      fromDate: x,
      toDate: m,
      mode: "range",
      defaultMonth: e == null ? void 0 : e.startDate,
      selected: { from: e == null ? void 0 : e.startDate, to: e == null ? void 0 : e.endDate },
      onDayClick: (n) => $(n),
      onDayMouseEnter: y,
      onDayMouseLeave: L,
      numberOfMonths: 1,
      disabled: b,
      "data-testid": p,
      modifiers: c ? { hoverRange: c } : void 0,
      modifiersClassNames: c ? { hoverRange: "bg-emphasis" } : void 0
    }
  );
  return v ? u : /* @__PURE__ */ r("div", { className: d("grid gap-2", o), children: /* @__PURE__ */ f(R, { children: [
    /* @__PURE__ */ r(E, { asChild: !0, children: /* @__PURE__ */ r(
      j,
      {
        "data-testid": "date-range",
        color: "secondary",
        EndIcon: "calendar",
        className: d("justify-between text-left font-normal", !e && "text-subtle"),
        children: e != null && e.startDate ? e != null && e.endDate ? /* @__PURE__ */ f(s, { children: [
          l(e.startDate, "LLL dd, y"),
          " - ",
          l(e.endDate, "LLL dd, y")
        ] }) : /* @__PURE__ */ f(s, { children: [
          l(e.startDate, "LLL dd, y"),
          " - End"
        ] }) : /* @__PURE__ */ r("span", { children: "Pick a date" })
      }
    ) }),
    /* @__PURE__ */ r(O, { children: /* @__PURE__ */ r(
      k,
      {
        className: "bg-default text-emphasis z-50 w-auto rounded-md border p-0 outline-none",
        align: "start",
        sideOffset: 4,
        side: D ? "bottom" : void 0,
        avoidCollisions: !D,
        onInteractOutside: (n) => {
          e != null && e.startDate && !(e != null && e.endDate) && n.preventDefault();
        },
        children: u
      }
    ) })
  ] }) });
}
export {
  H as DatePickerWithRange
};
