"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Switch = void 0;
var _tslib = require("tslib");
var _classnames = _interopRequireDefault(require("classnames"));
var _react = _interopRequireWildcard(require("react"));
var _nativeProps = require("../../utils/native-props");
var _usePropsValue = require("../../utils/use-props-value");
var _withDefaultProps = require("../../utils/with-default-props");
var _spinIcon = require("./spin-icon");
var _configProvider = require("../config-provider");
var _validate = require("../../utils/validate");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const classPrefix = `adm-switch`;
const defaultProps = {
  defaultChecked: false
};
const Switch = p => {
  const props = (0, _withDefaultProps.mergeProps)(defaultProps, p);
  const disabled = props.disabled || props.loading || false;
  const [changing, setChanging] = (0, _react.useState)(false);
  const {
    locale
  } = (0, _configProvider.useConfig)();
  const [checked, setChecked] = (0, _usePropsValue.usePropsValue)({
    value: props.checked,
    defaultValue: props.defaultChecked,
    onChange: props.onChange
  });
  function onClick() {
    return (0, _tslib.__awaiter)(this, void 0, void 0, function* () {
      if (disabled || props.loading || changing) {
        return;
      }
      const nextChecked = !checked;
      if (props.beforeChange) {
        setChanging(true);
        try {
          yield props.beforeChange(nextChecked);
          setChanging(false);
        } catch (e) {
          setChanging(false);
          throw e;
        }
      }
      const result = setChecked(nextChecked);
      if ((0, _validate.isPromise)(result)) {
        setChanging(true);
        try {
          yield result;
          setChanging(false);
        } catch (e) {
          setChanging(false);
          throw e;
        }
      }
    });
  }
  return (0, _nativeProps.withNativeProps)(props, _react.default.createElement("div", {
    onClick: onClick,
    className: (0, _classnames.default)(classPrefix, {
      [`${classPrefix}-checked`]: checked,
      [`${classPrefix}-disabled`]: disabled || changing
    }),
    role: 'switch',
    "aria-label": locale.Switch.name,
    "aria-checked": checked,
    "aria-disabled": disabled
  }, _react.default.createElement("div", {
    className: `${classPrefix}-checkbox`
  }, _react.default.createElement("div", {
    className: `${classPrefix}-handle`
  }, (props.loading || changing) && _react.default.createElement(_spinIcon.SpinIcon, {
    className: `${classPrefix}-spin-icon`
  })), _react.default.createElement("div", {
    className: `${classPrefix}-inner`
  }, checked ? props.checkedText : props.uncheckedText))));
};
exports.Switch = Switch;