"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

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

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _icon = _interopRequireDefault(require("../icon"));

var _util = require("../util");

var _configProvider = _interopRequireDefault(require("../config-provider"));

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

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

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); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/** Switch */
var Switch =
/*#__PURE__*/
function (_React$Component) {
  _inherits(Switch, _React$Component);

  function Switch(props, context) {
    var _this;

    _classCallCheck(this, Switch);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Switch).call(this, props, context));

    _defineProperty(_assertThisInitialized(_this), "onChange", function (ev) {
      var checked = !_this.state.checked;

      if (!('checked' in _this.props)) {
        _this.setState({
          checked: checked
        });
      }

      _this.props.onChange(checked, ev);

      _this.props.onClick && _this.props.onClick(ev);
    });

    _defineProperty(_assertThisInitialized(_this), "onKeyDown", function (e) {
      if (e.keyCode === _util.KEYCODE.ENTER || e.keyCode === _util.KEYCODE.SPACE) {
        _this.onChange(e);
      }

      _this.props.onKeyDown && _this.props.onKeyDown(e);
    });

    var _checked = props.checked || props.defaultChecked;

    _this.state = {
      checked: _checked
    };
    return _this;
  }

  _createClass(Switch, [{
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      if ('checked' in nextProps) {
        var checked = nextProps.checked;

        if (checked === undefined) {
          checked = false;
        }

        this.setState({
          checked: checked
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _classNames;

      /* eslint-disable no-unused-vars */
      var _this$props = this.props,
          prefix = _this$props.prefix,
          className = _this$props.className,
          loading = _this$props.loading,
          disabled = _this$props.disabled,
          size = _this$props.size,
          rtl = _this$props.rtl,
          others = _objectWithoutProperties(_this$props, ["prefix", "className", "loading", "disabled", "size", "rtl"]);

      var status = this.state.checked ? 'on' : 'off';
      var _size = size;

      if (_size !== 'small' && _size !== 'medium') {
        _size = 'medium';
      }

      var classes = (0, _classnames["default"])((_classNames = {}, _defineProperty(_classNames, "".concat(prefix, "switch"), true), _defineProperty(_classNames, "".concat(prefix, "switch-").concat(status), true), _defineProperty(_classNames, "".concat(prefix, "switch-loading"), loading), _defineProperty(_classNames, "".concat(prefix, "switch-").concat(_size), true), _defineProperty(_classNames, className, className), _classNames));
      var attrs;

      if (!disabled && !loading) {
        attrs = {
          onClick: this.onChange,
          tabIndex: 0,
          onKeyDown: this.onKeyDown,
          disabled: false
        };
      } else {
        attrs = {
          disabled: true
        };
      }

      return _react["default"].createElement("div", _extends({
        role: "switch",
        dir: rtl ? 'rtl' : undefined,
        tabIndex: "0"
      }, others, {
        className: classes
      }, attrs, {
        "aria-checked": this.state.checked
      }), loading ? _react["default"].createElement(_icon["default"], {
        className: "loading-icon",
        type: "loading"
      }) : null);
    }
  }]);

  return Switch;
}(_react["default"].Component);

_defineProperty(Switch, "contextTypes", {
  prefix: _propTypes["default"].string
});

_defineProperty(Switch, "propTypes", {
  prefix: _propTypes["default"].string,
  rtl: _propTypes["default"].bool,
  pure: _propTypes["default"].bool,

  /**
   * 自定义类名
   */
  className: _propTypes["default"].string,

  /**
   * 自定义内敛样式
   */
  style: _propTypes["default"].object,

  /**
   * 是否在加载中
   */
  loading: _propTypes["default"].bool,

  /**
   * 开关状态改变是触发此事件
   * @param {Boolean} checked 是否为打开状态
   * @param {Event} e DOM事件对象
   */
  onChange: _propTypes["default"].func,

  /**
   * 开关当前的值(针对受控组件)
   */
  checked: _propTypes["default"].bool,

  /**
   * 开关默认值 (针对非受控组件)
   */
  defaultChecked: _propTypes["default"].bool,

  /**
   * 表示开关被禁用
   */
  disabled: _propTypes["default"].bool,

  /**
   * switch的尺寸
   * @enumdesc 正常大小, 缩小版大小
   */
  size: _propTypes["default"].oneOf(['medium', 'small']),

  /**
   * 鼠标点击事件
   * @param {Event} e DOM事件对象
   */
  onClick: _propTypes["default"].func,

  /**
   * 键盘按键事件
   * @param {Event} e DOM事件对象
   */
  onKeyDown: _propTypes["default"].func
});

_defineProperty(Switch, "defaultProps", {
  prefix: 'next-',
  loading: false,
  disabled: false,
  size: 'medium',
  defaultChecked: false,
  onChange: function onChange() {}
});

var _default = _configProvider["default"].config(Switch);

exports["default"] = _default;