import p, { forwardRef as me, useContext as $, useMemo as fe, useState as q, Children as pe, useEffect as J, createRef as ge } from "react";
import k from "../../../classnames/index.mjs";
import be from "../../../rc-util/es/omit.mjs";
import { composeRef as ye } from "../../../rc-util/es/ref.mjs";
import { devUseWarning as ve } from "../_util/warning.mjs";
import Ce from "../_util/wave/index.mjs";
import "../config-provider/index.mjs";
import Ne from "../config-provider/DisabledContext.mjs";
import he from "../config-provider/hooks/useSize.mjs";
import { useCompactItemContext as xe } from "../space/Compact.mjs";
import Oe, { GroupSizeContext as Te } from "./button-group.mjs";
import { isUnBorderedButtonType as y, isTwoCNChar as _e, spaceChildren as Ee } from "./buttonHelpers.mjs";
import Se from "./IconWrapper.mjs";
import $e from "./LoadingIcon.mjs";
import ke from "./style/index.mjs";
import Ie from "./style/compactCmp.mjs";
import { ConfigContext as ze } from "../config-provider/context.mjs";
var je = globalThis && globalThis.__rest || function(e, l) {
  var c = {};
  for (var n in e)
    Object.prototype.hasOwnProperty.call(e, n) && l.indexOf(n) < 0 && (c[n] = e[n]);
  if (e != null && typeof Object.getOwnPropertySymbols == "function")
    for (var i = 0, n = Object.getOwnPropertySymbols(e); i < n.length; i++)
      l.indexOf(n[i]) < 0 && Object.prototype.propertyIsEnumerable.call(e, n[i]) && (c[n[i]] = e[n[i]]);
  return c;
};
function Be(e) {
  if (typeof e == "object" && e) {
    let l = e == null ? void 0 : e.delay;
    return l = !Number.isNaN(l) && typeof l == "number" ? l : 0, {
      loading: l <= 0,
      delay: l
    };
  }
  return {
    loading: !!e,
    delay: 0
  };
}
const we = (e, l) => {
  var c, n;
  const {
    loading: i = !1,
    prefixCls: K,
    type: u = "default",
    danger: Q,
    shape: C = "default",
    size: N,
    styles: h,
    disabled: x,
    className: X,
    rootClassName: Y,
    children: m,
    icon: a,
    ghost: I = !1,
    block: Z = !1,
    // React does not recognize the `htmlType` prop on a DOM element. Here we pick it out of `rest`.
    htmlType: ee = "button",
    classNames: O,
    style: te = {}
  } = e, z = je(e, ["loading", "prefixCls", "type", "danger", "shape", "size", "styles", "disabled", "className", "rootClassName", "children", "icon", "ghost", "block", "htmlType", "classNames", "style"]), {
    getPrefixCls: oe,
    autoInsertSpaceInButton: j,
    direction: B,
    button: s
  } = $(ze), t = oe("btn", K), [w, ne, se] = ke(t), le = $(Ne), g = x ?? le, ie = $(Te), b = fe(() => Be(i), [i]), [d, D] = q(b.loading), [T, R] = q(!1), f = ye(l, /* @__PURE__ */ ge()), P = pe.count(m) === 1 && !a && !y(u);
  J(() => {
    let o = null;
    b.delay > 0 ? o = setTimeout(() => {
      o = null, D(!0);
    }, b.delay) : D(b.loading);
    function r() {
      o && (clearTimeout(o), o = null);
    }
    return r;
  }, [b]), J(() => {
    if (!f || !f.current || j === !1)
      return;
    const o = f.current.textContent;
    P && _e(o) ? T || R(!0) : T && R(!1);
  }, [f]);
  const V = (o) => {
    const {
      onClick: r
    } = e;
    if (d || g) {
      o.preventDefault();
      return;
    }
    r == null || r(o);
  };
  if (process.env.NODE_ENV !== "production") {
    const o = ve("Button");
    process.env.NODE_ENV !== "production" && o(!(typeof a == "string" && a.length > 2), "breaking", `\`icon\` is using ReactNode instead of string naming in v4. Please check \`${a}\` at https://ant.design/components/icon`), process.env.NODE_ENV !== "production" && o(!(I && y(u)), "usage", "`link` or `text` button can't be a `ghost` button.");
  }
  const L = j !== !1, {
    compactSize: re,
    compactItemClassnames: G
  } = xe(t, B), ae = {
    large: "lg",
    small: "sm",
    middle: void 0
  }, U = he((o) => {
    var r, S;
    return (S = (r = N ?? re) !== null && r !== void 0 ? r : ie) !== null && S !== void 0 ? S : o;
  }), W = U && ae[U] || "", ce = d ? "loading" : a, _ = be(z, ["navigate"]), M = k(t, ne, se, {
    [`${t}-${C}`]: C !== "default" && C,
    [`${t}-${u}`]: u,
    [`${t}-${W}`]: W,
    [`${t}-icon-only`]: !m && m !== 0 && !!ce,
    [`${t}-background-ghost`]: I && !y(u),
    [`${t}-loading`]: d,
    [`${t}-two-chinese-chars`]: T && L && !d,
    [`${t}-block`]: Z,
    [`${t}-dangerous`]: !!Q,
    [`${t}-rtl`]: B === "rtl"
  }, G, X, Y, s == null ? void 0 : s.className), A = Object.assign(Object.assign({}, s == null ? void 0 : s.style), te), de = k(O == null ? void 0 : O.icon, (c = s == null ? void 0 : s.classNames) === null || c === void 0 ? void 0 : c.icon), ue = Object.assign(Object.assign({}, (h == null ? void 0 : h.icon) || {}), ((n = s == null ? void 0 : s.styles) === null || n === void 0 ? void 0 : n.icon) || {}), F = a && !d ? /* @__PURE__ */ p.createElement(Se, {
    prefixCls: t,
    className: de,
    style: ue
  }, a) : /* @__PURE__ */ p.createElement($e, {
    existIcon: !!a,
    prefixCls: t,
    loading: !!d
  }), H = m || m === 0 ? Ee(m, P && L) : null;
  if (_.href !== void 0)
    return w(/* @__PURE__ */ p.createElement("a", Object.assign({}, _, {
      className: k(M, {
        [`${t}-disabled`]: g
      }),
      href: g ? void 0 : _.href,
      style: A,
      onClick: V,
      ref: f,
      tabIndex: g ? -1 : 0
    }), F, H));
  let E = /* @__PURE__ */ p.createElement("button", Object.assign({}, z, {
    type: ee,
    className: M,
    style: A,
    onClick: V,
    disabled: g,
    ref: f
  }), F, H, G && /* @__PURE__ */ p.createElement(Ie, {
    key: "compact",
    prefixCls: t
  }));
  return y(u) || (E = /* @__PURE__ */ p.createElement(Ce, {
    component: "Button",
    disabled: !!d
  }, E)), w(E);
}, v = /* @__PURE__ */ me(we);
process.env.NODE_ENV !== "production" && (v.displayName = "Button");
v.Group = Oe;
v.__ANT_BUTTON = !0;
const Ze = v;
export {
  Ze as default
};
