"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _spinner = _interopRequireDefault(require("../../atoms/spinner/spinner"));
var _box = _interopRequireDefault(require("../../atoms/box/box"));
var _text = _interopRequireDefault(require("../../atoms/text/text"));
var _pressable = _interopRequireDefault(require("../../atoms/pressable/pressable"));
var _buttonGroup = require("./button-group");
var _useMolecularComponentConfig = require("../../../hooks/useMolecularComponentConfig");
var _styleFunctions = require("../../../theme/src/style-functions");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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); }
/** Button is used to trigger an action or event, such as submitting a form, opening a dialog, canceling an action, or performing a delete operation */
const Button = /*#__PURE__*/_react.default.memo( /*#__PURE__*/_react.default.forwardRef(({
  children,
  loadingText = undefined,
  spinnerPlacement = "start",
  isLoading = false,
  isFullWidth = false,
  leftIcon = undefined,
  rightIcon = undefined,
  ...rest
}, ref) => {
  const {
    size,
    variant,
    isDisabled,
    colorScheme
  } = (0, _buttonGroup.useButtonGroup)();

  // Overwrite props from checkbox group
  rest.size = rest.size ?? size;
  rest.variant = rest.variant ?? variant;
  rest.isDisabled = rest.isDisabled ?? isDisabled;
  rest.colorScheme = rest.colorScheme ?? colorScheme;
  const molecularProps = (0, _useMolecularComponentConfig.useMolecularComponentConfig)("Button", rest, {
    size: rest.size,
    variant: rest.variant
  }, rest.colorScheme, _styleFunctions.boxStyleFunctions, "pressable", "pressable", "pressable");
  const {
    atoms
  } = molecularProps;

  // Determine if the button is disabled
  const isButtonDisabled = rest.isDisabled ? true : isLoading;

  // Function to render the loading status of the button
  const renderLoadingStatus = () => {
    // If there is loading text, render the spinner and the loading text
    if (loadingText) {
      return /*#__PURE__*/_react.default.createElement(_box.default, {
        alignItems: "center",
        flexDirection: "row"
      }, spinnerPlacement === "start" ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_spinner.default, atoms.spinner), /*#__PURE__*/_react.default.createElement(_text.default, _extends({
        marginLeft: atoms.pressable.py
      }, atoms.text), loadingText)) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_text.default, _extends({
        marginRight: atoms.pressable.py
      }, atoms.text), loadingText), /*#__PURE__*/_react.default.createElement(_spinner.default, atoms.spinner)));
    } else {
      // If there is no loading text, render the spinner and the children with transparent color
      return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_spinner.default, _extends({
        isExpanded: true
      }, atoms.spinner)), /*#__PURE__*/_react.default.createElement(_text.default, _extends({}, atoms.text, {
        color: "transparent"
      }), children));
    }
  };

  // Function to render the main content of the button
  const renderMainContent = () => {
    // If there are left or right icons, render them along with the children
    if (leftIcon || rightIcon) {
      return /*#__PURE__*/_react.default.createElement(_box.default, {
        flexDirection: "row"
      }, leftIcon ? /*#__PURE__*/_react.default.cloneElement(leftIcon, {
        ...atoms.icon,
        marginLeft: 0,
        marginRight: atoms.icon.mr ?? atoms.icon.marginRight ?? atoms.icon.mx ?? atoms.icon.marginHorizontal ?? atoms.icon.m ?? atoms.icon.margin,
        ...leftIcon.props
      }) : null, /*#__PURE__*/_react.default.createElement(_text.default, atoms.text, children), rightIcon ? /*#__PURE__*/_react.default.cloneElement(rightIcon, {
        ...atoms.icon,
        marginRight: 0,
        marginLeft: atoms.icon.ml ?? atoms.icon.marginLeft ?? atoms.icon.mx ?? atoms.icon.marginHorizontal ?? atoms.icon.m ?? atoms.icon.margin,
        ...rightIcon.props
      }) : null);
    } else {
      // If there are no icons, render only the children
      return /*#__PURE__*/_react.default.createElement(_text.default, atoms.text, children);
    }
  };
  return /*#__PURE__*/_react.default.createElement(_pressable.default, _extends({}, atoms.pressable, {
    ref: ref,
    isDisabled: isButtonDisabled,
    width: isFullWidth ? "100%" : undefined,
    alignSelf: isFullWidth ? "stretch" : "flex-start",
    accessibilityLabel: !isLoading ? atoms.pressable.accessibilityLabel ? atoms.pressable.accessibilityLabel : children : "Loading",
    accessibilityState: {
      disabled: isButtonDisabled,
      busy: isLoading
    }
  }), isLoading ? renderLoadingStatus() : renderMainContent());
}));
Button.displayName = "Button";
var _default = exports.default = Button;
//# sourceMappingURL=button.js.map