import type { AppState, StateRepository } from '@domain/ports/StateRepository';

export interface ReorderEnclosCommand {
  type: 'reorder-enclos';
  fromIndex: number;
  toIndex: number;
}

export function createReorderEnclosHandler(state: AppState, repo: StateRepository) {
  return (cmd: ReorderEnclosCommand): void => {
    if (cmd.fromIndex < 0 || cmd.toIndex < 0) return;
    if (cmd.fromIndex >= state.enclos.length || cmd.toIndex >= state.enclos.length) return;
    const [moved] = state.enclos.splice(cmd.fromIndex, 1);
    if (moved) state.enclos.splice(cmd.toIndex, 0, moved);
    repo.save(state);
  };
}
