import type { SprintWorkspaceStore } from './store'
import type {
  SprintWorkspaceSprint,
  SprintWorkspaceStory,
  SprintWorkspaceTask,
  SprintWorkspaceTaskDetail,
} from './types'

// G1: stable EMPTY-references zodat selectors geen nieuwe array per call retourneren.
const EMPTY_SPRINTS: SprintWorkspaceSprint[] = []
const EMPTY_STORIES: SprintWorkspaceStory[] = []
const EMPTY_TASKS: (SprintWorkspaceTask | SprintWorkspaceTaskDetail)[] = []

/**
 * Lijst-selector. Vereist `useShallow` in componenten (G2).
 */
export function selectVisibleSprints(s: SprintWorkspaceStore): SprintWorkspaceSprint[] {
  const productId = s.context.activeProduct?.id
  if (!productId) return EMPTY_SPRINTS
  const ids = s.relations.sprintIdsByProduct[productId]
  if (!ids || ids.length === 0) return EMPTY_SPRINTS
  const out: SprintWorkspaceSprint[] = []
  for (const id of ids) {
    const sprint = s.entities.sprintsById[id]
    if (sprint) out.push(sprint)
  }
  return out.length === 0 ? EMPTY_SPRINTS : out
}

/**
 * Lijst-selector. Vereist `useShallow` in componenten (G2).
 */
export function selectStoriesForActiveSprint(
  s: SprintWorkspaceStore,
): SprintWorkspaceStory[] {
  const sprintId = s.context.activeSprintId
  if (!sprintId) return EMPTY_STORIES
  const ids = s.relations.storyIdsBySprint[sprintId]
  if (!ids || ids.length === 0) return EMPTY_STORIES
  const out: SprintWorkspaceStory[] = []
  for (const id of ids) {
    const story = s.entities.storiesById[id]
    if (story) out.push(story)
  }
  return out.length === 0 ? EMPTY_STORIES : out
}

/**
 * Lijst-selector. Vereist `useShallow` in componenten (G2).
 */
export function selectTasksForActiveStory(
  s: SprintWorkspaceStore,
): (SprintWorkspaceTask | SprintWorkspaceTaskDetail)[] {
  const storyId = s.context.activeStoryId
  if (!storyId) return EMPTY_TASKS
  const ids = s.relations.taskIdsByStory[storyId]
  if (!ids || ids.length === 0) return EMPTY_TASKS
  const out: (SprintWorkspaceTask | SprintWorkspaceTaskDetail)[] = []
  for (const id of ids) {
    const task = s.entities.tasksById[id]
    if (task) out.push(task)
  }
  return out.length === 0 ? EMPTY_TASKS : out
}

/**
 * Single-value selector. `useShallow` niet vereist.
 */
export function selectActiveSprint(
  s: SprintWorkspaceStore,
): SprintWorkspaceSprint | null {
  const id = s.context.activeSprintId
  if (!id) return null
  return s.entities.sprintsById[id] ?? null
}

/**
 * Single-value selector. `useShallow` niet vereist.
 */
export function selectActiveStory(
  s: SprintWorkspaceStore,
): SprintWorkspaceStory | null {
  const id = s.context.activeStoryId
  if (!id) return null
  return s.entities.storiesById[id] ?? null
}

/**
 * Single-value selector. `useShallow` niet vereist.
 */
export function selectActiveTask(
  s: SprintWorkspaceStore,
): SprintWorkspaceTask | SprintWorkspaceTaskDetail | null {
  const id = s.context.activeTaskId
  if (!id) return null
  return s.entities.tasksById[id] ?? null
}

/**
 * Lijst-selector voor tasks binnen een specifieke story (niet per se actief).
 * Vereist `useShallow` in componenten (G2).
 */
export function selectTasksForStory(
  s: SprintWorkspaceStore,
  storyId: string,
): (SprintWorkspaceTask | SprintWorkspaceTaskDetail)[] {
  const ids = s.relations.taskIdsByStory[storyId]
  if (!ids || ids.length === 0) return EMPTY_TASKS
  const out: (SprintWorkspaceTask | SprintWorkspaceTaskDetail)[] = []
  for (const id of ids) {
    const task = s.entities.tasksById[id]
    if (task) out.push(task)
  }
  return out.length === 0 ? EMPTY_TASKS : out
}
