import { jsx as r, jsxs as f, Fragment as s } from "react/jsx-runtime";
import { ab as R, ac as E, ad as O, z as d, ae as j, af as k, B as z, ag as l, ah as B, ai as F } from "./index-C62tPk5p.js";
import { useState as I, useMemo as P } from "react";
function S({
  startDate: a,
  endDate: e,
  clickedDate: n
}) {
  return !a || e ? { startDate: n, endDate: void 0 } : n < a ? { startDate: n, endDate: a } : { startDate: a, endDate: n };
}
function A({
  className: a,
  dates: e,
  minDate: n,
  maxDate: m,
  onDatesChange: h,
  disabled: b,
  withoutPopover: v,
  popoverModal: p = !0,
  popoverOpen: g,
  onPopoverOpenChange: $,
  "data-testid": y,
  strictlyBottom: D,
  allowPastDates: L = !1
}) {
  const [o, i] = I(void 0);
  function x(t) {
    const w = S({
      startDate: e.startDate,
      endDate: e.endDate,
      clickedDate: t
    });
    h(w), i(void 0);
  }
  function M(t) {
    e.startDate && !e.endDate && i(t);
  }
  function C() {
    i(void 0);
  }
  const N = L && n === null ? void 0 : n ?? /* @__PURE__ */ new Date(), c = P(() => {
    if (!(!e.startDate || e.endDate || !o) && !R(e.startDate, o))
      return E(o, e.startDate) ? { from: o, to: e.startDate } : { from: e.startDate, to: o };
  }, [e.startDate, e.endDate, o]), u = /* @__PURE__ */ r(
    O,
    {
      initialFocus: !0,
      fromDate: N,
      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: (t) => x(t),
      onDayMouseEnter: M,
      onDayMouseLeave: C,
      numberOfMonths: 1,
      disabled: b,
      "data-testid": y,
      modifiers: c ? { hoverRange: c } : void 0,
      modifiersClassNames: c ? { hoverRange: "bg-emphasis" } : void 0
    }
  );
  return v ? u : /* @__PURE__ */ r("div", { className: d("grid gap-2", a), children: /* @__PURE__ */ f(j, { modal: p, open: g, onOpenChange: $, children: [
    /* @__PURE__ */ r(k, { asChild: !0, children: /* @__PURE__ */ r(
      z,
      {
        "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(B, { children: /* @__PURE__ */ r(
      F,
      {
        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: (t) => {
          e != null && e.startDate && !(e != null && e.endDate) && t.preventDefault();
        },
        children: u
      }
    ) })
  ] }) });
}
export {
  A as DatePickerWithRange
};
