'use strict';

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

var _rollupPluginBabelHelpers = require('../../../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefault(React);

var isReactObject = function isReactObject(element) {
  return _rollupPluginBabelHelpers.typeof(element) === 'object' || typeof element === 'function';
};
var getValidComponent = function getValidComponent(element, props) {
  if (/*#__PURE__*/React.isValidElement(element)) {
    return /*#__PURE__*/React.cloneElement(element, props);
  }
  if (isReactObject(element)) {
    var Component = element;
    return /*#__PURE__*/React__default.default.createElement(Component, props);
  }
  return element;
};
var getNewSelected = function getNewSelected(selected, newValue, type) {
  if (newValue === undefined || newValue === null) {
    return newValue;
  }
  if ((type === 'single' || type === 'native') && (typeof newValue === 'string' || typeof newValue === 'number' || typeof newValue === 'boolean')) {
    return newValue;
  }
  var newSelected = selected || [];
  var set = new Set(newSelected);
  if (set.has(newValue)) {
    set["delete"](newValue);
  } else {
    set.add(newValue);
  }
  return Array.from(set);
};
var getChildrenInnerText = function getChildrenInnerText(children) {
  if (!children || typeof children === 'string') {
    return children;
  }
  return getChildrenInnerText(children.props.children);
};
var getValues = function getValues(items, value) {
  var result = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
  items.forEach(function (item) {
    if (item.props === undefined) {
      return result;
    }
    var newValue = item.props.value;
    if (item.props.children && Array.isArray(item.props.children)) {
      return getValues(item.props.children, value, result);
    }
    if (!Array.isArray(value) && newValue === value || Array.isArray(value) && value !== null && value !== void 0 && value.find(function (v) {
      return newValue === v;
    })) {
      var index = Array.isArray(value) && (value === null || value === void 0 ? void 0 : value.findIndex(function (v) {
        return newValue === v;
      })) || 0;
      if (item.props.text) {
        var _newText = item.props.text;
        result[index] = [newValue, _newText];
        return;
      }
      var newText = getChildrenInnerText(item.props.children);
      result[index] = [newValue, newText];
    }
    return result;
  });
  return result;
};
var getChildrenItemChecked = function getChildrenItemChecked(value, child) {
  return !Array.isArray(value) && (child === null || child === void 0 ? void 0 : child.props.value) === value || Array.isArray(value) && Boolean(value === null || value === void 0 ? void 0 : value.find(function (v) {
    return (child === null || child === void 0 ? void 0 : child.props.value) === v;
  }));
};

// INFO: Функция для рекурсивного обновления пропсов
var updatePropsRecursively = function updatePropsRecursively(children, externalProps, value, search, filterFunction) {
  return React.Children.map(children || [], function (child) {
    if (! /*#__PURE__*/React.isValidElement(child)) {
      return child;
    }
    var hasValue = 'value' in (child === null || child === void 0 ? void 0 : child.props);
    var props = _rollupPluginBabelHelpers.objectSpread2({
      key: "".concat(value, "$"),
      checked: getChildrenItemChecked(value, child)
    }, hasValue && {
      ref: function ref(element) {
        var _externalProps$childr;
        if (externalProps !== null && externalProps !== void 0 && (_externalProps$childr = externalProps.childrenRefs) !== null && _externalProps$childr !== void 0 && _externalProps$childr.current && element) {
          externalProps.childrenRefs.current.push(element);
        }
      },
      onClick: function onClick(event) {
        var _child$props$onClick, _child$props, _externalProps$onClic;
        (_child$props$onClick = (_child$props = child.props).onClick) === null || _child$props$onClick === void 0 || _child$props$onClick.call(_child$props, event);
        externalProps === null || externalProps === void 0 || (_externalProps$onClic = externalProps.onClick) === null || _externalProps$onClic === void 0 || _externalProps$onClic.call(externalProps, event);
      }
    });
    var updatedChild = updatePropsRecursively(child.props.children, externalProps, value, search, filterFunction);
    if (filterFunction) {
      var _child$props2;
      var text = ((_child$props2 = child.props) === null || _child$props2 === void 0 ? void 0 : _child$props2.text) || '';
      return filterFunction(text, search) ? /*#__PURE__*/React.cloneElement(child, props, updatedChild) : null;
    }
    return /*#__PURE__*/React.cloneElement(child, props, updatedChild);
  });
};
var defaultFilterFunction = function defaultFilterFunction(text) {
  var filterValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  return text.includes(filterValue);
};
var getChildren = function getChildren(children, externalProps, value, search) {
  var filterFunction = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : defaultFilterFunction;
  if (externalProps.childrenRefs) {
    externalProps.childrenRefs.current = [];
  }
  return updatePropsRecursively(children, externalProps, value, search, filterFunction);
};

exports.getChildren = getChildren;
exports.getNewSelected = getNewSelected;
exports.getValidComponent = getValidComponent;
exports.getValues = getValues;
exports.updatePropsRecursively = updatePropsRecursively;
//# sourceMappingURL=index.js.map
