import { useRef as o, useCallback as l, useEffect as s, useState as R } from "react";
import { COMMAND_DELAY_DEFAULT as N, COMMAND_VARIANT as k } from "../constants/index.js";
import C from "../../../hooks/useControlledState.js";
function F(u, t) {
  t === void 0 && (t = 0);
  var r = o(!1), e = o(), c = o(u), n = l(function() {
    return r.current;
  }, []), i = l(function() {
    r.current = !1, e.current && clearTimeout(e.current), e.current = setTimeout(function() {
      r.current = !0, c.current();
    }, t);
  }, [t]), a = l(function() {
    r.current = null, e.current && clearTimeout(e.current);
  }, []);
  return s(function() {
    c.current = u;
  }, [u]), s(function() {
    return i(), a;
  }, [t]), [n, a, i];
}
function I(u, t, r) {
  t === void 0 && (t = 0), r === void 0 && (r = []);
  var e = F(u, t), c = e[0], n = e[1], i = e[2];
  return s(i, r), [c, n];
}
const H = ({
  value: u,
  defaultValue: t,
  onValueChange: r,
  variant: e,
  showLoadingOnType: c,
  onDebouncedValueChange: n,
  delay: i = N,
  isPopover: a,
  ref: A
}) => {
  const d = o(null), v = o(null), p = A ?? d, [S, E] = R(!1), [M, m] = R(!1), O = e === k.CHECKBOX ? void 0 : u, { value: f, setValue: _ } = C(O, t ?? "", r);
  return I(
    () => {
      c && m(!1), n == null || n(f);
    },
    i,
    [f, n]
  ), s(() => {
    if (!a) return;
    const y = setTimeout(() => {
      var T;
      (T = v.current) == null || T.focus();
    }, 50);
    return () => clearTimeout(y);
  }, [a]), {
    open: S,
    setOpen: E,
    isPending: M,
    setIsPending: m,
    valueState: f,
    setValueState: _,
    availableRef: p,
    inputRef: v
  };
};
export {
  H as useCommandState
};
//# sourceMappingURL=useCommandState.js.map
