"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useControllableState = useControllableState;
var _react = require("react");
/**
 * Canonical Radix/shadcn-style controllable state hook. Returns a tuple of the
 * current value and a setter that:
 *   - updates internal state when uncontrolled, and
 *   - always calls `onChange` (so controlled parents stay in sync).
 *
 * Switching between controlled and uncontrolled at runtime is supported but
 * discouraged; the hook keeps the latest `value` for reads either way.
 */
function useControllableState({
  value,
  defaultValue,
  onChange
}) {
  const [internal, setInternal] = (0, _react.useState)(defaultValue);
  const isControlled = value !== undefined;
  const current = isControlled ? value : internal;
  const onChangeRef = (0, _react.useRef)(onChange);
  onChangeRef.current = onChange;
  const setValue = (0, _react.useCallback)(next => {
    if (!isControlled) {
      setInternal(next);
    }
    onChangeRef.current?.(next);
  }, [isControlled]);
  return [current, setValue];
}
//# sourceMappingURL=useControllableState.js.map