'use strict';

import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { FormControl, isIndeterminate } from './FormControl';
import Checkbox from 'react-bootstrap/lib/Checkbox';
export var ToggleControl =
/*#__PURE__*/
function (_FormControl) {
  _inheritsLoose(ToggleControl, _FormControl);

  function ToggleControl(props) {
    var _this;

    _this = _FormControl.call(this, props) || this;

    _defineProperty(_assertThisInitialized(_this), "onFocus", function () {
      _this.handleFocus(true);
    });

    _defineProperty(_assertThisInitialized(_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 = 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 = _objectWithoutPropertiesLoose(_this$getPropsFromCon, ["readOnly", "disabled", "checked", "children", "hideLabel", "removeName"]);

    var hasFocus = this.state.hasFocus;
    var onClassName = checked ? 'rc-toggle-on' : 'rc-toggle-off';
    return React.createElement(Checkbox, _extends({
      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.createElement("div", {
      "aria-hidden": "true",
      className: classnames('rc-toggle-input', onClassName, {
        'rc-toggle-disabled': disabled,
        'rc-toggle-focus': hasFocus
      })
    }, React.createElement("div", {
      className: "rc-toggle-circle"
    })), React.createElement("span", {
      "data-test-automation-id": "label",
      className: classnames({
        'sr-only': hideLabel
      })
    }, children));
  };

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

  return ToggleControl;
}(FormControl);

_defineProperty(ToggleControl, "propTypes", Object.assign({}, FormControl.propTypes, {
  hideLabel: PropTypes.bool,
  removeName: PropTypes.bool
}));

_defineProperty(ToggleControl, "defaultProps", Object.assign({}, FormControl.defaultProps, {
  testAutomationId: 'toggle',
  hideLabel: false,
  removeName: false
}));