"use strict";
const React = require("react");
const index$1 = require("../../icon/react-icon/IconLoading/index.js");
const group = require("./group.js");
const classNames = require("../_util/classNames.js");
const index = require("../ConfigProvider/index.js");
const useMergeProps = require("../_util/hooks/useMergeProps.js");
const emotionReactJsxRuntime_browser_esm = require("../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js");
const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
const React__default = /* @__PURE__ */ _interopDefaultLegacy(React);
var __assign = globalThis && globalThis.__assign || function() {
  __assign = Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s)
        if (Object.prototype.hasOwnProperty.call(s, p))
          t[p] = s[p];
    }
    return t;
  };
  return __assign.apply(this, arguments);
};
var __rest = globalThis && globalThis.__rest || function(s, e) {
  var t = {};
  for (var p in s)
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
      t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function")
    for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
      if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
        t[p[i]] = s[p[i]];
    }
  return t;
};
var __read = globalThis && globalThis.__read || function(o, n) {
  var m = typeof Symbol === "function" && o[Symbol.iterator];
  if (!m)
    return o;
  var i = m.call(o), r, ar = [], e;
  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
      ar.push(r.value);
  } catch (error) {
    e = {
      error
    };
  } finally {
    try {
      if (r && !r.done && (m = i["return"]))
        m.call(i);
    } finally {
      if (e)
        throw e.error;
    }
  }
  return ar;
};
var regexTwoCNChar = /^[\u4e00-\u9fa5]{2}$/;
function processChildren(children) {
  var childrenList = [];
  var isPrevChildPure = false;
  React__default.default.Children.forEach(children, function(child) {
    var isCurrentChildPure = typeof child === "string" || typeof child === "number";
    if (isCurrentChildPure && isPrevChildPure) {
      var lastIndex = childrenList.length - 1;
      var lastChild = childrenList[lastIndex];
      childrenList[lastIndex] = "" + lastChild + child;
    } else {
      childrenList.push(child);
    }
    isPrevChildPure = isCurrentChildPure;
  });
  return React__default.default.Children.map(childrenList, function(child) {
    return typeof child === "string" ? /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("span", {
      children: child
    }) : child;
  });
}
var defaultProps = {
  htmlType: "button",
  type: "default",
  shape: "square"
};
function Button(baseProps, ref) {
  var _a;
  var _b = React.useContext(index.ConfigContext), getPrefixCls = _b.getPrefixCls, ctxSize = _b.size, autoInsertSpaceInButton = _b.autoInsertSpaceInButton, componentConfig = _b.componentConfig, rtl = _b.rtl;
  var props = useMergeProps(baseProps, defaultProps, componentConfig === null || componentConfig === void 0 ? void 0 : componentConfig.Button);
  var style = props.style, className = props.className, children = props.children, htmlType = props.htmlType, type = props.type, status = props.status, size = props.size, shape = props.shape, href = props.href, anchorProps = props.anchorProps, disabled = props.disabled, loading = props.loading, loadingFixedWidth = props.loadingFixedWidth, icon = props.icon, iconOnly = props.iconOnly, onClick = props.onClick, long = props.long, rest = __rest(props, ["style", "className", "children", "htmlType", "type", "status", "size", "shape", "href", "anchorProps", "disabled", "loading", "loadingFixedWidth", "icon", "iconOnly", "onClick", "long"]);
  var iconNode = loading ? /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$1, {}) : icon;
  var _c = __read(React.useState(false), 2), isTwoCNChar = _c[0], setIsTwoCNChar = _c[1];
  var innerButtonRef = React.useRef();
  var buttonRef = ref || innerButtonRef;
  React.useEffect(function() {
    if (autoInsertSpaceInButton && buttonRef && buttonRef.current) {
      var textContent = buttonRef.current.textContent;
      if (regexTwoCNChar.test(textContent)) {
        if (!isTwoCNChar) {
          setIsTwoCNChar(true);
        }
      } else if (isTwoCNChar) {
        setIsTwoCNChar(false);
      }
    }
  }, [buttonRef.current, autoInsertSpaceInButton]);
  var prefixCls = getPrefixCls("btn");
  var _type = type === "default" ? "secondary" : type;
  var classNames$1 = classNames(prefixCls, prefixCls + "-" + _type, prefixCls + "-size-" + (size || ctxSize), prefixCls + "-shape-" + shape, (_a = {}, _a[prefixCls + "-long"] = long, _a[prefixCls + "-status-" + status] = status, _a[prefixCls + "-loading-fixed-width"] = loadingFixedWidth, _a[prefixCls + "-loading"] = loading, _a[prefixCls + "-link"] = href, _a[prefixCls + "-icon-only"] = iconOnly || !children && children !== 0 && iconNode, _a[prefixCls + "-disabled"] = disabled, _a[prefixCls + "-two-chinese-chars"] = isTwoCNChar, _a[prefixCls + "-rtl"] = rtl, _a), className);
  var handleClick = function(event) {
    if (loading) {
      typeof (event === null || event === void 0 ? void 0 : event.preventDefault) === "function" && event.preventDefault();
      return;
    }
    onClick && onClick(event);
  };
  var InnerContent = /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs(emotionReactJsxRuntime_browser_esm.Fragment, {
    children: [iconNode, processChildren(children)]
  });
  if (href) {
    var _anchorProps = __assign({}, anchorProps);
    if (disabled) {
      delete _anchorProps.href;
    } else {
      _anchorProps.href = href;
    }
    return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("a", {
      ...__assign({
        ref: buttonRef
      }, rest, _anchorProps, {
        style,
        className: classNames$1,
        onClick: handleClick
      }),
      children: InnerContent
    });
  }
  return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("button", {
    ...__assign({
      ref: buttonRef
    }, rest, {
      style,
      className: classNames$1,
      type: htmlType,
      disabled,
      onClick: handleClick
    }),
    children: InnerContent
  });
}
var ForwardRefButton = React.forwardRef(Button);
var ButtonComponent = ForwardRefButton;
ButtonComponent.__BYTE_BUTTON = true;
ButtonComponent.Group = group;
ButtonComponent.displayName = "Button";
const Button$1 = ButtonComponent;
module.exports = Button$1;
//# sourceMappingURL=index.js.map
