export function moveItems(selectedItemsList, itemsList, foward) {
  if (!selectedItemsList.length)
    return [...itemsList];
  if (!itemsList.length)
    return [];
  const selectedIndexes = getItemsIndex(selectedItemsList, itemsList);
  const startIndex = getMinValue(selectedIndexes);
  const endIndex = getMaxValue(selectedIndexes);
  return moveRange(startIndex, endIndex, foward, itemsList);
}
function getMinValue(numbers) {
  if (numbers.length === 0)
    return 0;
  return numbers.reduce((min, current) => (current < min ? current : min), numbers[0]);
}
function getMaxValue(numbers) {
  if (numbers.length === 0)
    return 0;
  return numbers.reduce((max, current) => (current > max ? current : max), numbers[0]);
}
function getItemsIndex(selectedItemsList, itemsList) {
  return selectedItemsList.map(selectedId => itemsList.findIndex(original => original.id === selectedId));
}
function moveRange(startIndex, endIndex, moveForward, listItem) {
  if (startIndex < 0 || endIndex >= listItem.length || startIndex > endIndex)
    return listItem;
  if (moveForward)
    return moveFoward(endIndex, startIndex, listItem);
  return moveBackward(startIndex, endIndex, listItem);
}
function moveBackward(startIndex, endIndex, listItem) {
  if (startIndex === 0)
    return listItem;
  const beforeRange = listItem.slice(0, startIndex - 1);
  const range = listItem.slice(startIndex, endIndex + 1);
  const afterRange = listItem.slice(endIndex + 1);
  const movedItem = listItem[startIndex - 1];
  return [...beforeRange, ...range, movedItem, ...afterRange];
}
function moveFoward(endIndex, startIndex, listItem) {
  if (endIndex === listItem.length - 1)
    return listItem;
  const beforeRange = listItem.slice(0, startIndex);
  const range = listItem.slice(startIndex, endIndex + 1);
  const afterRange = listItem.slice(endIndex + 1);
  const movedItem = afterRange.shift();
  return movedItem
    ? [...beforeRange, movedItem, ...range, ...afterRange]
    : listItem;
}
export function transferAll({ from, to, selectedFrom, selectedTo }) {
  to = [...to.map(i => setItemSelection(i, false)), ...from.map(i => setItemSelection(i, true))];
  selectedFrom = [];
  selectedTo = [...from.map(({ id }) => id)];
  from = [];
  return { from, to, selectedFrom, selectedTo };
}
export function transferItems({ from, to, selectedFrom, selectedTo, insertIndex = to.length, }) {
  if (!from.length)
    return { from, to, selectedFrom, selectedTo };
  const itemsToTransfer = getItemsToTransfer(to, selectedFrom, from);
  if (!itemsToTransfer.length)
    return { from, to, selectedFrom, selectedTo };
  let updatedTo = to.map(i => setItemSelection(i, false));
  updatedTo.splice(insertIndex, 0, ...itemsToTransfer.map(i => setItemSelection(i, true)));
  return {
    from: from.filter(item => !selectedFrom.includes(item.id)),
    to: updatedTo,
    selectedFrom: [],
    selectedTo: [...selectedFrom],
  };
}
function getItemsToTransfer(to, selectedFrom, from) {
  const toListIds = to.map(item => item.id);
  //Não deve transferir um item que já existe na lista destino
  const itemsToTransferId = selectedFrom.filter((id) => !toListIds.includes(id));
  return from.filter(({ id }) => itemsToTransferId.includes(id));
}
function setItemSelection(item, select) {
  return Object.assign(Object.assign({}, item), { selected: select });
}
