import type { Column } from "../types";

export const reorderColumns = (
  columns: Column[],
  activeId: string,
  overId: string,
): Column[] => {
  const oldIndex = columns.findIndex((column) => column.id === activeId);
  const newIndex = columns.findIndex((column) => column.id === overId);

  if (oldIndex === -1 || newIndex === -1 || oldIndex === newIndex) {
    return columns;
  }

  const result = [...columns];
  const [removed] = result.splice(oldIndex, 1);
  result.splice(newIndex, 0, removed);
  return result.map((column, index) => ({ ...column, order: index + 1 }));
};
