'use strict';

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

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

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

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

var _Row = _interopRequireDefault(require("react-bootstrap/lib/Row"));

var _Col = _interopRequireDefault(require("react-bootstrap/lib/Col"));

var _lodash = _interopRequireDefault(require("lodash"));

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

var _Checkbox = require("./Checkbox");

var _Form2 = require("./Form");

var _Icon = require("../Icon");

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

  function CheckboxGroup(props, context) {
    var _this;

    _this = _Form.call(this, props, context) || this;
    _this.state = {
      opened: true
    };
    return _this;
  }

  var _proto = CheckboxGroup.prototype;

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

    return _react.default.createElement(_Checkbox.Checkbox, {
      readOnly: this.extractFlatProperty('readOnly'),
      disabled: this.extractFlatProperty('disabled'),
      checked: this.getMainCheckboxValue(),
      onChange: function onChange(e) {
        return _this2.onMainCheckboxClick(!!e.target.checked);
      },
      label: this.props.title
    });
  };

  _proto.getMainCheckboxValue = function getMainCheckboxValue() {
    var value = this.getInputValue();

    var selectedCheckboxes = _react.default.Children.toArray(this.props.children).filter(function (checkbox) {
      return value[checkbox.props.name];
    });

    return this.calculateMainCheckboxValue(selectedCheckboxes.length, this.props.children.length);
  };

  _proto.calculateMainCheckboxValue = function calculateMainCheckboxValue(selectedCheckboxesCount, totalCheckboxesCount) {
    var mainCheckboxValue = null; // indeterminate by default

    if (selectedCheckboxesCount === totalCheckboxesCount) {
      mainCheckboxValue = true;
    }

    if (selectedCheckboxesCount === 0) {
      mainCheckboxValue = false;
    }

    return mainCheckboxValue;
  };

  _proto.onMainCheckboxClick = function onMainCheckboxClick(checked) {
    var value = _lodash.default.extend({}, this.getInputValue());

    var diff = {};

    _react.default.Children.toArray(this.props.children).forEach(function (checkbox) {
      if (checkbox.props.readOnly) {
        return;
      }

      var id = checkbox.props.name;

      if (id in value) {
        if (value[id] !== checked) {
          diff[id] = checked;
        }

        value[id] = checked;
      }
    });

    this.setOutputValue(value, diff);
  };

  _proto.checkboxesPanelToggleHandler = function checkboxesPanelToggleHandler() {
    this.setState({
      opened: !this.state.opened
    });
  };

  _proto.render = function render() {
    var checkboxGroupClassNames = (0, _classnames.default)('rc-checkbox-group', {
      opened: this.state.opened,
      collapsed: !this.state.opened
    });
    var iconName = this.state.opened ? 'chevron-up-big' : 'chevron-down-big';
    return _react.default.createElement("div", {
      className: checkboxGroupClassNames,
      "data-test-automation-id": this.props.testAutomationId
    }, _react.default.createElement(_Row.default, {
      className: "rc-checkbox-group-header"
    }, _react.default.createElement(_Col.default, {
      xs: 6
    }, this.getMainCheckbox()), _react.default.createElement(_Col.default, {
      xs: 6,
      className: "rc-checkbox-group-icon",
      onClick: this.checkboxesPanelToggleHandler.bind(this)
    }, _react.default.createElement(_Icon.Icon, {
      name: iconName,
      className: "pull-right"
    }))), _react.default.createElement(_Row.default, null, _react.default.createElement(_Col.default, {
      xs: 12,
      className: "rc-checkbox-group-body",
      "data-test-automation-id": "list"
    }, this.props.children)));
  };

  return CheckboxGroup;
}(_Form2.Form);

exports.CheckboxGroup = CheckboxGroup;