import { jsxs as N, jsx as i, Fragment as Pe } from "react/jsx-runtime";
import { useState as ee, useEffect as ne, useRef as R } from "react";
import { DayPicker as F } from "react-day-picker";
import { format as te, parse as ke, isValid as oe } from "date-fns";
import * as L from "date-fns/locale";
import { debounce as Ne } from "es-toolkit/function";
import Re from "../../hooks/useClickOutside.js";
import we from "../../hooks/usePopperDropdown.js";
import Ee from "../../hooks/useKey.js";
import xe from "../clearableInput/ClearableInput.js";
import Fe from "../../utils/classNames.js";
const Le = (e) => !e.mode || e.mode === "single", Te = (e) => e.mode === "multiple", Be = (e) => e.mode === "range", Qe = (e) => {
  const {
    id: r,
    selected: o,
    inputProps: s = {},
    alignRight: u,
    bsSize: f = "",
    locale: a = "en-GB",
    footer: S,
    inline: re,
    showOutsideDays: ie = !0,
    fixedWeeks: ae = !0,
    mode: b = "single",
    dateFormat: m,
    placeholder: O,
    isClearable: j = !1,
    closeOnSelect: K = !0,
    defaultMonth: se,
    startMonth: ce,
    endMonth: de,
    modifiers: le,
    modifiersStyles: ye,
    inputClassName: V,
    className: A
  } = e, c = b === "single", G = b === "multiple", ue = b === "range", p = o !== void 0, [fe, D] = ee(), M = p ? o : fe, C = c ? M instanceof Date ? M : void 0 : G ? Array.isArray(M) ? M : void 0 : Oe(M) ? M : void 0, me = je(a), [$, I] = ee(""), z = p && !c ? Ke(C, a, b, m) : $;
  ne(() => {
    c && p && o instanceof Date && I(T(o, a, m));
  }, [o, c, p, a, m]);
  const v = (n, t) => {
    if (Le(e) && e.onSelectChange) {
      e.onSelectChange(n, t);
      return;
    }
    if (Te(e) && e.onSelectChange) {
      e.onSelectChange(n, t);
      return;
    }
    Be(e) && e.onSelectChange && e.onSelectChange(n, t);
  }, H = (n) => U(n), pe = (n) => U(n.currentTarget.value), ge = R(
    Ne((n) => {
      const t = B(n, a);
      console.log(t);
      const l = !!t;
      if (D(t), c && "onInputChange" in e && e.onInputChange && e.onInputChange(n, t, l), t !== void 0 || n === "") {
        const De = m ? B(n, a, m) : t;
        v(De, {});
      }
    }, 300)
  ).current, U = (n) => {
    I(n), ge(n);
  }, P = R(!1), W = (n) => {
    const t = n.relatedTarget, l = t && X.current?.contains(t);
    if (d && !l && h(), !c)
      return;
    const y = B($, a, m);
    y && oe(y) && (D(y), v(y, {}), I(T(y, a, m)));
  }, _ = () => {
    d || J();
  }, Z = (n) => {
    if (n.key === "Tab" && (P.current = !0), n.key === "Enter") {
      if (!d) {
        k();
        return;
      }
      d && K && (v(C, {}), h());
    }
  }, q = (n) => {
    p || D(n);
  }, Me = () => {
    H(""), q(void 0), I(""), p && D(void 0), ue && "onChange" in e && e.onChange && e.onChange(void 0), v(void 0, {});
  }, w = (n, t, l, y) => {
    q(n), v(n, l), c && !p && I(T(n, a, m)), P.current && g?.current?.focus(), K && (h(), P.current = !1);
  }, {
    isOpen: d,
    open: J,
    close: h,
    referenceRef: Q,
    popperElementRef: he,
    popperStyles: Ce,
    popperAttributes: Ie
  } = we({ placement: u ? "bottom-end" : "bottom-start" }), g = R(null), X = R(null), ve = (n) => {
    X.current = n, he(n);
  }, E = Re(
    (n) => {
      const t = n.target;
      if (!t) {
        h();
        return;
      }
      const l = Q.current?.contains(t), y = g.current?.contains(t);
      l || y || h();
    },
    void 0,
    d
  ), k = () => {
    J(), g.current?.focus();
  };
  Ee("Escape", (n) => {
    const t = n.target;
    g.current && g.current === t && d && h();
  }), ne(() => {
    const n = (t) => {
      !d || !E.current || t.key === "Tab" && E.current.contains(document.activeElement) && (P.current = !0, console.log("User tabbed into dropdown"));
    };
    return document.addEventListener("keydown", n), () => {
      document.removeEventListener("keydown", n);
    };
  }, [d]);
  const Se = `DayPicker ${f === "sm" ? "margin-10" : "margin-15"} ${f}`, x = {
    defaultMonth: se,
    locale: me,
    captionLayout: "dropdown",
    animate: !0,
    navLayout: "around",
    startMonth: ce,
    endMonth: de,
    modifiers: le,
    modifiersStyles: ye,
    showOutsideDays: ie,
    fixedWeeks: ae,
    className: Se
  }, Y = () => c ? /* @__PURE__ */ i(
    F,
    {
      ...x,
      mode: "single",
      selected: C,
      onSelect: w
    }
  ) : G ? /* @__PURE__ */ i(
    F,
    {
      ...x,
      mode: "multiple",
      selected: C,
      onSelect: w
    }
  ) : /* @__PURE__ */ i(
    F,
    {
      ...x,
      mode: "range",
      selected: C,
      onSelect: w
    }
  );
  if (re)
    return /* @__PURE__ */ N("div", { className: `display-flex flex-column justify-content-between ${A}`, children: [
      Y(),
      S ? /* @__PURE__ */ i("div", { className: "padding-15", children: S }) : /* @__PURE__ */ i("div", {})
    ] });
  const be = {
    ...s,
    readOnly: !c,
    style: {
      background: "var(--color-white)",
      ...s?.style ?? {}
    }
  };
  return /* @__PURE__ */ N(Pe, { children: [
    /* @__PURE__ */ i("div", { ref: Q, className: `form-group ${A}`, children: /* @__PURE__ */ N("div", { className: "input-group", children: [
      /* @__PURE__ */ i("span", { className: "input-group-addon", onClick: k, children: /* @__PURE__ */ i("span", { className: "rioglyph rioglyph-calendar", "aria-hidden": "true" }) }),
      j && /* @__PURE__ */ i(
        xe,
        {
          id: r,
          autoComplete: "off",
          placeholder: O,
          ...be,
          type: "text",
          value: z,
          onChange: H,
          onClick: k,
          onClear: Me,
          onBlur: W,
          onFocus: _,
          onKeyPress: Z,
          ref: g,
          inputClassName: V
        }
      ),
      !j && /* @__PURE__ */ i(
        "input",
        {
          id: r,
          size: 12,
          type: "text",
          autoComplete: "off",
          placeholder: O,
          ...s,
          value: z,
          onChange: pe,
          onClick: k,
          onBlur: W,
          onFocus: _,
          onKeyDown: Z,
          ref: g,
          readOnly: !c,
          className: Fe("ClearableInput form-control bg-white", V)
        }
      )
    ] }) }),
    d && /* @__PURE__ */ i(
      "div",
      {
        style: Ce,
        className: "DayPickerPopover z-index-max bg-white shadow-default rounded margin-y-3 border",
        ...Ie,
        ref: ve,
        role: "dialog",
        "aria-label": "DayPicker calendar",
        children: /* @__PURE__ */ N("div", { ref: E, className: "display-flex flex-column justify-content-between", children: [
          Y(),
          S ? /* @__PURE__ */ i("div", { className: "padding-15", children: S }) : /* @__PURE__ */ i("div", {})
        ] })
      }
    )
  ] });
}, Oe = (e) => {
  if (!e || typeof e != "object")
    return !1;
  const r = e, o = r.from instanceof Date, s = r.to === void 0 || r.to instanceof Date;
  return o && s;
}, je = (e) => {
  const r = e.split("-")[0];
  return L[r] || L[e] || L.enGB;
}, T = (e, r, o) => e ? o ? te(e, o) : new Intl.DateTimeFormat(r).format(e) : "", Ke = (e, r, o, s) => {
  if (!e)
    return "";
  const u = (f) => s ? te(f, s) : new Intl.DateTimeFormat(r).format(f);
  if (o === "single" && e instanceof Date)
    return u(e);
  if (o === "multiple" && Array.isArray(e))
    return e.map(u).join(", ");
  if (o === "range" && typeof e == "object" && "from" in e) {
    const { from: f, to: a } = e;
    return !f || !a ? "" : [f, a].filter(Boolean).map(u).join(" – ");
  }
  return "";
}, Ve = {
  "bg-BG": "dd.MM.yyyy",
  "cs-CZ": "dd.MM.yyyy",
  "da-DK": "dd-MM-yyyy",
  "de-DE": "dd.MM.yyyy",
  "el-GR": "dd/MM/yyyy",
  "en-GB": "dd/MM/yyyy",
  "es-ES": "dd/MM/yyyy",
  "et-EE": "dd.MM.yyyy",
  "fi-FI": "dd.MM.yyyy",
  "fr-FR": "dd/MM/yyyy",
  "hr-HR": "dd.MM.yyyy",
  "hu-HU": "yyyy. MM. dd.",
  "it-IT": "dd/MM/yyyy",
  "lt-LT": "dd-MM-yyyy",
  "lv-LV": "dd.MM.yyyy",
  "nb-NO": "dd.MM.yyyy",
  "nl-NL": "dd-MM-yyyy",
  "pl-PL": "dd.MM.yyyy",
  "pt-BR": "dd/MM/yyyy",
  "pt-PT": "dd/MM/yyyy",
  "ro-RO": "dd.MM.yyyy",
  "sk-SK": "dd.MM.yyyy",
  "sl-SI": "dd.MM.yyyy",
  "sv-SE": "yyyy-MM-dd"
}, Ae = (e, r = "yyyy-MM-dd") => Ve[e] || r, B = (e, r, o) => {
  if (!e)
    return;
  const s = o || Ae(r, o), u = ke(e, s, /* @__PURE__ */ new Date());
  return oe(u) ? u : void 0;
};
export {
  Qe as default
};
//# sourceMappingURL=DayPickerPrototype.js.map
