"use client";
'use strict';

var PFRGBC2K = require('./PFRGBC2K.cjs');
var LMDWO4NN = require('./LMDWO4NN.cjs');
var ABQUS43J = require('./ABQUS43J.cjs');
var _3YLGPPWQ = require('./3YLGPPWQ.cjs');
var DTR5TSDJ = require('../../../../../../@ariakit_core@0.4.15/node_modules/@ariakit/core/esm/__chunks/DTR5TSDJ.cjs');
var events = require('../../../../../../@ariakit_core@0.4.15/node_modules/@ariakit/core/esm/utils/events.cjs');
var PBFD2E7P = require('../../../../../../@ariakit_core@0.4.15/node_modules/@ariakit/core/esm/__chunks/PBFD2E7P.cjs');
var QAGXQEUG = require('../../../../../../@ariakit_core@0.4.15/node_modules/@ariakit/core/esm/__chunks/QAGXQEUG.cjs');
var React = require('react');

var TagName = "button";
function isNativeClick(event) {
  if (!event.isTrusted) return false;
  const element = event.currentTarget;
  if (event.key === "Enter") {
    return DTR5TSDJ.isButton(element) || element.tagName === "SUMMARY" || element.tagName === "A";
  }
  if (event.key === " ") {
    return DTR5TSDJ.isButton(element) || element.tagName === "SUMMARY" || element.tagName === "INPUT" || element.tagName === "SELECT";
  }
  return false;
}
var symbol = Symbol("command");
var useCommand = LMDWO4NN.createHook(
  function useCommand2(_a) {
    var _b = _a, { clickOnEnter = true, clickOnSpace = true } = _b, props = _3YLGPPWQ.__objRest(_b, ["clickOnEnter", "clickOnSpace"]);
    const ref = React.useRef(null);
    const [isNativeButton, setIsNativeButton] = React.useState(false);
    React.useEffect(() => {
      if (!ref.current) return;
      setIsNativeButton(DTR5TSDJ.isButton(ref.current));
    }, []);
    const [active, setActive] = React.useState(false);
    const activeRef = React.useRef(false);
    const disabled = PBFD2E7P.disabledFromProps(props);
    const [isDuplicate, metadataProps] = ABQUS43J.useMetadataProps(props, symbol, true);
    const onKeyDownProp = props.onKeyDown;
    const onKeyDown = ABQUS43J.useEvent((event) => {
      onKeyDownProp == null ? void 0 : onKeyDownProp(event);
      const element = event.currentTarget;
      if (event.defaultPrevented) return;
      if (isDuplicate) return;
      if (disabled) return;
      if (!events.isSelfTarget(event)) return;
      if (DTR5TSDJ.isTextField(element)) return;
      if (element.isContentEditable) return;
      const isEnter = clickOnEnter && event.key === "Enter";
      const isSpace = clickOnSpace && event.key === " ";
      const shouldPreventEnter = event.key === "Enter" && !clickOnEnter;
      const shouldPreventSpace = event.key === " " && !clickOnSpace;
      if (shouldPreventEnter || shouldPreventSpace) {
        event.preventDefault();
        return;
      }
      if (isEnter || isSpace) {
        const nativeClick = isNativeClick(event);
        if (isEnter) {
          if (!nativeClick) {
            event.preventDefault();
            const _a2 = event, eventInit = _3YLGPPWQ.__objRest(_a2, ["view"]);
            const click = () => events.fireClickEvent(element, eventInit);
            if (QAGXQEUG.isFirefox()) {
              events.queueBeforeEvent(element, "keyup", click);
            } else {
              queueMicrotask(click);
            }
          }
        } else if (isSpace) {
          activeRef.current = true;
          if (!nativeClick) {
            event.preventDefault();
            setActive(true);
          }
        }
      }
    });
    const onKeyUpProp = props.onKeyUp;
    const onKeyUp = ABQUS43J.useEvent((event) => {
      onKeyUpProp == null ? void 0 : onKeyUpProp(event);
      if (event.defaultPrevented) return;
      if (isDuplicate) return;
      if (disabled) return;
      if (event.metaKey) return;
      const isSpace = clickOnSpace && event.key === " ";
      if (activeRef.current && isSpace) {
        activeRef.current = false;
        if (!isNativeClick(event)) {
          event.preventDefault();
          setActive(false);
          const element = event.currentTarget;
          const _a2 = event, eventInit = _3YLGPPWQ.__objRest(_a2, ["view"]);
          queueMicrotask(() => events.fireClickEvent(element, eventInit));
        }
      }
    });
    props = _3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues(_3YLGPPWQ.__spreadValues({
      "data-active": active || void 0,
      type: isNativeButton ? "button" : void 0
    }, metadataProps), props), {
      ref: ABQUS43J.useMergeRefs(ref, props.ref),
      onKeyDown,
      onKeyUp
    });
    props = PFRGBC2K.useFocusable(props);
    return props;
  }
);
LMDWO4NN.forwardRef(function Command2(props) {
  const htmlProps = useCommand(props);
  return LMDWO4NN.createElement(TagName, htmlProps);
});

exports.useCommand = useCommand;
//# sourceMappingURL=2RSXSRCN.cjs.map
