function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

import React, { memo, forwardRef } from 'react';
import Spinner from '../Spinner';
import { usePropsResolution } from '../../../hooks/useThemeProps';
import { default as Box } from '../Box';
import HStack from '../Stack/HStack';
import { Pressable } from '../Pressable';
import { composeEventHandlers } from '../../../utils';
import { useHover, useFocus, useIsPressed } from '../../primitives/Pressable/Pressable';
import { useHasResponsiveProps } from '../../../hooks/useHasResponsiveProps';

const Button = ({
  //@ts-ignore
  children,
  startIcon,
  rightIcon,
  leftIcon,
  endIcon,
  spinner,
  isDisabled,
  isLoading,
  spinnerPlacement = 'start',
  ...props
}, ref) => {
  var _props$accessibilityR;

  const {
    hoverProps,
    isHovered
  } = useHover();
  const {
    pressableProps,
    isPressed
  } = useIsPressed();
  const {
    focusProps,
    isFocused
  } = useFocus();
  const {
    onPressIn,
    onPressOut,
    onHoverIn,
    onHoverOut,
    onFocus,
    onBlur,
    _text,
    _stack,
    _spinner,
    isLoadingText,
    ...resolvedProps
  } = usePropsResolution('Button', props, {
    isDisabled,
    isHovered,
    isFocused,
    isPressed,
    isLoading
  }, {
    ignoreProps: ['_spinner']
  }); // const pressableProps = {
  //   ...resolvedProps,
  //   _hover,
  //   _pressed,
  //   _focus,
  //   _focusVisible,
  // };
  // let {
  //   _text,
  //   _disabled,
  //   _focus,
  //   _hover,
  //   _pressed,
  //   _focusVisible,
  //   _loading,
  //   _stack,
  //   _spinner,
  //   spinnerProps,
  //   isLoadingText,
  //   ...resolvedProps
  // } = usePropsResolution('Button', props);
  // const pressableProps = {
  //   ...resolvedProps,
  //   _hover,
  //   _pressed,
  //   _focus,
  //   _focusVisible,
  // };
  //TODO: refactor for responsive prop

  if (useHasResponsiveProps(props)) {
    return null;
  }

  if (leftIcon) {
    startIcon = leftIcon;
  }

  if (rightIcon) {
    endIcon = rightIcon;
  }

  if (endIcon && /*#__PURE__*/React.isValidElement(endIcon)) {
    endIcon = React.Children.map(endIcon, (child, index) => {
      return /*#__PURE__*/React.cloneElement(child, {
        key: "button-end-icon-".concat(index),
        ..._text,
        ...child.props
      });
    });
  }

  if (startIcon && /*#__PURE__*/React.isValidElement(startIcon)) {
    startIcon = React.Children.map(startIcon, (child, index) => {
      return /*#__PURE__*/React.cloneElement(child, {
        key: "button-start-icon-".concat(index),
        ..._text,
        ...child.props
      });
    });
  }

  const boxChildren = isLoading && isLoadingText ? isLoadingText : children;
  const spinnerElement = spinner ? spinner : /*#__PURE__*/React.createElement(Spinner, _extends({
    color: _text === null || _text === void 0 ? void 0 : _text.color
  }, _spinner));
  return /*#__PURE__*/React.createElement(Pressable, _extends({
    disabled: isDisabled || isLoading,
    ref: ref,
    onPressIn: composeEventHandlers(onPressIn, pressableProps.onPressIn),
    onPressOut: composeEventHandlers(onPressOut, pressableProps.onPressOut) // @ts-ignore - web only
    ,
    onHoverIn: composeEventHandlers(onHoverIn, hoverProps.onHoverIn) // @ts-ignore - web only
    ,
    onHoverOut: composeEventHandlers(onHoverOut, hoverProps.onHoverOut) // @ts-ignore - web only
    ,
    onFocus: composeEventHandlers(composeEventHandlers(onFocus, focusProps.onFocus) // focusRingProps.onFocus
    ) // @ts-ignore - web only
    ,
    onBlur: composeEventHandlers(composeEventHandlers(onBlur, focusProps.onBlur) // focusRingProps.onBlur
    )
  }, resolvedProps, {
    // {...pressableProps}
    // {...(isDisabled && _disabled)}
    // {...(isLoading && _loading)}
    accessibilityRole: (_props$accessibilityR = props.accessibilityRole) !== null && _props$accessibilityR !== void 0 ? _props$accessibilityR : 'button'
  }), /*#__PURE__*/React.createElement(HStack, _stack, startIcon && !isLoading ? startIcon : null, isLoading && spinnerPlacement === 'start' ? spinnerElement : null, boxChildren ? /*#__PURE__*/React.createElement(Box, {
    _text: { ..._text // ...hoverTextProps,
      // ...focusTextProps,
      // ...focusVisibleTextProps,
      // ...pressedTextProps,
      // ...loadingTextProps,
      // ...disabledTextProps,

    }
  }, isLoading && isLoadingText ? isLoadingText : children) : null, endIcon && !isLoading ? endIcon : null, isLoading && spinnerPlacement === 'end' ? spinnerElement : null));
};

export default /*#__PURE__*/memo( /*#__PURE__*/forwardRef(Button));
//# sourceMappingURL=Button.js.map