import { jsx as i, jsxs as $, Fragment as q } from "react/jsx-runtime";
import { useState as H, useRef as J, useEffect as Re } from "react";
import Ie from "react-dom";
import Ne from "../../hooks/useClickOutside.js";
import Be from "../../hooks/useKey.js";
import Me from "../../hooks/usePopperDropdown.js";
import Ve from "../../utils/classNames.js";
import { getOrCreatePortalRoot as Ee } from "../../utils/portalRoot.js";
import Fe from "./DayPickerCalendar.js";
import je from "./DayPickerDropdown.js";
import Ge from "./DayPickerInput.js";
import { isSingleDayPickerProps as L, isMultipleDayPickerProps as Q, isRangeDayPickerProps as Te } from "./dayPickerTypes.js";
import xe from "./useDayPickerInputState.js";
import "date-fns";
import "date-fns/locale";
const Ae = (e) => Object.hasOwn(e, "value"), Ke = (e) => Object.hasOwn(e, "closeOnSelect"), w = (e, o) => typeof e == "function" ? e(o) : e ?? null, nt = (e) => {
  const {
    id: o,
    name: l,
    mode: g = "single",
    locale: m = "en-GB",
    placeholder: h,
    iconName: p = "rioglyph-calendar",
    dateFormat: U,
    onBlur: X,
    inputProps: c,
    errorMessage: S,
    warningMessage: k,
    messageWhiteSpace: M = "normal",
    inputClassName: Y,
    className: Z,
    dropdownClassName: _,
    leftSidebarClassName: v,
    rightSidebarClassName: ee,
    footerClassName: te,
    usePortal: R = !1,
    alignRight: ne,
    isClearable: re = !1,
    closeOnOutsideClick: V = !0,
    selectionBehavior: I = "immediate"
  } = e, E = Ae(e), N = L(e), oe = Ke(e) ? e.closeOnSelect !== !1 : I !== "deferred", [ae, ie] = H(e.defaultValue), s = E ? e.value : ae, [y, d] = H(s), {
    isOpen: a,
    open: D,
    close: B,
    update: le,
    referenceRef: O,
    popperElementRef: ce,
    popperStyles: se,
    popperAttributes: de
  } = Me({ placement: ne ? "bottom-end" : "bottom-start" }), C = J(null), F = J(null);
  Re(() => {
    d(s);
  }, [s]);
  const ue = (t) => {
    F.current = t, ce(t);
  }, fe = (t, n) => {
    if (N) {
      e.onChange?.(t, n);
      return;
    }
    if (Q(e)) {
      e.onChange?.(t, n);
      return;
    }
    Te(e) && e.onChange?.(t, n);
  }, b = (t, n = {}) => {
    E || ie(t), d(t), fe(t, n);
  }, ge = () => {
    b(void 0, {});
  }, u = () => {
    B(), d(s);
  }, j = () => {
    b(y, {}), B();
  }, me = Ne(
    (t) => {
      if (!V)
        return;
      const n = t.target;
      if (!n) {
        u();
        return;
      }
      const f = O.current?.contains(n), P = C.current?.contains(n);
      f || P || u();
    },
    void 0,
    a
  );
  Be("Escape", (t) => {
    const n = t.target;
    if (!n || !a)
      return;
    const f = C.current && C.current === n, P = !!O.current?.contains(n);
    (f || P) && u();
  });
  const {
    displayValue: G,
    handleInputBlur: he,
    handleInputChange: pe,
    handleClearInput: T,
    handleInputFocus: Ce,
    handleInputKeyDown: be,
    inputMaskProps: Pe
  } = xe({
    props: e,
    locale: m,
    dateFormat: U,
    inputProps: c,
    onBlur: X,
    isDropdownOpen: a,
    isSingleMode: N,
    selectionBehavior: I,
    closeOnOutsideClick: V,
    committedValue: s,
    draftValue: y,
    setDraftValue: d,
    commitValue: b,
    clearValue: ge,
    openDropdown: D,
    closeDropdown: u,
    applyDraftValue: j,
    popperElementRef: F
  }), x = () => {
    D(), C.current?.focus();
  }, Se = () => {
    if (a) {
      u();
      return;
    }
    D();
  }, r = We({
    props: e,
    value: s,
    draftValue: y,
    setDraftValue: d,
    apply: j,
    clear: T,
    close: u
  }), A = r.trigger ? w(r.trigger, {
    ...r.renderProps,
    id: o,
    name: l,
    displayValue: G,
    isOpen: a,
    open: D,
    toggle: Se
  }) : null, ke = (t, n) => {
    if (n.disabled || n.hidden)
      return;
    const f = I === "immediate";
    f ? b(t, n) : d(t);
    const P = g !== "range" || !!t?.from && !!t?.to;
    oe && P && (f || b(t, n), B());
  }, K = !!(S || k), ye = {
    ...c,
    name: l || c?.name,
    autoComplete: c?.autoComplete || "off",
    placeholder: h || c?.placeholder
  }, De = r.leftSidebar ? w(r.leftSidebar, r.renderProps) : null, Oe = r.rightSidebar ? w(r.rightSidebar, r.renderProps) : null, we = r.footer ? w(r.footer, r.renderProps) : null, W = R ? Ee() : null, z = /* @__PURE__ */ i(
    je,
    {
      assignPopperElement: ue,
      popperStyles: se,
      popperAttributes: de,
      enableStackedCalendars: (e.numberOfMonths ?? 1) > 1,
      dropdownClassName: _,
      outsideRef: me,
      updatePopperPosition: le,
      leftSidebar: De,
      rightSidebar: Oe,
      footer: we,
      leftSidebarClassName: v,
      rightSidebarClassName: ee,
      footerClassName: te,
      children: /* @__PURE__ */ i(
        Fe,
        {
          ...e,
          size: "sm",
          value: y,
          onChange: ke,
          className: "margin-15"
        }
      )
    }
  );
  return /* @__PURE__ */ $(q, { children: [
    /* @__PURE__ */ i(
      "div",
      {
        className: Ve(
          "form-group",
          K && "has-feedback",
          S && "has-error",
          k && "has-warning",
          Z
        ),
        children: A ? /* @__PURE__ */ $(q, { children: [
          /* @__PURE__ */ i("div", { ref: O, className: "display-inline-block", children: A }),
          K ? /* @__PURE__ */ i("span", { className: `help-block white-space-${M}`, children: /* @__PURE__ */ i("span", { children: S || k }) }) : null
        ] }) : /* @__PURE__ */ i(
          Ge,
          {
            id: o,
            triggerRef: O,
            inputRef: C,
            displayValue: G,
            iconName: p,
            isSingleMode: N,
            isClearable: re,
            inputMaskProps: Pe,
            mergedInputProps: ye,
            inputClassName: Y,
            errorMessage: S,
            warningMessage: k,
            messageWhiteSpace: M,
            onChange: pe,
            onAddonClick: x,
            onClick: (t) => {
              c?.onClick?.(t), x();
            },
            onFocus: Ce,
            onBlur: he,
            onKeyDown: be,
            onClear: T
          }
        )
      }
    ),
    a && R && W && Ie.createPortal(z, W),
    a && !R && z
  ] });
}, We = ({
  props: e,
  value: o,
  draftValue: l,
  setDraftValue: g,
  apply: m,
  clear: h,
  close: p
}) => L(e) ? {
  trigger: e.trigger,
  leftSidebar: e.leftSidebar,
  rightSidebar: e.rightSidebar,
  footer: e.footer,
  renderProps: {
    value: o,
    draftValue: l,
    setDraftValue: g,
    apply: m,
    clear: h,
    close: p,
    mode: e.mode || "single",
    locale: e.locale || "en-GB"
  }
} : Q(e) ? {
  trigger: e.trigger,
  leftSidebar: e.leftSidebar,
  rightSidebar: e.rightSidebar,
  footer: e.footer,
  renderProps: {
    value: o,
    draftValue: l,
    setDraftValue: g,
    apply: m,
    clear: h,
    close: p,
    mode: e.mode,
    locale: e.locale || "en-GB"
  }
} : {
  trigger: e.trigger,
  leftSidebar: e.leftSidebar,
  rightSidebar: e.rightSidebar,
  footer: e.footer,
  renderProps: {
    value: o,
    draftValue: l,
    setDraftValue: g,
    apply: m,
    clear: h,
    close: p,
    mode: e.mode,
    locale: e.locale || "en-GB"
  }
};
export {
  Fe as DayPickerCalendar,
  nt as default
};
//# sourceMappingURL=DayPicker.js.map
