"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getDummyItem = getDummyItem;
exports.getElementId = getElementId;
exports.getFocusableItemsDefaultImpl = getFocusableItemsDefaultImpl;
exports.getItemsMap = getItemsMap;
exports.getKeyActionFn = getKeyActionFn;
exports.getNextItem = getNextItem;
exports.getPrevItem = getPrevItem;
exports.moveFocusToFirstItem = moveFocusToFirstItem;
exports.moveFocusToLastItem = moveFocusToLastItem;
exports.moveFocusToNextItem = moveFocusToNextItem;
exports.moveFocusToPrevItem = moveFocusToPrevItem;
exports.setAllItemsState = setAllItemsState;
exports.setItemState = setItemState;
exports.toggleSelect = toggleSelect;
var _constants = require("./constants");
function getFocusableItemsDefaultImpl(items) {
  return items.filter(({
    isDisabled
  }) => !isDisabled);
}
function getItemsMap(items) {
  const entries = items.map(n => [n.id, n]);
  return new Map(entries);
}
function setItemState(id, patch, items) {
  return items.map(item => item.id === id ? {
    ...item,
    ...patch
  } : item);
}
function setAllItemsState(patch, items) {
  return items.map(item => ({
    ...item,
    ...patch
  }));
}
function getKeyActionFn(key) {
  if (key === _constants.KEYS.DOWN) {
    return moveFocusToNextItem;
  }
  if (key === _constants.KEYS.UP) {
    return moveFocusToPrevItem;
  }
  if (key === _constants.KEYS.HOME) {
    return moveFocusToFirstItem;
  }
  if (key === _constants.KEYS.END) {
    return moveFocusToLastItem;
  }
  if (key === _constants.KEYS.ENTER || key === _constants.KEYS.SPACE) {
    return toggleSelect;
  }
  return null;
}

// action functions start
function moveFocusToNextItem({
  item,
  context
}) {
  const targetItem = getNextItem(item, context.items);
  if (!targetItem) {
    return;
  }
  const targetElement = context.refs.current.get(targetItem.id);
  context.setItems(items => {
    const allUnfocused = setAllItemsState({
      isFocused: false
    }, items);
    return setItemState(targetItem.id, {
      isFocused: true
    }, allUnfocused);
  });
  targetElement?.focus();
}
function moveFocusToPrevItem({
  item,
  context
}) {
  const targetItem = getPrevItem(item, context.items);
  if (!targetItem) {
    return;
  }
  const targetElement = context.refs.current.get(targetItem.id);
  context.setItems(items => {
    const allUnfocused = setAllItemsState({
      isFocused: false
    }, items);
    return setItemState(targetItem.id, {
      isFocused: true
    }, allUnfocused);
  });
  targetElement?.focus();
}
function moveFocusToFirstItem({
  context
}) {
  const targetItem = context.items[0];
  const targetElement = context.refs.current.get(targetItem.id);
  context.setItems(items => {
    const allUnfocused = setAllItemsState({
      isFocused: false
    }, items);
    return setItemState(targetItem.id, {
      isFocused: true
    }, allUnfocused);
  });
  targetElement?.focus();
}
function moveFocusToLastItem({
  context
}) {
  const targetItem = context.items[context.items.length - 1];
  const targetElement = context.refs.current.get(targetItem.id);
  context.setItems(items => {
    const allUnfocused = setAllItemsState({
      isFocused: false
    }, items);
    return setItemState(targetItem.id, {
      isFocused: true
    }, allUnfocused);
  });
  targetElement?.focus();
}
function toggleSelect({
  onToggleSelected,
  isSelected
}) {
  onToggleSelected(!isSelected);
}

// action functions end
function getNextItem(item, items) {
  const index = items.indexOf(item);
  return items.find((n, i) => i > index) ?? null;
}
function getPrevItem(item, items) {
  const index = items.indexOf(item);
  return items[index - 1] ?? null;
}
function getDummyItem(id) {
  return {
    id,
    isDisabled: true,
    isFocused: false,
    isSelected: false
  };
}
function getElementId(listId, id) {
  return `list-${listId}-item-${id}`;
}