import type { Bug } from './bug';
import type { DevTask } from './dev-task';
import type { TestCase } from './test-case';
import type { V22WorkspaceData } from './v22-api';
import type { VersionPlan } from './version-plan';

export interface WorkspaceCollections {
  versionPlans: VersionPlan[];
  devTasks: DevTask[];
  testCases: TestCase[];
  bugs: Bug[];
}

export interface SelectWorkspaceCollectionsInput {
  v22Loaded: boolean;
  v22Failed: boolean;
  v22Data: V22WorkspaceData | null;
  appData: {
    plans: VersionPlan[];
    devTasks: DevTask[];
    testCases: TestCase[];
    bugs: Bug[];
  };
}

export function selectWorkspaceCollections({
  v22Loaded,
  v22Failed,
  v22Data,
  appData,
}: SelectWorkspaceCollectionsInput): WorkspaceCollections {
  if (v22Loaded && !v22Failed && v22Data) {
    return {
      versionPlans: v22Data.versionPlans,
      devTasks: v22Data.devTasks,
      testCases: v22Data.testCases,
      bugs: v22Data.bugs,
    };
  }

  return {
    versionPlans: appData.plans,
    devTasks: appData.devTasks,
    testCases: appData.testCases,
    bugs: appData.bugs,
  };
}
