'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _omit = require('lodash/omit');

var _omit2 = _interopRequireDefault(_omit);

var _CheckBox = require('../CheckBox');

var _CheckBox2 = _interopRequireDefault(_CheckBox);

var _FieldControl = require('../FieldControl');

var _FieldControl2 = _interopRequireDefault(_FieldControl);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

function getInputProps(props) {
  var input = props.input,
      resultProps = _objectWithoutProperties(props, ['input']);

  return _extends({}, resultProps, input);
}

var CheckBoxes = function (_React$Component) {
  _inherits(CheckBoxes, _React$Component);

  function CheckBoxes() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, CheckBoxes);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = CheckBoxes.__proto__ || Object.getPrototypeOf(CheckBoxes)).call.apply(_ref, [this].concat(args))), _this), _this.onChange = function (checkedValue, initialValue) {
      var input = _this.props.input;

      var _ref2 = input || _this.props,
          _ref2$value = _ref2.value,
          value = _ref2$value === undefined ? [] : _ref2$value,
          onChange = _ref2.onChange;

      var resultValue = void 0;
      var valueFound = value instanceof Array && value.find(function (item) {
        return JSON.stringify(item) === JSON.stringify(initialValue);
      });
      if (valueFound) {
        resultValue = value.filter(function (item) {
          return JSON.stringify(item) !== JSON.stringify(initialValue);
        });
      } else {
        resultValue = value instanceof Array ? [].concat(_toConsumableArray(value)) : [];
        resultValue.push(initialValue);
      }
      if (typeof onChange === 'function') {
        onChange(resultValue);
      }
    }, _this.onFocus = function () {
      var input = _this.props.input;

      var _ref3 = input || _this.props,
          _ref3$value = _ref3.value,
          value = _ref3$value === undefined ? [] : _ref3$value,
          onFocus = _ref3.onFocus;

      if (typeof onFocus === 'function') {
        onFocus(value);
      }
    }, _this.onBlur = function () {
      var input = _this.props.input;

      var _ref4 = input || _this.props,
          _ref4$value = _ref4.value,
          value = _ref4$value === undefined ? [] : _ref4$value,
          onBlur = _ref4.onBlur;

      if (typeof onBlur === 'function') {
        onBlur(value);
      }
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(CheckBoxes, [{
    key: 'render',
    value: function render() {
      var _this2 = this;

      var _props = this.props,
          options = _props.options,
          props = _objectWithoutProperties(_props, ['options']);

      return _react2.default.createElement(
        _FieldControl2.default,
        props,
        function (restProps) {
          var _getInputProps = getInputProps(restProps),
              value = _getInputProps.value,
              disabled = _getInputProps.disabled,
              resultProps = _objectWithoutProperties(_getInputProps, ['value', 'disabled']);

          var resultValue = value instanceof Array ? value : [];
          return options.map(function (_ref5) {
            var optionValue = _ref5.value,
                optionLabel = _ref5.label,
                optionDisabled = _ref5.disabled;
            return _react2.default.createElement(
              _CheckBox2.default,
              _extends({
                optionValue: optionValue,
                value: resultValue.find(function (item) {
                  return JSON.stringify(item) === JSON.stringify(optionValue);
                }) ? optionValue : undefined,
                onChange: _this2.onChange,
                onFocus: _this2.onFocus,
                onBlur: _this2.onBlur,
                disabled: !!(disabled || optionDisabled),
                key: JSON.stringify(optionValue)
              }, (0, _omit2.default)(resultProps, ['onChange', 'onFocus', 'onBlur']), {
                withoutMarge: true
              }),
              optionLabel
            );
          });
        }
      );
    }
  }]);

  return CheckBoxes;
}(_react2.default.Component);

CheckBoxes.propTypes = {
  options: _propTypes2.default.arrayOf(_propTypes2.default.shape({
    value: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number, _propTypes2.default.bool, _propTypes2.default.array, _propTypes2.default.object]),
    label: _propTypes2.default.string,
    disabled: _propTypes2.default.bool
  })),
  onChange: _propTypes2.default.func,
  disabled: _propTypes2.default.bool,
  mandatory: _propTypes2.default.bool,
  input: _propTypes2.default.object
};
CheckBoxes.defaultProps = {
  options: [],
  onChange: null,
  disabled: false,
  mandatory: false,
  input: null
};


CheckBoxes.CheckBox = _CheckBox2.default;
exports.default = CheckBoxes;
module.exports = exports['default'];