import type { Requirement } from './requirement';

type RequirementVersionPatch = Pick<Partial<Requirement>, 'versionId' | 'addedToVersionBy' | 'status'>;
const VERSION_SCOPED_REQUIREMENT_STATUSES = new Set<Requirement['status']>(['planned', 'developing', 'testing', 'released']);

export function getRequirementVersionUnlinkPatch(
  requirement: Pick<Requirement, 'status'>,
): RequirementVersionPatch {
  return {
    versionId: undefined,
    addedToVersionBy: undefined,
    ...(VERSION_SCOPED_REQUIREMENT_STATUSES.has(requirement.status) ? { status: 'adopted' as const } : {}),
  };
}

export function getRequirementVersionSelectionPatch(
  requirement: Pick<Requirement, 'status' | 'addedToVersionBy'>,
  versionId?: string,
  addedBy?: string,
): RequirementVersionPatch {
  if (!versionId) {
    return getRequirementVersionUnlinkPatch(requirement);
  }

  return {
    versionId,
    addedToVersionBy: addedBy || requirement.addedToVersionBy,
  };
}
