import { slicedToArray as _slicedToArray } from '../../../_virtual/_rollupPluginBabelHelpers.js';
import { useState, useLayoutEffect } from 'react';
import { syncCheckedState } from '../utils/syncCheckedState.js';
import { updateDescendants } from '../utils/updateDescendants.js';
import { isEmpty } from '../../../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 = useState(valueToCheckedMap),
    _useState2 = _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(item, checkedCopy, true, valueToItemMap);
          break;
        }
      case true:
        {
          updateDescendants(item, checkedCopy, false, valueToItemMap);
          checkedCopy.set(item.value, false);
          break;
        }
      case false:
        {
          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(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(item.items)) {
      handleItemClick(item, event);
      return;
    }
    if (multiselect) {
      handleCheckboxChange(item);
    }
  };
  useLayoutEffect(function () {
    setChecked(syncCheckedState(value, valueToCheckedMap, valueToItemMap));
  }, [value, valueToCheckedMap, valueToItemMap]);
  return {
    checked: checked,
    handleCheckboxChange: handleCheckboxChange,
    handleItemClick: handleItemClick,
    handlePressDown: handlePressDown
  };
};

export { useCheckedState };
