import { useState as A, useRef as T, useMemo as V, useEffect as L, useCallback as m } from "react";
import { format as E } from "date-fns";
import N from "../utils/parseFormatParts.js";
import j from "../../../utils/parseDate.js";
import Q from "../utils/formatInputAsTyping.js";
import S from "../utils/formatDateInput.js";
function $({ value: e, onChange: d, disableDate: I, isOpen: a, onOpenChange: f, closeOnSelect: k = !0 }, u) {
  const [q, z] = A(!1), P = a !== void 0, B = P ? a : q, [p, n] = A(""), l = T(!1), t = T(d);
  t.current = d;
  const c = T(!1), y = V(() => N(u), [u]);
  L(() => {
    B || (c.current = !1), c.current || n(e ? E(e, u) : ""), l.current = !!e;
  }, [e, u, B]);
  const M = V(() => j(p, u), [p, u]), D = V(() => M || /* @__PURE__ */ new Date(), [M]), o = m(
    (r) => {
      if (!P)
        z((s) => {
          const i = typeof r == "function" ? r(s) : r;
          return f == null || f(i), i;
        });
      else {
        const i = typeof r == "function" ? r(a ?? !1) : r;
        f == null || f(i);
      }
    },
    [P, a, f]
  ), G = m(
    (r) => {
      var s;
      l.current = !!r, (s = t.current) == null || s.call(t, r), k && o(!1);
    },
    [k, o]
  ), H = m(
    (r) => {
      if (c.current = !0, l.current)
        n(r);
      else {
        const s = Q(r, y);
        n(s);
      }
      c.current = !1;
    },
    [y]
  ), v = m(() => {
    n(e ? E(e, u) : ""), c.current = !1;
  }, [u, e]), J = m(() => {
    var i, w;
    if (!p) {
      c.current = !1, l.current = !1, n(""), (i = t.current) == null || i.call(t, void 0), o(!1);
      return;
    }
    const r = S(p, y);
    n(r);
    const s = j(r, u);
    if (s && (I != null && I(s))) {
      v(), o(!1);
      return;
    }
    s && r.length === u.length ? (l.current = !0, (w = t.current) == null || w.call(t, s)) : l.current = !1, c.current = !1, o(!1);
  }, [p, u, y, I, v, o]), K = m(() => {
    var r;
    n(""), c.current = !1, l.current = !1, (r = t.current) == null || r.call(t, void 0);
  }, []);
  return {
    // State
    isOpen: B,
    setIsOpen: o,
    inputValue: p,
    defaultMonth: D,
    // Parsed date from input
    parsedInputDate: M,
    // Handlers
    handleCalendarChange: G,
    handleInputChange: H,
    handleInputBlur: J,
    handleClear: K
  };
}
export {
  $ as default
};
//# sourceMappingURL=useDatePicker.js.map
