import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import IconButton from '../button/IconButton';

var SwitchBase =
/*#__PURE__*/
function (_Component) {
  _inherits(SwitchBase, _Component);

  _createClass(SwitchBase, null, [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(props, state) {
      var defaultChecked = props.defaultChecked;

      if (!state.isControlled && defaultChecked !== state.prevDefaultChecked) {
        return {
          checked: defaultChecked,
          prevDefaultChecked: defaultChecked
        };
      }

      return null;
    }
  }]);

  function SwitchBase(props) {
    var _this;

    _classCallCheck(this, SwitchBase);

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

    _defineProperty(_assertThisInitialized(_this), "onInputChange", function (e) {
      var checked = e.target.checked;
      var onChange = _this.props.onChange;

      if (!_this.isControlled) {
        _this.setState({
          checked: checked
        });
      }

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

    _defineProperty(_assertThisInitialized(_this), "getEl", function (el) {
      var inputRef = _this.props.inputRef;

      if (inputRef) {
        inputRef(el);
      }
    });

    var defaultChecked = props.defaultChecked,
        _checked = props.checked;
    _this.isControlled = _checked !== undefined;
    _this.state = {
      checked: defaultChecked,
      isControlled: _this.isControlled,
      prevDefaultChecked: defaultChecked
    };
    return _this;
  }

  _createClass(SwitchBase, [{
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      var checked = this.props.checked;

      if (!this.isControlled && checked !== undefined) {
        this.isControlled = true;
        this.setState({
          checked: null
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _classNames;

      var _this$props = this.props,
          prefixCls = _this$props.prefixCls,
          inputType = _this$props.inputType,
          inputClass = _this$props.inputClass,
          checkedIcon = _this$props.checkedIcon,
          uncheckedIcon = _this$props.uncheckedIcon,
          indeterminate = _this$props.indeterminate,
          indeterminateIcon = _this$props.indeterminateIcon,
          inputRef = _this$props.inputRef,
          className = _this$props.className,
          disabled = _this$props.disabled,
          propsChecked = _this$props.checked,
          defaultChecked = _this$props.defaultChecked,
          label = _this$props.label,
          onChange = _this$props.onChange,
          iconRender = _this$props.iconRender,
          error = _this$props.error,
          size = _this$props.size,
          inputProps = _this$props.inputProps,
          status = _this$props.status,
          errorIcon = _this$props.errorIcon,
          loadingIcon = _this$props.loadingIcon,
          successIcon = _this$props.successIcon,
          tabIndex = _this$props.tabIndex,
          showStatusIcon = _this$props.showStatusIcon,
          other = _objectWithoutProperties(_this$props, ["prefixCls", "inputType", "inputClass", "checkedIcon", "uncheckedIcon", "indeterminate", "indeterminateIcon", "inputRef", "className", "disabled", "checked", "defaultChecked", "label", "onChange", "iconRender", "error", "size", "inputProps", "status", "errorIcon", "loadingIcon", "successIcon", "tabIndex", "showStatusIcon"]);

      var stateChecked = this.state.checked;
      var checked = this.isControlled ? propsChecked : stateChecked;
      var icon = indeterminate ? indeterminateIcon : checkedIcon;
      return React.createElement("label", {
        className: classNames("".concat(prefixCls), (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-disabled"), disabled), _defineProperty(_classNames, "".concat(prefixCls, "-checked"), checked), _defineProperty(_classNames, "".concat(prefixCls, "-").concat(size), size), _classNames), className)
      }, React.createElement(IconButton, {
        component: "span",
        className: "".concat(prefixCls, "-button"),
        variant: "text",
        active: checked,
        tabIndex: null,
        disabled: disabled,
        size: "small"
      }, React.createElement("input", _extends({
        className: "".concat(prefixCls, "-input"),
        checked: checked,
        onChange: this.onInputChange,
        ref: this.getEl,
        tabIndex: tabIndex,
        disabled: disabled
      }, inputProps, other)), iconRender ? iconRender(indeterminate ? 'indeterminate' : checked ? 'checked' : 'unchecked') : checked ? icon : uncheckedIcon), React.createElement("span", {
        className: "".concat(prefixCls, "-label-text")
      }, typeof label === 'function' ? label(checked, disabled) : label));
    }
  }]);

  return SwitchBase;
}(Component);

_defineProperty(SwitchBase, "propTypes", {
  inputType: PropTypes.string,
  prefixCls: PropTypes.string,
  label: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
  disabled: PropTypes.bool,
  checked: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
  defaultChecked: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
  checkedIcon: PropTypes.node,
  className: PropTypes.string,
  uncheckedIcon: PropTypes.node,
  indeterminate: PropTypes.bool,
  indeterminateIcon: PropTypes.node,
  iconRender: PropTypes.func,
  inputRef: PropTypes.func,
  onChange: PropTypes.func,
  size: PropTypes.oneOf(['small', 'medium', 'large'])
});

_defineProperty(SwitchBase, "defaultProps", {
  type: 'checkbox',
  defaultChecked: false,
  prefixCls: 'ne-switch-base',
  disabled: false,
  size: 'medium'
});

export { SwitchBase as default };