"use strict";

exports.__esModule = true;
exports.default = void 0;

var _babelRuntimeJsxStyleTransform = require("babel-runtime-jsx-style-transform");

var _tslib = require("tslib");

var _react = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _vibrate = require("@uni/vibrate");

var _index = _interopRequireDefault(require("../icon/index"));

var _view = _interopRequireDefault(require("../view"));

var _utils = require("../utils");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

var getCheckedState = function (props) {
  return (0, _utils.isUndef)(props.checked) ? !!props.defaultChecked : !!props.checked;
};

var Switch = function (props) {
  var _classNames;

  var _props$prefix = props.prefix,
      prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix,
      className = props.className,
      style = props.style,
      _props$size = props.size,
      size = _props$size === void 0 ? 'medium' : _props$size,
      _props$defaultChecked = props.defaultChecked,
      defaultChecked = _props$defaultChecked === void 0 ? false : _props$defaultChecked,
      checked = props.checked,
      _props$disabled = props.disabled,
      disabled = _props$disabled === void 0 ? false : _props$disabled,
      _props$readOnly = props.readOnly,
      readOnly = _props$readOnly === void 0 ? false : _props$readOnly,
      _props$loading = props.loading,
      loading = _props$loading === void 0 ? false : _props$loading,
      _props$vibrative = props.vibrative,
      vibrative = _props$vibrative === void 0 ? true : _props$vibrative,
      onChange = props.onChange,
      others = (0, _tslib.__rest)(props, ["prefix", "className", "style", "size", "defaultChecked", "checked", "disabled", "readOnly", "loading", "vibrative", "onChange"]);
  var initChecked = getCheckedState(props);

  var _useState = (0, _react.useState)(initChecked),
      hasChecked = _useState[0],
      setChecked = _useState[1];

  var checkedPrefix = hasChecked ? '-checked' : '';
  var disabledPrefix = disabled ? '-disabled' : '';
  var switchClassNames = (0, _classnames.default)(prefix + "switch", prefix + "switch--" + size, (_classNames = {}, _classNames[prefix + "switch-" + disabledPrefix + checkedPrefix] = hasChecked || disabled, _classNames), className);
  var switchKnobClassNames = (0, _classnames.default)(prefix + "switch-knob", prefix + "switch-knob--" + size + checkedPrefix);
  var switchLoadingClassNames = (0, _classnames.default)(prefix + "switch-knob-loading", prefix + "switch-knob-loading--" + size);

  var onSwitchTap = function (e) {
    if (disabled || readOnly) {
      return;
    }

    var checkedValue = !hasChecked;

    if (vibrative) {
      (0, _vibrate.vibrateShort)({}).then(function () {});
    }

    if (!('checked' in props)) {
      setChecked(checkedValue);
    }

    if (onChange) {
      onChange(checkedValue, e);
    }
  };

  (0, _react.useEffect)(function () {
    if (initChecked !== hasChecked) {
      setChecked(initChecked);
    }
  }, [checked]);
  return /*#__PURE__*/(0, _react.createElement)(_view.default, _extends({
    className: switchClassNames,
    style: (0, _babelRuntimeJsxStyleTransform.rpx2vw4style)(style),
    onClick: onSwitchTap
  }, others), /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: switchKnobClassNames
  }, loading ? /*#__PURE__*/(0, _react.createElement)(_index.default, {
    className: switchLoadingClassNames,
    name: "loading"
  }) : null));
};

var _default = /*#__PURE__*/(0, _react.forwardRef)(Switch);

exports.default = _default;