'use strict';

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

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

var createNavigationMaps = function createNavigationMaps() {
  var items = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var pathMap = new Map();
  var focusedToValueMap = new Map();
  var valueToPathMap = new Map();
  pathMap.set('root', (items === null || items === void 0 ? void 0 : items.length) || 0);
  var rec = function rec(itemsToMap) {
    var prevIndex = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
    var path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
    itemsToMap === null || itemsToMap === void 0 || itemsToMap.forEach(function (item, index) {
      var value = item.value,
        innerItems = item.items;
      var currIndex = "".concat(prevIndex, "/").concat(index).replace(/^(\/)/, '');
      focusedToValueMap.set(currIndex, item);
      valueToPathMap.set(value.toString(), [].concat(_rollupPluginBabelHelpers.toConsumableArray(path), [value.toString()]));
      if (innerItems && !isEmpty.isEmpty(innerItems)) {
        pathMap.set(value, innerItems.length);
        rec(innerItems, currIndex, [].concat(_rollupPluginBabelHelpers.toConsumableArray(path), [value.toString()]));
      }
    });
  };
  rec(items);
  return [pathMap, focusedToValueMap, valueToPathMap];
};
var createSelectionMaps = function createSelectionMaps() {
  var items = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var valueToCheckedMap = new Map();
  var valueToItemMap = new Map();
  var valueToPathMap = new Map();
  var rec = function rec(itemsToMap) {
    var path = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
    itemsToMap === null || itemsToMap === void 0 || itemsToMap.forEach(function (item) {
      var value = item.value,
        innerItems = item.items;
      valueToCheckedMap.set(value, false);
      valueToPathMap.set(value.toString(), [].concat(_rollupPluginBabelHelpers.toConsumableArray(path), [value.toString()]));
      if (!innerItems || isEmpty.isEmpty(innerItems)) {
        valueToItemMap.set(value, item);
      } else {
        rec(innerItems, [].concat(_rollupPluginBabelHelpers.toConsumableArray(path), [value.toString()]));
      }
    });
  };
  rec(items);
  return [valueToCheckedMap, valueToItemMap, valueToPathMap];
};

// Создает структуры для навигации по текущему видимому дереву.
var useNavigationMaps = function useNavigationMaps() {
  var items = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  return React.useMemo(function () {
    return createNavigationMaps(items);
  }, [items]);
};

// Создает структуры для синхронизации выбранных значений по полному дереву.
var useSelectionMaps = function useSelectionMaps() {
  var items = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  return React.useMemo(function () {
    return createSelectionMaps(items);
  }, [items]);
};

exports.useNavigationMaps = useNavigationMaps;
exports.useSelectionMaps = useSelectionMaps;
