import { j as a } from "../../_virtual/jsx-runtime.js";
import { useState as s, useRef as Je, useEffect as v, useMemo as me } from "react";
import { usePopper as Be } from "../../node_modules/react-popper/lib/esm/usePopper.js";
import { useOnClickOutside as Ge } from "../../hooks/useClickOutside.js";
import Ue from "../../icons/AlertCircle.js";
import { getId as he } from "../../utils/index.js";
import { LabelWrapper as qe, Label as ze, SupportingText as Ke, ErrorMessage as Qe, AssistMessage as Xe } from "../Field/styles.js";
import { ActionButtons as Ze } from "./components/actionButtons/index.js";
import { Calendar as ue } from "./components/calendar/index.js";
import { getNextMonth as fe, getNextYear as $e } from "./components/calendar/utils.js";
import { DatePickerNarrow as pe } from "./components/datePickerNarrow/index.js";
import { Header as et } from "./components/header/index.js";
import { Input as tt } from "./components/input/index.js";
import { Selectors as at, SelectorsOld as nt } from "./components/selectors/index.js";
import { getDaysInMonth as rt, getIsSameDay as lt, YEARS_TO_DISPLAY as ge } from "./dateUtils.js";
import { DatePickerWrapper as st, Wrapper as it, CalendarsWrapper as ct } from "./styles.js";
import { CALENDAR_HEADER_HEIGHT as ot, getSelectors as xe, shouldUseSelectorsProp as dt, getCurrentMonth as mt, CommonSelectorValue as A, prevMonth as ht, year as ut, commonDateSelectors as ft, JS_MONTHS as y, popperOptions as gt } from "./utils.js";
import { MonthAndYearSelectionHeader as xt } from "./components/monthAndYearSelectionHeader/index.js";
import { Month as St } from "./components/month/index.js";
import { Year as Dt } from "./components/year/index.js";
function Nt(e) {
  var le, se, ie;
  const {
    variant: z,
    disabled: C = !1,
    selection: m = "single-date",
    dismissText: K = "Dismiss",
    confirmText: Q = "Confirm",
    months: X = 1,
    startingDay: P = "sunday",
    placeholder: Se = "Select Date",
    narrow: Z = !1,
    locale: g = "en_CA",
    label: W,
    hasError: H = !1,
    errorText: $,
    assistText: L,
    onBlur: V,
    touched: N,
    leadingIcon: De,
    getInputLabel: ye,
    "data-testid": Ce,
    "data-trackid": je,
    monthAndYearSelection: ve = !1
  } = e, p = /* @__PURE__ */ new Date(), l = he(Ce), Me = he(je), [O, _] = s(), [d, w] = s(), [i, x] = s(
    null
  ), [c, h] = s(p.getFullYear()), [n, u] = s(0), Re = fe(n), [f, J] = s(!1), [Ee, Ye] = s(
    null
  ), [S, be] = s(null), { containerRef: we } = Ge(U), { styles: Ie, attributes: Fe } = Be(
    Ee,
    S,
    gt
  ), [I, Te] = s("primary"), [ee, M] = s(0), [ke, Ae] = s(0), R = Je(null), [B, Pe] = s(!1), [E, F] = s("calendar"), [te, We] = s(0), [ae, He] = s(0);
  v(() => {
    E === "calendar" && (We((S == null ? void 0 : S.offsetWidth) ?? 0), He(
      S ? S.offsetHeight - ot : 0
    ));
  }, [S]);
  const j = me(
    () => e.selectors ? xe(e.selectors) : xe(ft),
    [(le = e.selectors) == null ? void 0 : le.length]
  ), Y = me(
    () => e.showSelectors || dt(j),
    [j, e.showSelectors]
  );
  v(() => {
    Pe(typeof N > "u" ? H : H && N);
  }, [H, N]), v(() => {
    f || F("calendar");
    const t = mt(p, e.selectedRange);
    u(t), e.selectedRange && h(
      (e.selectedRange instanceof Date ? e.selectedRange : e.selectedRange.start).getFullYear()
    ), R != null && R.current && (f || (R.current.blur(), V == null || V()));
  }, [f]), v(() => {
    Y && w(
      j.find(
        (t) => t.value === A.custom
      )
    );
  }, [Y]), v(() => {
    var t;
    if (Y && d && d.value !== A.custom) {
      const { dateRange: o } = d;
      if (o) {
        const D = o instanceof Date ? { start: o, end: o } : { start: o.start, end: o.end };
        x(D), u(D.start.getMonth()), h(D.start.getFullYear()), (t = e.onSelect) == null || t.call(e, D), d.value === A.lastMonth && (u(ht), h(ut));
      }
    }
  }, [d]), v(() => {
    M(rt(n, c));
  }, [n]);
  function b() {
    n === y.December ? (u(0), h(c + 1)) : u(n + 1);
  }
  function Le() {
    h(c - ge);
  }
  function Ve() {
    h(c + ge);
  }
  function T() {
    n <= y.January ? (h(c - 1), u(y.December)) : u(n - 1);
  }
  function G(t) {
    var o, D, ce, oe, de;
    if (w(
      j.find(
        (r) => r.value === A.custom
      )
    ), m === "single-date" && ((t.getMonth() > n && t.getFullYear() >= c || t.getMonth() === y.January && n === y.December) && b(), (t.getMonth() < n && t.getFullYear() <= c || n === y.January && t.getMonth() === y.December) && T()), e.selection === "range") {
      if (!i) {
        const r = { start: t, end: t };
        x(r), (o = e.onSelect) == null || o.call(e, r);
      }
      if (i) {
        if (t < i.start) {
          const r = { start: t, end: i.start };
          x(r), (D = e.onSelect) == null || D.call(e, r);
        }
        if (t >= i.start) {
          const r = { ...i, end: t };
          x(r), (ce = e.onSelect) == null || ce.call(e, r);
        }
        if (!lt(i.start, i.end)) {
          const r = { start: t, end: t };
          x(r), (oe = e.onSelect) == null || oe.call(e, r);
        }
      }
    } else {
      const r = { start: t, end: t };
      x(r), (de = e.onSelect) == null || de.call(e, r);
    }
  }
  function Ne() {
    C || J(!0);
  }
  function ne() {
    i && e.onChange(i), J(!1), _(void 0);
  }
  function U() {
    x(null), J(!1), O && (w(O), _(void 0));
  }
  function q(t) {
    w(t), O || _(d);
  }
  function k() {
    return e.selectedRange instanceof Date ? { start: e.selectedRange, end: e.selectedRange } : e.selectedRange;
  }
  function re(t, o) {
    o === "primary" && t === "secondary" ? M(0) : o === "secondary" && t === "primary" && M(ke - 1), Te(t);
  }
  const Oe = (t) => {
    F("calendar"), u(t);
  }, _e = (t) => {
    F("calendar"), h(t);
  };
  return /* @__PURE__ */ a.jsx(
    st,
    {
      "data-testid": l(),
      "data-trackid": Me(),
      children: e.narrow ? /* @__PURE__ */ a.jsx(
        pe,
        {
          ...e,
          disabled: C,
          selectors: j,
          showSelectors: !!e.showSelectors,
          selection: e.selection ?? "single-date",
          selectedRange: i ?? k(),
          customSelectors: e.customSelectors,
          defaultSelectorValues: e.defaultSelectorValues,
          selectedSelector: d,
          variant: z,
          month: n,
          year: c,
          startingDay: P,
          locale: g,
          handleChange: G,
          dismissText: K,
          confirmText: Q,
          onConfirm: ne,
          onDismiss: U,
          onSelectorClick: q,
          eventDates: e.eventDates,
          minDate: e.minDate,
          maxDate: e.maxDate,
          placeholder: e.placeholder,
          label: e.label
        }
      ) : /* @__PURE__ */ a.jsxs("div", { ref: we, children: [
        /* @__PURE__ */ a.jsxs("div", { ref: Ye, children: [
          W && /* @__PURE__ */ a.jsx(qe, { disabled: C, hasLabel: !!W, children: /* @__PURE__ */ a.jsx(ze, { "data-testid": l("label"), disabled: C, children: W }) }),
          /* @__PURE__ */ a.jsx("div", { ref: R, "data-testid": l("wrapper"), children: /* @__PURE__ */ a.jsx(
            tt,
            {
              value: k(),
              variant: z,
              placeholder: Se,
              visible: f,
              disabled: C,
              locale: g,
              handleClick: Ne,
              "data-testid": l("input"),
              hasError: B,
              leadingIcon: De,
              getInputLabel: ye
            }
          ) }),
          /* @__PURE__ */ a.jsxs(Ke, { children: [
            !!$ && B && !C && /* @__PURE__ */ a.jsxs(Qe, { "data-testid": l("error"), children: [
              /* @__PURE__ */ a.jsx(Ue, { width: 20, height: 20 }),
              $
            ] }),
            !B && (typeof L == "string" ? /* @__PURE__ */ a.jsx(Xe, { "data-testid": l("message"), children: L }) : L)
          ] })
        ] }),
        f && /* @__PURE__ */ a.jsxs(
          it,
          {
            isVisible: f,
            role: "dialog",
            "aria-modal": "true",
            "data-testid": l("dialog"),
            ref: be,
            style: Ie.popper,
            ...Fe.popper,
            children: [
              e.selectors && m === "range" && d && /* @__PURE__ */ a.jsx(
                at,
                {
                  narrow: Z,
                  selectors: j,
                  selectedSelector: d,
                  handleChange: q,
                  "data-testid": l("selectors")
                }
              ),
              e.showSelectors && m === "range" && d && /* @__PURE__ */ a.jsx(
                nt,
                {
                  narrow: Z,
                  defaultSelectorValues: e.defaultSelectorValues,
                  customSelectors: e.customSelectors,
                  selectedSelector: d,
                  handleChange: q,
                  "data-testid": l("selectors")
                }
              ),
              /* @__PURE__ */ a.jsxs("div", { children: [
                ve && m !== "range" ? /* @__PURE__ */ a.jsx(
                  xt,
                  {
                    currentMonth: n,
                    currentYear: c,
                    locale: g,
                    viewType: E,
                    handleNextYear: Ve,
                    handlePrevYear: Le,
                    handleNextMonth: b,
                    handlePrevMonth: T,
                    updateViewType: (t) => F(t),
                    "data-testid": l(
                      "month-and-year-selection-header"
                    ),
                    minDate: e.minDate,
                    maxDate: e.maxDate
                  }
                ) : /* @__PURE__ */ a.jsx(
                  et,
                  {
                    selection: m,
                    currentMonth: n,
                    nextMonth: Re,
                    currentYear: c,
                    months: X,
                    locale: g,
                    handleNextMonth: b,
                    handlePrevMonth: T,
                    "data-testid": l("header")
                  }
                ),
                E === "year" && /* @__PURE__ */ a.jsx(
                  Dt,
                  {
                    "data-testid": l("year-selection"),
                    width: te,
                    height: ae,
                    minimumYear: (se = e.minDate) == null ? void 0 : se.getFullYear(),
                    maximumYear: (ie = e.maxDate) == null ? void 0 : ie.getFullYear(),
                    currentYear: c,
                    updateCurrentYear: _e
                  }
                ),
                E === "month" && /* @__PURE__ */ a.jsx(
                  St,
                  {
                    "data-testid": l("month-selection"),
                    width: te,
                    height: ae,
                    locale: g,
                    currentMonth: n,
                    updateCurrentMonth: Oe
                  }
                ),
                E === "calendar" && /* @__PURE__ */ a.jsxs(a.Fragment, { children: [
                  /* @__PURE__ */ a.jsxs(ct, { "data-testid": l("calendar"), children: [
                    /* @__PURE__ */ a.jsx(
                      ue,
                      {
                        value: "primary",
                        focusElementIndex: I === "primary" ? ee : null,
                        selection: m,
                        selectedRange: i || k(),
                        month: n,
                        year: c,
                        startingDay: P,
                        locale: g,
                        eventDates: e.eventDates,
                        handleChange: G,
                        minDate: e.minDate,
                        maxDate: e.maxDate,
                        showSelectors: Y,
                        visible: f,
                        handlePrevMonth: T,
                        handleNextMonth: m === "single-date" ? b : void 0,
                        hasFocus: I === "primary",
                        setCalenderWithFocus: re,
                        setFocusElementIndex: M,
                        setLength: Ae
                      }
                    ),
                    X === 2 && m === "range" && /* @__PURE__ */ a.jsx(
                      ue,
                      {
                        value: "secondary",
                        focusElementIndex: I === "secondary" ? ee : null,
                        selection: m,
                        selectedRange: i || k(),
                        month: fe(n),
                        year: $e(n, c),
                        startingDay: P,
                        locale: g,
                        eventDates: e.eventDates,
                        handleChange: G,
                        minDate: e.minDate,
                        maxDate: e.maxDate,
                        showSelectors: Y,
                        visible: f,
                        handleNextMonth: b,
                        hasFocus: I === "secondary",
                        setCalenderWithFocus: re,
                        setFocusElementIndex: M
                      }
                    )
                  ] }),
                  /* @__PURE__ */ a.jsx(
                    Ze,
                    {
                      "data-testid": l("action-buttons"),
                      dismissText: K,
                      confirmText: Q,
                      onConfirm: ne,
                      onDismiss: U
                    }
                  )
                ] })
              ] })
            ]
          }
        )
      ] })
    }
  );
}
export {
  Nt as DatePicker
};
