"use strict";
const React = require("react");
const classNames = require("../_util/classNames.js");
const is = require("../_util/is.js");
const omit = require("../_util/omit.js");
const index = require("../ConfigProvider/index.js");
const index$1 = require("../../icon/react-icon/IconLoading/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 CSSTransition = require("../../../../../../react-transition-group@4.4.5_biqbaboplfbrettd7655fr4n2y/node_modules/react-transition-group/esm/CSSTransition.js");
const SwitchTransition = require("../../../../../../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 = React.useContext(index.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(React.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$1 = classNames(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 && is.isArray(children)) {
    checkedElement = children.find(function(child) {
      return child.key === "open";
    });
    unCheckedElement = children.find(function(child) {
      return child.key === "close";
    });
  } else if (children && is.isObject(children)) {
    if (children.key === "open") {
      checkedElement = children;
    } else if (children.key === "close") {
      unCheckedElement = children;
    }
  }
  var extraProps = omit(rest, ["onChange", "checked", "error"]);
  return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs("button", {
    ...__assign({
      ref,
      role: "switch",
      "aria-checked": !!mergedChecked
    }, extraProps, {
      style,
      className: classNames$1,
      disabled,
      onClick: onHandleClick,
      type: "button"
    }),
    children: [/* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs("div", {
      className: prefixCls + "-dot",
      children: [!loading && (checkedIcon || uncheckedIcon) && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(SwitchTransition.default, {
        children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(CSSTransition, {
          classNames: "fadeIn",
          timeout: 200,
          children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("span", {
            className: prefixCls + "-dot-icon",
            children: mergedChecked ? checkedIcon : uncheckedIcon
          })
        }, mergedChecked ? "checked" : "unchecked")
      }), loading && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("span", {
        className: prefixCls + "-dot-icon",
        children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$1, {})
      })]
    }), size !== "small" && type !== "line" && (checkedElement || unCheckedElement) && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs(emotionReactJsxRuntime_browser_esm.Fragment, {
      children: [/* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs("div", {
        className: prefixCls + "-text-holder",
        children: [checkedElement && mergedChecked && checkedElement, unCheckedElement && !mergedChecked && unCheckedElement]
      }), /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(CSSTransition, {
        in: mergedChecked,
        classNames: "switchSlideText",
        timeout: 200,
        children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs("div", {
          className: prefixCls + "-text",
          children: [checkedElement && mergedChecked && checkedElement, unCheckedElement && !mergedChecked && unCheckedElement]
        })
      })]
    })]
  });
}
var ForwardRefSwitch = React.forwardRef(Switch);
var SwitchComponent = ForwardRefSwitch;
SwitchComponent.__BYTE_SWITCH = true;
SwitchComponent.displayName = "Switch";
const ArcoSwitch = SwitchComponent;
module.exports = ArcoSwitch;
//# sourceMappingURL=index.js.map
