'use strict';

import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import React from 'react';
import Row from 'react-bootstrap/lib/Row';
import Col from 'react-bootstrap/lib/Col';
import _ from 'lodash';
import classnames from 'classnames';
import { Checkbox } from './Checkbox';
import { Form } from './Form';
import { Icon } from '../Icon';
export var CheckboxGroup =
/*#__PURE__*/
function (_Form) {
  _inheritsLoose(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.createElement(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.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 = _.extend({}, this.getInputValue());

    var diff = {};
    React.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 = classnames('rc-checkbox-group', {
      opened: this.state.opened,
      collapsed: !this.state.opened
    });
    var iconName = this.state.opened ? 'chevron-up-big' : 'chevron-down-big';
    return React.createElement("div", {
      className: checkboxGroupClassNames,
      "data-test-automation-id": this.props.testAutomationId
    }, React.createElement(Row, {
      className: "rc-checkbox-group-header"
    }, React.createElement(Col, {
      xs: 6
    }, this.getMainCheckbox()), React.createElement(Col, {
      xs: 6,
      className: "rc-checkbox-group-icon",
      onClick: this.checkboxesPanelToggleHandler.bind(this)
    }, React.createElement(Icon, {
      name: iconName,
      className: "pull-right"
    }))), React.createElement(Row, null, React.createElement(Col, {
      xs: 12,
      className: "rc-checkbox-group-body",
      "data-test-automation-id": "list"
    }, this.props.children)));
  };

  return CheckboxGroup;
}(Form);