import { cloneDeep } from 'lodash-es';

var useSelect = _ref => {
  let {
    nodeList,
    currentSelectedKeys,
    updateSelectedKeys,
    props,
    emit
  } = _ref;
  const selectNode = (val, event) => {
    if (!props.selectable) {
      return;
    }
    const node = nodeList.get(val);
    const values = cloneDeep(currentSelectedKeys.value);
    const index = values.indexOf(val);
    if (props.multiple) {
      if (index !== -1) {
        if (props.cancelable) {
          values.splice(index, 1);
        }
      } else {
        values.push(val);
      }
    } else if (index !== -1) {
      if (props.cancelable) {
        values.splice(index, 1);
      }
    } else {
      values[0] = val;
    }
    updateSelectedKeys(values);
    emit('select', {
      selectedKeys: values,
      event,
      node,
      selected: values.includes(val)
    });
  };
  return {
    selectNode
  };
};

export { useSelect as default };
