import { useState as R, useEffect as H } from "react";
import { isSingleDayPickerProps as d, isMultipleDayPickerProps as U, isRangeDayPickerProps as V } from "./dayPickerTypes.js";
import { getInputMaskProps as W, formatSelectionForInput as C, parseDateString as X } from "./dayPickerUtils.js";
const x = ({
  props: n,
  locale: e,
  dateFormat: r,
  inputProps: f,
  onBlur: b,
  isDropdownOpen: a,
  isSingleMode: I,
  selectionBehavior: s,
  closeOnOutsideClick: j,
  committedValue: y,
  draftValue: k,
  setDraftValue: S,
  commitValue: F,
  clearValue: D,
  openDropdown: l,
  closeDropdown: E,
  applyDraftValue: m,
  popperElementRef: q
}) => {
  const [g, P] = R(""), [K, c] = R(!1);
  H(() => {
    I && c(!1);
  }, [y, I]), H(() => {
    const t = a && s === "deferred" ? k : y;
    I && K || P(_(t, n, e, r));
  }, [
    y,
    k,
    K,
    a,
    e,
    r,
    s,
    I,
    n
  ]);
  const z = (t) => {
    f?.onFocus?.(t), a || l();
  }, A = (t) => {
    b?.(t), f?.onBlur?.(t);
    const h = t.relatedTarget;
    if (!(h && q.current?.contains(h))) {
      if (d(n)) {
        const u = T(n, g, e), i = u instanceof Date;
        n.onInputChange?.(g, u, i), g === "" ? D() : i ? (c(!1), s === "immediate" ? F(u, {}) : S(u)) : c(!0);
      }
      j && E();
    }
  }, G = (t) => {
    if (f?.onKeyDown?.(t), t.key === "Enter") {
      if (t.preventDefault(), !a) {
        l();
        return;
      }
      s === "deferred" ? m() : E();
    }
  }, J = (t, h) => {
    if (P(t), !d(n))
      return;
    const u = T(n, t, e), i = u instanceof Date;
    n.onInputChange?.(t, u, i), c(t !== "" && !i), t === "" ? D() : i && s === "immediate" ? F(u, {}) : i && S(u);
  }, L = () => {
    c(!1), P(""), D();
  }, N = I ? g : _(a && s === "deferred" ? k : y, n, e, r), Q = d(n) && !n.parseInput && !n.renderValue ? W(e, r) : void 0;
  return {
    displayValue: N,
    handleInputBlur: A,
    handleInputChange: J,
    handleClearInput: L,
    handleInputFocus: z,
    handleInputKeyDown: G,
    inputMaskProps: Q
  };
}, T = (n, e, r) => {
  if (d(n))
    return n.parseInput ? n.parseInput(e, r) : X(e, r, n.dateFormat);
}, _ = (n, e, r, f) => d(e) ? e.renderValue ? e.renderValue(n, r) : C(n, r, "single", f) : U(e) ? e.renderValue ? e.renderValue(n, r) : C(n, r, "multiple", f) : V(e) ? e.renderValue ? e.renderValue(n, r) : C(n, r, "range", f) : "";
export {
  x as default
};
//# sourceMappingURL=useDayPickerInputState.js.map
