'use strict';

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

var vue = require('vue');
require('../../../../utils/index.cjs');
var useBaseProps = require('../../../vc-select/src/hooks/useBaseProps.cjs');
var useSearchOptions = require('../hooks/useSearchOptions.cjs');
var KeyCode = require('../../../../utils/KeyCode.cjs');

var useKeyboard = (context, options, fieldNames, activeValueCells, setActiveValueCells, onKeyBoardSelect) => {
  const baseProps = useBaseProps.default();
  const rtl = vue.computed(() => baseProps.direction === "rtl");
  const [validActiveValueCells, lastActiveIndex, lastActiveOptions] = [
    vue.ref([]),
    vue.ref(),
    vue.ref([])
  ];
  vue.watchEffect(() => {
    let activeIndex = -1;
    let currentOptions = options.value;
    const mergedActiveIndexes = [];
    const mergedActiveValueCells = [];
    const len = activeValueCells.value.length;
    for (let i = 0; i < len && currentOptions; i += 1) {
      const nextActiveIndex = currentOptions.findIndex(
        (option) => option[fieldNames.value.value] === activeValueCells.value[i]
      );
      if (nextActiveIndex === -1)
        break;
      activeIndex = nextActiveIndex;
      mergedActiveIndexes.push(activeIndex);
      mergedActiveValueCells.push(activeValueCells.value[i]);
      currentOptions = currentOptions[activeIndex][fieldNames.value.children];
    }
    let activeOptions = options.value;
    for (let i = 0; i < mergedActiveIndexes.length - 1; i += 1)
      activeOptions = activeOptions[mergedActiveIndexes[i]][fieldNames.value.children];
    [validActiveValueCells.value, lastActiveIndex.value, lastActiveOptions.value] = [
      mergedActiveValueCells,
      activeIndex,
      activeOptions
    ];
  });
  const internalSetActiveValueCells = (next) => {
    setActiveValueCells(next);
  };
  const offsetActiveOption = (offset) => {
    const len = lastActiveOptions.value.length;
    let currentIndex = lastActiveIndex.value;
    if (currentIndex === -1 && offset < 0)
      currentIndex = len;
    for (let i = 0; i < len; i += 1) {
      currentIndex = (currentIndex + offset + len) % len;
      const option = lastActiveOptions.value[currentIndex];
      if (option && !option.disabled) {
        const value = option[fieldNames.value.value];
        const nextActiveCells = validActiveValueCells.value.slice(0, -1).concat(value);
        internalSetActiveValueCells(nextActiveCells);
        return;
      }
    }
  };
  const prevColumn = () => {
    if (validActiveValueCells.value.length > 1) {
      const nextActiveCells = validActiveValueCells.value.slice(0, -1);
      internalSetActiveValueCells(nextActiveCells);
    } else {
      baseProps.toggleOpen(false);
    }
  };
  const nextColumn = () => {
    var _a;
    const nextOptions = ((_a = lastActiveOptions.value[lastActiveIndex.value]) == null ? void 0 : _a[fieldNames.value.children]) || [];
    const nextOption = nextOptions.find((option) => !option.disabled);
    if (nextOption) {
      const nextActiveCells = [...validActiveValueCells.value, nextOption[fieldNames.value.value]];
      internalSetActiveValueCells(nextActiveCells);
    }
  };
  context.expose({
    // scrollTo: treeRef.current?.scrollTo,
    onKeydown: (event) => {
      const { which } = event;
      switch (which) {
        case KeyCode.KeyCode.UP:
        case KeyCode.KeyCode.DOWN: {
          let offset = 0;
          if (which === KeyCode.KeyCode.UP)
            offset = -1;
          else if (which === KeyCode.KeyCode.DOWN)
            offset = 1;
          if (offset !== 0)
            offsetActiveOption(offset);
          break;
        }
        case KeyCode.KeyCode.LEFT: {
          if (rtl.value)
            nextColumn();
          else
            prevColumn();
          break;
        }
        case KeyCode.KeyCode.RIGHT: {
          if (rtl.value)
            prevColumn();
          else
            nextColumn();
          break;
        }
        case KeyCode.KeyCode.BACKSPACE: {
          if (!baseProps.searchValue)
            prevColumn();
          break;
        }
        case KeyCode.KeyCode.ENTER: {
          if (validActiveValueCells.value.length) {
            const option = lastActiveOptions.value[lastActiveIndex.value];
            const originOptions = (option == null ? void 0 : option[useSearchOptions.SEARCH_MARK]) || [];
            if (originOptions.length) {
              onKeyBoardSelect(
                originOptions.map((opt) => opt[fieldNames.value.value]),
                originOptions[originOptions.length - 1]
              );
            } else {
              onKeyBoardSelect(validActiveValueCells.value, option);
            }
          }
          break;
        }
        case KeyCode.KeyCode.ESC: {
          baseProps.toggleOpen(false);
          if (open)
            event.stopPropagation();
        }
      }
    },
    onKeyup: () => {
    }
  });
};

exports.default = useKeyboard;
//# sourceMappingURL=useKeyboard.cjs.map
