import { ClipCreationIntent, StudioProjectState } from '../types';

export default function updateQueuedGeneration(
  queuedGenerationId: string,
  updateFunction: (
    clipCreationIntent: ClipCreationIntent
  ) => ClipCreationIntent | null
) {
  return (state: StudioProjectState): StudioProjectState => ({
    ...state,
    tracks: state.tracks.map((t) => {
      if (
        t.clipCreationIntents.some((c) =>
          c.queuedGenerationIds.includes(queuedGenerationId)
        )
      ) {
        return {
          ...t,
          clipCreationIntents: t.clipCreationIntents
            .map((c) =>
              c.queuedGenerationIds.includes(queuedGenerationId)
                ? updateFunction(c)
                : c
            )
            .filter(Boolean) as ClipCreationIntent[],
        };
      } else {
        return t;
      }
    }),
  });
}
