var __defProp = Object.defineProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
import React, { useContext, useMemo } from "react";
import classnames from "classnames";
import useMergedState from "../hooks/use-merged-state";
import Loading from "../loading";
import { addUnit } from "../utils";
import ConfigProviderContext from "../config-provider/ConfigProviderContext";
const Swtich = (props) => {
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace("switch", prefixCls);
  const {
    loading,
    disabled,
    size,
    activeColor,
    inactiveColor
  } = props;
  const [checked, setChecked] = useMergedState({
    value: props.checked,
    defaultValue: props.defaultChecked
  });
  const isChecked = useMemo(() => checked === props.activeValue, [checked, props.activeValue]);
  const style = __spreadValues({
    fontSize: addUnit(size),
    backgroundColor: isChecked ? activeColor : inactiveColor
  }, props.style);
  const onClick = (e) => {
    var _a, _b;
    if (!props.disabled) {
      (_a = props.onClick) == null ? void 0 : _a.call(props, e);
    }
    if (!props.disabled && !props.loading) {
      const newValue = isChecked ? props.inactiveValue : props.activeValue;
      setChecked(newValue);
      (_b = props.onChange) == null ? void 0 : _b.call(props, newValue);
    }
  };
  const renderLoading = () => {
    if (props.loading) {
      const color = isChecked ? props.activeColor : props.inactiveColor;
      return /* @__PURE__ */ React.createElement(Loading, {
        className: classnames(bem("loading")),
        color
      });
    }
    return null;
  };
  return /* @__PURE__ */ React.createElement("div", {
    role: "switch",
    tabIndex: 0,
    className: classnames(props.className, bem({
      on: isChecked,
      loading,
      disabled
    })),
    style,
    "aria-checked": isChecked,
    onClick
  }, /* @__PURE__ */ React.createElement("div", {
    className: classnames(bem("node"))
  }, renderLoading()));
};
Swtich.defaultProps = {
  activeValue: true,
  inactiveValue: false
};
var stdin_default = Swtich;
export {
  stdin_default as default
};
