import { forwardRef, useContext, useState } from "react";
import cs from "../_util/classNames.js";
import { isArray, isObject } from "../_util/is.js";
import omit from "../_util/omit.js";
import { ConfigContext } from "../ConfigProvider/index.js";
import IconLoading from "../../icon/react-icon/IconLoading/index.js";
import useMergeProps from "../_util/hooks/useMergeProps.js";
import { jsxs, jsx, Fragment } from "../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js";
import CSSTransition from "../../../../../../react-transition-group@4.4.5_biqbaboplfbrettd7655fr4n2y/node_modules/react-transition-group/esm/CSSTransition.js";
import SwitchTransition from "../../../../../../react-transition-group@4.4.5_biqbaboplfbrettd7655fr4n2y/node_modules/react-transition-group/esm/SwitchTransition.js";
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 defaultProps = {
  type: "circle"
};
function Switch(baseProps, ref) {
  var _a;
  var _b = useContext(ConfigContext), getPrefixCls = _b.getPrefixCls, ctxSize = _b.size, componentConfig = _b.componentConfig, rtl = _b.rtl;
  var props = useMergeProps(baseProps, defaultProps, componentConfig === null || componentConfig === void 0 ? void 0 : componentConfig.Switch);
  var className = props.className, children = props.children, _c = props.style, style = _c === void 0 ? {} : _c, disabled = props.disabled, propSize = props.size, loading = props.loading, onChange = props.onChange, type = props.type, checkedText = props.checkedText, uncheckedText = props.uncheckedText, checkedIcon = props.checkedIcon, uncheckedIcon = props.uncheckedIcon, rest = __rest(props, ["className", "children", "style", "disabled", "size", "loading", "onChange", "type", "checkedText", "uncheckedText", "checkedIcon", "uncheckedIcon"]);
  var prefixCls = getPrefixCls("switch");
  var size = propSize || ctxSize;
  var _d = __read(useState(props.defaultChecked), 2), checked = _d[0], setChecked = _d[1];
  var mergedChecked = "checked" in props ? props.checked : checked;
  var onHandleClick = function(event) {
    if (loading) {
      return;
    }
    props.onClick && props.onClick(event);
    if (!("checked" in props)) {
      setChecked(!mergedChecked);
    }
    onChange && onChange(!mergedChecked, event);
  };
  var classNames = cs(prefixCls, size === "small" ? prefixCls + "-" + size : void 0, (_a = {}, _a[prefixCls + "-type-" + type] = type, _a[prefixCls + "-checked"] = mergedChecked, _a[prefixCls + "-loading"] = loading, _a[prefixCls + "-rtl"] = rtl, _a), className);
  var checkedElement = checkedText;
  var unCheckedElement = uncheckedText;
  if (children && isArray(children)) {
    checkedElement = children.find(function(child) {
      return child.key === "open";
    });
    unCheckedElement = children.find(function(child) {
      return child.key === "close";
    });
  } else if (children && isObject(children)) {
    if (children.key === "open") {
      checkedElement = children;
    } else if (children.key === "close") {
      unCheckedElement = children;
    }
  }
  var extraProps = omit(rest, ["onChange", "checked", "error"]);
  return /* @__PURE__ */ jsxs("button", {
    ...__assign({
      ref,
      role: "switch",
      "aria-checked": !!mergedChecked
    }, extraProps, {
      style,
      className: classNames,
      disabled,
      onClick: onHandleClick,
      type: "button"
    }),
    children: [/* @__PURE__ */ jsxs("div", {
      className: prefixCls + "-dot",
      children: [!loading && (checkedIcon || uncheckedIcon) && /* @__PURE__ */ jsx(SwitchTransition, {
        children: /* @__PURE__ */ jsx(CSSTransition, {
          classNames: "fadeIn",
          timeout: 200,
          children: /* @__PURE__ */ jsx("span", {
            className: prefixCls + "-dot-icon",
            children: mergedChecked ? checkedIcon : uncheckedIcon
          })
        }, mergedChecked ? "checked" : "unchecked")
      }), loading && /* @__PURE__ */ jsx("span", {
        className: prefixCls + "-dot-icon",
        children: /* @__PURE__ */ jsx(IconLoading, {})
      })]
    }), size !== "small" && type !== "line" && (checkedElement || unCheckedElement) && /* @__PURE__ */ jsxs(Fragment, {
      children: [/* @__PURE__ */ jsxs("div", {
        className: prefixCls + "-text-holder",
        children: [checkedElement && mergedChecked && checkedElement, unCheckedElement && !mergedChecked && unCheckedElement]
      }), /* @__PURE__ */ jsx(CSSTransition, {
        in: mergedChecked,
        classNames: "switchSlideText",
        timeout: 200,
        children: /* @__PURE__ */ jsxs("div", {
          className: prefixCls + "-text",
          children: [checkedElement && mergedChecked && checkedElement, unCheckedElement && !mergedChecked && unCheckedElement]
        })
      })]
    })]
  });
}
var ForwardRefSwitch = forwardRef(Switch);
var SwitchComponent = ForwardRefSwitch;
SwitchComponent.__BYTE_SWITCH = true;
SwitchComponent.displayName = "Switch";
const ArcoSwitch = SwitchComponent;
export {
  ArcoSwitch as default
};
//# sourceMappingURL=index.js.map
