"use strict";

import { useCallback, useRef, useState } from '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.
 */
export function useControllableState({
  value,
  defaultValue,
  onChange
}) {
  const [internal, setInternal] = useState(defaultValue);
  const isControlled = value !== undefined;
  const current = isControlled ? value : internal;
  const onChangeRef = useRef(onChange);
  onChangeRef.current = onChange;
  const setValue = useCallback(next => {
    if (!isControlled) {
      setInternal(next);
    }
    onChangeRef.current?.(next);
  }, [isControlled]);
  return [current, setValue];
}
//# sourceMappingURL=useControllableState.js.map