'use strict';

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

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

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

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

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 _FormControl2 = require("./FormControl");

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

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

  function CheckboxControl() {
    return _FormControl.apply(this, arguments) || this;
  }

  var _proto = CheckboxControl.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 _this = this;

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

    return _react.default.createElement(_Checkbox.default, (0, _extends2.default)({
      disabled: readOnly || disabled,
      checked: checked,
      inputRef: function inputRef(ref) {
        return _this.input = ref;
      },
      "data-test-automation-id": "input"
    }, this.getSafeProps(props), {
      name: removeName ? null : props.name
    }), _react.default.createElement("span", {
      "data-test-automation-id": "label",
      className: !!hiddenLabel ? 'sr-only' : null
    }, children));
  };

  return CheckboxControl;
}(_FormControl2.FormControl);

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