'use strict';

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

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

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

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

var _FormControl2 = require("./FormControl");

var _Checkbox = _interopRequireDefault(require("react-bootstrap/lib/Checkbox"));

var ToggleControl =
/*#__PURE__*/
function (_FormControl) {
  (0, _inheritsLoose2.default)(ToggleControl, _FormControl);

  function ToggleControl(props) {
    var _this;

    _this = _FormControl.call(this, props) || this;
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onFocus", function () {
      _this.handleFocus(true);
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onBlur", function () {
      _this.handleFocus(false);
    });
    _this.state = {
      hasFocus: false
    };
    return _this;
  }

  var _proto = ToggleControl.prototype;

  _proto.getValue = function getValue() {
    return this.getInputDOMNode().checked;
  };

  _proto.inputConverter = function inputConverter(value) {
    return {
      checked: value
    };
  };

  _proto.outputConverter = function outputConverter(e) {
    return e.target.checked;
  };

  _proto.getInputDOMNode = function getInputDOMNode() {
    return this.input;
  };

  _proto.componentWillReceiveProps = function componentWillReceiveProps(newProps) {
    this.getInputDOMNode().indeterminate = (0, _FormControl2.isIndeterminate)(newProps.checked);
  };

  _proto.componentDidMount = function componentDidMount() {
    this.componentWillReceiveProps(this.props);
    this.testAutomationIdHandler();
  };

  _proto.componentDidUpdate = function componentDidUpdate() {
    this.testAutomationIdHandler();
  };

  _proto.testAutomationIdHandler = function testAutomationIdHandler() {
    if (this.input && !this.props.hasFormGroupContainer) {
      this.input.parentNode.setAttribute('data-test-automation-id', this.props.testAutomationId);
    }
  };

  _proto.render = function render() {
    var _this2 = this;

    var _this$getPropsFromCon = this.getPropsFromContext(),
        readOnly = _this$getPropsFromCon.readOnly,
        disabled = _this$getPropsFromCon.disabled,
        checked = _this$getPropsFromCon.checked,
        children = _this$getPropsFromCon.children,
        hideLabel = _this$getPropsFromCon.hideLabel,
        removeName = _this$getPropsFromCon.removeName,
        props = (0, _objectWithoutPropertiesLoose2.default)(_this$getPropsFromCon, ["readOnly", "disabled", "checked", "children", "hideLabel", "removeName"]);

    var hasFocus = this.state.hasFocus;
    var onClassName = checked ? 'rc-toggle-on' : 'rc-toggle-off';
    return _react.default.createElement(_Checkbox.default, (0, _extends2.default)({
      role: "switch",
      disabled: readOnly || disabled,
      checked: checked,
      "aria-checked": checked,
      inputRef: function inputRef(ref) {
        return _this2.input = ref;
      },
      "data-test-automation-id": "input"
    }, this.getSafeProps(props), {
      onFocus: this.onFocus,
      onBlur: this.onBlur,
      className: "rc-toggle",
      name: removeName ? null : props.name
    }), _react.default.createElement("div", {
      "aria-hidden": "true",
      className: (0, _classnames.default)('rc-toggle-input', onClassName, {
        'rc-toggle-disabled': disabled,
        'rc-toggle-focus': hasFocus
      })
    }, _react.default.createElement("div", {
      className: "rc-toggle-circle"
    })), _react.default.createElement("span", {
      "data-test-automation-id": "label",
      className: (0, _classnames.default)({
        'sr-only': hideLabel
      })
    }, children));
  };

  _proto.handleFocus = function handleFocus(hasFocus) {
    this.setState({
      hasFocus: hasFocus
    });
  };

  return ToggleControl;
}(_FormControl2.FormControl);

exports.ToggleControl = ToggleControl;
(0, _defineProperty2.default)(ToggleControl, "propTypes", Object.assign({}, _FormControl2.FormControl.propTypes, {
  hideLabel: _propTypes.default.bool,
  removeName: _propTypes.default.bool
}));
(0, _defineProperty2.default)(ToggleControl, "defaultProps", Object.assign({}, _FormControl2.FormControl.defaultProps, {
  testAutomationId: 'toggle',
  hideLabel: false,
  removeName: false
}));