import { useState as k, useRef as H, useMemo as D, useEffect as Q, useCallback as c } from "react";
import { format as r } from "date-fns";
import In from "../../date-picker/utils/parseFormatParts.js";
import U from "../utils/formatRangeValue.js";
import W from "../utils/parseRangeInput.js";
import X from "../../../utils/parseDate.js";
import pn from "../utils/getDefaultMonth.js";
import Tn from "../utils/formatRangeString.js";
import hn from "../utils/formatRangeInputAsTyping.js";
import Y from "../../date-picker/utils/formatInputAsTyping.js";
import { RANGE_SEPARATOR as vn } from "../constants/index.js";
import dn from "../utils/validateAndSwapDates.js";
const Cn = (Z, s) => {
  const { onChange: J, value: n, disableDate: u, isDisabled: K, hasInput: w = !1, isOpen: E, onOpenChange: y } = Z, [g, x] = k(!1), $ = E !== void 0, G = $ ? E : g, [V, M] = k(null), [P, m] = k(""), [S, N] = k(null), [_, O] = k(!1), [R, p] = k(""), [b, T] = k(""), I = H(!1), i = H(J);
  i.current = J;
  const f = H(!1), d = D(() => In(s), [s]);
  Q(() => {
    G || (f.current = !1, O(!1));
  }, [G]), Q(() => {
    f.current || (m(U(n, s)), p(n != null && n.from ? r(n.from, s) : ""), T(n != null && n.to ? r(n.to, s) : "")), I.current = !!(n != null && n.from && (n != null && n.to));
  }, [n, s]);
  const { parsedFromDate: j, parsedToDate: q } = D(() => W(P, s), [P, s]), a = D(() => R.length === s.length ? X(R, s) : null, [R, s]), A = D(() => b.length === s.length ? X(b, s) : null, [b, s]), F = D(() => {
    const e = w ? a || (n == null ? void 0 : n.from) : (n == null ? void 0 : n.from) || j, o = w ? A || (n == null ? void 0 : n.to) : (n == null ? void 0 : n.to) || q;
    return pn(e || null, o || null);
  }, [w, n, j, q, a, A]), h = c(
    (e) => {
      if ($) {
        const t = typeof e == "function" ? e(E ?? !1) : e;
        y == null || y(t);
      } else
        x((o) => {
          const t = typeof e == "function" ? e(o) : e;
          return y == null || y(t), t;
        });
    },
    [$, E, y]
  ), nn = c(
    (e, o) => {
      var l, v, B;
      if (!o)
        return;
      const t = (v = (l = o[e]).getValue) == null ? void 0 : v.call(l);
      M(e), (B = i.current) == null || B.call(i, t), p(t != null && t.from ? r(t.from, s) : ""), T(t != null && t.to ? r(t.to, s) : "");
    },
    [s]
  ), sn = c(
    (e, o) => {
      if (M(null), f.current = !0, !(e == null ? void 0 : e.from)) {
        N(null), m(""), p(""), T(""), O(!1), I.current = !1;
        return;
      }
      if (!_) {
        N(o), m(r(o, s)), p(r(o, s)), T(""), O(!0), I.current = !1;
        return;
      }
      if (_ && S) {
        const l = Tn(S, o, s);
        m(l);
        const v = S > o ? o : S, B = S > o ? S : o;
        p(r(v, s)), T(r(B, s)), O(!1), I.current = !0;
        return;
      }
      m(r(o, s)), p(r(o, s)), T(""), O(!0), I.current = !1;
    },
    [s, _, S]
  ), en = c(
    (e) => {
      if (f.current = !0, I.current)
        m(e);
      else {
        const o = hn(e, d);
        m(o);
      }
      M(null), f.current = !1;
    },
    [d]
  ), on = c(
    (e) => {
      if (f.current = !0, I.current)
        p(e);
      else {
        const o = Y(e, d);
        p(o);
      }
      M(null), f.current = !1;
    },
    [d]
  ), tn = c(
    (e) => {
      if (f.current = !0, I.current)
        T(e);
      else {
        const o = Y(e, d);
        T(o);
      }
      M(null), f.current = !1;
    },
    [d]
  ), rn = c(() => {
    f.current = !1;
  }, []), ln = c(() => {
    f.current = !1;
  }, []), cn = c(() => {
    f.current = !1;
  }, []), C = c(() => {
    m(n ? U(n, s) : ""), p(n != null && n.from ? r(n.from, s) : ""), T(n != null && n.to ? r(n.to, s) : ""), f.current = !1;
  }, [s, n]), un = c(() => {
    var e;
    M(null), m(""), p(""), T(""), f.current = !1, I.current = !1, O(!1), N(null), (e = i.current) == null || e.call(i, void 0);
  }, []), L = c(() => {
    var v;
    const e = a && (u == null ? void 0 : u(a)), o = A && (u == null ? void 0 : u(A));
    if (e || o) {
      C(), h(!1);
      return;
    }
    let t = a, l = A;
    t && l && t > l && ([t, l] = [l, t], p(r(t, s)), T(r(l, s))), t && l ? (m(`${r(t, s)}${vn}${r(l, s)}`), I.current = !0) : t && (m(r(t, s)), I.current = !1), (v = i.current) == null || v.call(i, { from: t || void 0, to: l || void 0 }), h(!1);
  }, [a, A, u, C, h, s]), z = c(() => {
    var B;
    const e = dn(P, s, d), { parsedFromDate: o, parsedToDate: t } = W(e, s), l = o && (u == null ? void 0 : u(o)), v = t && (u == null ? void 0 : u(t));
    if (l || v) {
      C(), h(!1);
      return;
    }
    I.current = !!(o && t), m(e), (B = i.current) == null || B.call(i, { from: o || void 0, to: t || void 0 }), h(!1);
  }, [P, s, d, u, C, h]), fn = c(() => {
    w ? !!!(R || b) && !!P ? z() : L() : z();
  }, [w, P, R, b, L, z]), mn = c(() => {
    K || h(!0);
  }, [K, h]);
  return {
    // State
    isOpen: G,
    setIsOpen: h,
    selectedPreset: V,
    // Combined trigger input value
    inputValue: P,
    defaultMonth: F,
    // Parsed dates from combined input (drive calendar when hasInput=false)
    parsedFromDate: j,
    parsedToDate: q,
    // Individual inline input values (used when hasInput=true)
    fromInputValue: R,
    toInputValue: b,
    // Parsed dates from individual inputs (drive calendar when hasInput=true)
    parsedFromDateInput: a,
    parsedToDateInput: A,
    // Handlers
    handlePresetClick: nn,
    handleCalendarChange: sn,
    handleInputChange: en,
    handleInputBlur: rn,
    handleInputFocus: mn,
    handleFromInputChange: on,
    handleToInputChange: tn,
    handleFromInputBlur: ln,
    handleToInputBlur: cn,
    handleClear: un,
    handleApply: fn,
    handleResetToPropValue: C
  };
};
export {
  Cn as useDateRangePicker
};
//# sourceMappingURL=useDateRangePicker.js.map
