export function insertAfter<T>(items: T[], index: number, item: T): T[] {
  const insertionIndex = Math.max(0, Math.min(items.length, index + 1));
  return [...items.slice(0, insertionIndex), item, ...items.slice(insertionIndex)];
}

export function moveArrayItem<T>(items: T[], fromIndex: number, toIndex: number): T[] {
  if (fromIndex < 0 || fromIndex >= items.length || toIndex < 0 || toIndex >= items.length || fromIndex === toIndex) {
    return items;
  }
  const next = [...items];
  const [item] = next.splice(fromIndex, 1);
  next.splice(toIndex, 0, item);
  return next;
}
