'use strict';

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

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

var useCheckedState = function useCheckedState(_ref) {
  var multiselect = _ref.multiselect,
    mode = _ref.mode,
    value = _ref.value,
    valueToCheckedMap = _ref.valueToCheckedMap,
    valueToItemMap = _ref.valueToItemMap,
    closeAfterSelect = _ref.closeAfterSelect,
    handleChange = _ref.handleChange,
    resetListState = _ref.resetListState;
  var _useState = React.useState(valueToCheckedMap),
    _useState2 = _rollupPluginBabelHelpers.slicedToArray(_useState, 2),
    checked = _useState2[0],
    setChecked = _useState2[1];
  var handleCheckboxChange = function handleCheckboxChange(item) {
    if (!multiselect) {
      return;
    }
    var checkedCopy = new Map(checked);
    switch (checkedCopy.get(item.value)) {
      case 'indeterminate':
        {
          updateDescendants.updateDescendants(item, checkedCopy, true, valueToItemMap);
          break;
        }
      case true:
        {
          updateDescendants.updateDescendants(item, checkedCopy, false, valueToItemMap);
          checkedCopy.set(item.value, false);
          break;
        }
      case false:
        {
          updateDescendants.updateDescendants(item, checkedCopy, true, valueToItemMap);
          checkedCopy.set(item.value, true);
          break;
        }
    }
    var newValues = [];
    valueToItemMap.forEach(function (currentItem, currentValue) {
      if (checkedCopy.get(currentValue)) {
        newValues.push(currentItem.value);
      }
    });
    if (Array.isArray(value)) {
      value.forEach(function (currentValue) {
        if (!valueToItemMap.has(currentValue)) {
          newValues.push(currentValue);
        }
      });
    }
    if (closeAfterSelect) {
      resetListState();
    }
    handleChange(newValues, item);
  };
  var handleItemClick = function handleItemClick(item, event) {
    if (!isEmpty.isEmpty(item.items)) {
      return;
    }
    if (multiselect) {
      handleCheckboxChange(item);
      return;
    }
    event === null || event === void 0 || event.stopPropagation();
    var isCurrentChecked = checked.get(item.value);
    if (closeAfterSelect) {
      resetListState();
    }
    if (mode === 'radio' && isCurrentChecked) {
      return;
    }
    handleChange(isCurrentChecked ? '' : item.value, item);
  };
  var handlePressDown = function handlePressDown(item, event) {
    if (isEmpty.isEmpty(item.items)) {
      handleItemClick(item, event);
      return;
    }
    if (multiselect) {
      handleCheckboxChange(item);
    }
  };
  React.useLayoutEffect(function () {
    setChecked(syncCheckedState.syncCheckedState(value, valueToCheckedMap, valueToItemMap));
  }, [value, valueToCheckedMap, valueToItemMap]);
  return {
    checked: checked,
    handleCheckboxChange: handleCheckboxChange,
    handleItemClick: handleItemClick,
    handlePressDown: handlePressDown
  };
};

exports.useCheckedState = useCheckedState;
