import { useState, useCallback } from 'react';

function useOptionallyControlledState({
  controlledValue,
  defaultValue,
  onChange
}) {
  const [internalValue, setInternalValue] = useState(
    defaultValue
  );
  const isControlled = controlledValue !== void 0;
  const value = isControlled ? controlledValue : internalValue;
  const setValue = useCallback(
    (newValue) => {
      if (!isControlled) {
        setInternalValue(newValue);
      }
      if (typeof newValue === "function") {
        onChange?.(
          newValue(value) ?? value
        );
      } else {
        onChange?.(newValue);
      }
    },
    [isControlled, onChange, value]
  );
  return [value, setValue];
}

export { useOptionallyControlledState as u };
//# sourceMappingURL=useOptionallyControlledState-DbDuos5L.js.map
