function removeCollectionDefinition({
  updateInProgressMatchUps = true,
  tournamentRecord,
  drawDefinition,
  tieFormatName,
  collectionId,
  structureId,
  matchUpId,
  eventId,
  event,
}) {
  let result = getTieFormat({
    drawDefinition,
    structureId,
    matchUpId,
    eventId,
    event,
  });
  if (result.error) return result;

  const { matchUp, structure, tieFormat: existingTieFormat } = result;
  const originalValueGoal = existingTieFormat.winCriteria.valueGoal;
  const tieFormat = copyTieFormat(existingTieFormat);

  result = validateTieFormat({ tieFormat });
  if (!result.valid) return { error: INVALID_VALUES, errors: result.errors };

  const collectionExists = tieFormat?.collectionDefinitions?.find(
    (collectionDefinition) => collectionDefinition.collectionId === collectionId
  );
  if (!collectionExists) return { error: NOT_FOUND, collectionId };

  tieFormat.collectionDefinitions = tieFormat.collectionDefinitions.filter(
    (collectionDefinition) => collectionDefinition.collectionId !== collectionId
  );

  // calculate new winCriteria for tieFormat
  // if existing winCriteria is aggregateValue, retain
  const { aggregateValue, valueGoal } = calculateWinCriteria({
    collectionDefinitions: tieFormat.collectionDefinitions,
  });

  tieFormat.winCriteria = { aggregateValue, valueGoal };

  // if valueGoal has changed, force renaming of the tieFormat
  if (originalValueGoal && originalValueGoal !== valueGoal) {
    if (tieFormatName) {
      tieFormat.tieFormatName = tieFormatName;
    } else {
      delete tieFormat.tieFormatName;
    }
  }

  // check all scoped lineUps in the drawDefinition to identify collectionAssignments
  let matchUps = [];

  if (matchUpId && matchUp) {
    matchUps = [matchUp];
  } else if (structureId && structure) {
    matchUps = getAllStructureMatchUps({
      matchUpFilters: { matchUpTypes: [TEAM] },
      structure,
    })?.matchUps;
  } else if (drawDefinition) {
    matchUps = allDrawMatchUps({
      matchUpFilters: { matchUpTypes: [TEAM] },
      drawDefinition,
    })?.matchUps;
  }

  // all team matchUps in the structure which are completed or which have a tieFormat should not be modified
  const targetMatchUps = matchUps.filter(
    (matchUp) =>
      !matchUp.tieFormat &&
      ![COMPLETED, IN_PROGRESS].includes(matchUp.matchUpStatus) &&
      !matchUp.winningSide &&
      !(!updateInProgressMatchUps && scoreHasValue(matchUp))
  );

  const deletedMatchUpIds = [];
  for (const matchUp of targetMatchUps) {
    // remove any collectionAssignments from LineUps that include collectionId
    for (const side of matchUp?.sides || []) {
      side.lineUp = (side.lineUp || []).map((assignment) => ({
        participantId: assignment.participantId,
        collectionAssignments: (assignment?.collectionAssignments || []).filter(
          (collectionAssignment) =>
            collectionAssignment.collectionId !== collectionId
        ),
      }));
    }

    // delete any tieMatchUps that contain collectionId
    matchUp.tieMatchUps = (matchUp.tieMatchUps || []).filter((matchUp) => {
      const deleteTarget = matchUp.collectionId === collectionId;
      if (deleteTarget) deletedMatchUpIds.push(matchUp.matchUpId);
      return !deleteTarget;
    });

    if (matchUp.tieFormat) matchUp.tieFormat = copyTieFormat(tieFormat);

    modifyMatchUpNotice({
      tournamentId: tournamentRecord?.tournamentId,
      eventId: event?.eventId,
      drawDefinition,
      matchUp,
    });
  }

  // remove any matchUps which contain collectionId
  if (deletedMatchUpIds.length) {
    // notify subscribers that matchUps have been deleted
    deleteMatchUpsNotice({
      tournamentId: tournamentRecord?.tournamentId,
      matchUpIds: deletedMatchUpIds,
      eventId: event?.eventId,
      drawDefinition,
    });
  }

  if (eventId) {
    event.tieFormat = tieFormat;
    // NOTE: there is not a modifyEventNotice
  } else if (structure) {
    structure.tieFormat = tieFormat;
  } else if (drawDefinition) {
    drawDefinition.tieFormat = tieFormat;
  } else if (!matchUp || !drawDefinition) {
    return { error: MISSING_DRAW_DEFINITION };
  }

  modifyDrawNotice({ drawDefinition, eventId: event?.eventId });

  return { ...SUCCESS, tieFormat, targetMatchUps, deletedMatchUpIds };
}
/*
 * collectionDefinition will be removed from an event tieFormat (if present)
 * if a matchUpId is provided, will be removed from matchUp.tieFormat
 * if a structureId is provided, will be removed from structure.tieFormat
 * TODO: determine whether all contained instances of tieFormat should be updated
 */
collectionDefinition will be removed from an event tieFormat (if present)
if a matchUpId is provided, will be removed from matchUp.tieFormat
if a structureId is provided, will be removed from structure.tieFormat
TODO: determine whether all contained instances of tieFormat should be updated
collectionDefinition will be removed from an event tieFormat ( if present ) if a matchUpId is provided will be removed from matchUp . tieFormat if a structureId is provided will be removed from structure . tieFormat TODO : determine whether all contained instances of tieFormat should be updated
function removeCollectionDefinition({
  updateInProgressMatchUps = true,
  tournamentRecord,
  drawDefinition,
  tieFormatName,
  collectionId,
  structureId,
  matchUpId,
  eventId,
  event,
}) {
  let result = getTieFormat({
    drawDefinition,
    structureId,
    matchUpId,
    eventId,
    event,
  });
  if (result.error) return result;
  const { matchUp, structure, tieFormat: existingTieFormat } = result;
  const originalValueGoal = existingTieFormat.winCriteria.valueGoal;
  const tieFormat = copyTieFormat(existingTieFormat);
  result = validateTieFormat({ tieFormat });
  if (!result.valid) return { error: INVALID_VALUES, errors: result.errors };
  const collectionExists = tieFormat?.collectionDefinitions?.find(
    (collectionDefinition) => collectionDefinition.collectionId === collectionId
  );
  if (!collectionExists) return { error: NOT_FOUND, collectionId };
  tieFormat.collectionDefinitions = tieFormat.collectionDefinitions.filter(
    (collectionDefinition) => collectionDefinition.collectionId !== collectionId
  );
  const { aggregateValue, valueGoal } = calculateWinCriteria({
    collectionDefinitions: tieFormat.collectionDefinitions,
  });
  tieFormat.winCriteria = { aggregateValue, valueGoal };
  if (originalValueGoal && originalValueGoal !== valueGoal) {
    if (tieFormatName) {
      tieFormat.tieFormatName = tieFormatName;
    } else {
      delete tieFormat.tieFormatName;
    }
  }
  let matchUps = [];
  if (matchUpId && matchUp) {
    matchUps = [matchUp];
  } else if (structureId && structure) {
    matchUps = getAllStructureMatchUps({
      matchUpFilters: { matchUpTypes: [TEAM] },
      structure,
    })?.matchUps;
  } else if (drawDefinition) {
    matchUps = allDrawMatchUps({
      matchUpFilters: { matchUpTypes: [TEAM] },
      drawDefinition,
    })?.matchUps;
  }
  const targetMatchUps = matchUps.filter(
    (matchUp) =>
      !matchUp.tieFormat &&
      ![COMPLETED, IN_PROGRESS].includes(matchUp.matchUpStatus) &&
      !matchUp.winningSide &&
      !(!updateInProgressMatchUps && scoreHasValue(matchUp))
  );
  const deletedMatchUpIds = [];
  for (const matchUp of targetMatchUps) {
    for (const side of matchUp?.sides || []) {
      side.lineUp = (side.lineUp || []).map((assignment) => ({
        participantId: assignment.participantId,
        collectionAssignments: (assignment?.collectionAssignments || []).filter(
          (collectionAssignment) =>
            collectionAssignment.collectionId !== collectionId
        ),
      }));
    }
    matchUp.tieMatchUps = (matchUp.tieMatchUps || []).filter((matchUp) => {
      const deleteTarget = matchUp.collectionId === collectionId;
      if (deleteTarget) deletedMatchUpIds.push(matchUp.matchUpId);
      return !deleteTarget;
    });
    if (matchUp.tieFormat) matchUp.tieFormat = copyTieFormat(tieFormat);
    modifyMatchUpNotice({
      tournamentId: tournamentRecord?.tournamentId,
      eventId: event?.eventId,
      drawDefinition,
      matchUp,
    });
  }
  if (deletedMatchUpIds.length) {
    deleteMatchUpsNotice({
      tournamentId: tournamentRecord?.tournamentId,
      matchUpIds: deletedMatchUpIds,
      eventId: event?.eventId,
      drawDefinition,
    });
  }
  if (eventId) {
    event.tieFormat = tieFormat;
  } else if (structure) {
    structure.tieFormat = tieFormat;
  } else if (drawDefinition) {
    drawDefinition.tieFormat = tieFormat;
  } else if (!matchUp || !drawDefinition) {
    return { error: MISSING_DRAW_DEFINITION };
  }
  modifyDrawNotice({ drawDefinition, eventId: event?.eventId });
  return { ...SUCCESS, tieFormat, targetMatchUps, deletedMatchUpIds };
}
function removeCollectionDefinition ( { updateInProgressMatchUps = true , tournamentRecord , drawDefinition , tieFormatName , collectionId , structureId , matchUpId , eventId , event , } ) { let result = getTieFormat ( { drawDefinition , structureId , matchUpId , eventId , event , } ) ; if ( result . error ) return result ; const { matchUp , structure , tieFormat : existingTieFormat } = result ; const originalValueGoal = existingTieFormat . winCriteria . valueGoal ; const tieFormat = copyTieFormat ( existingTieFormat ) ; result = validateTieFormat ( { tieFormat } ) ; if ( ! result . valid ) return { error : INVALID_VALUES , errors : result . errors } ; const collectionExists = tieFormat ?. collectionDefinitions ?. find ( ( collectionDefinition ) => collectionDefinition . collectionId === collectionId ) ; if ( ! collectionExists ) return { error : NOT_FOUND , collectionId } ; tieFormat . collectionDefinitions = tieFormat . collectionDefinitions . filter ( ( collectionDefinition ) => collectionDefinition . collectionId !== collectionId ) ; const { aggregateValue , valueGoal } = calculateWinCriteria ( { collectionDefinitions : tieFormat . collectionDefinitions , } ) ; tieFormat . winCriteria = { aggregateValue , valueGoal } ; if ( originalValueGoal && originalValueGoal !== valueGoal ) { if ( tieFormatName ) { tieFormat . tieFormatName = tieFormatName ; } else { delete tieFormat . tieFormatName ; } } let matchUps = [ ] ; if ( matchUpId && matchUp ) { matchUps = [ matchUp ] ; } else if ( structureId && structure ) { matchUps = getAllStructureMatchUps ( { matchUpFilters : { matchUpTypes : [ TEAM ] } , structure , } ) ?. matchUps ; } else if ( drawDefinition ) { matchUps = allDrawMatchUps ( { matchUpFilters : { matchUpTypes : [ TEAM ] } , drawDefinition , } ) ?. matchUps ; } const targetMatchUps = matchUps . filter ( ( matchUp ) => ! matchUp . tieFormat && ! [ COMPLETED , IN_PROGRESS ] . includes ( matchUp . matchUpStatus ) && ! matchUp . winningSide && ! ( ! updateInProgressMatchUps && scoreHasValue ( matchUp ) ) ) ; const deletedMatchUpIds = [ ] ; for ( const matchUp of targetMatchUps ) { for ( const side of matchUp ?. sides || [ ] ) { side . lineUp = ( side . lineUp || [ ] ) . map ( ( assignment ) => ( { participantId : assignment . participantId , collectionAssignments : ( assignment ?. collectionAssignments || [ ] ) . filter ( ( collectionAssignment ) => collectionAssignment . collectionId !== collectionId ) , } ) ) ; } matchUp . tieMatchUps = ( matchUp . tieMatchUps || [ ] ) . filter ( ( matchUp ) => { const deleteTarget = matchUp . collectionId === collectionId ; if ( deleteTarget ) deletedMatchUpIds . push ( matchUp . matchUpId ) ; return ! deleteTarget ; } ) ; if ( matchUp . tieFormat ) matchUp . tieFormat = copyTieFormat ( tieFormat ) ; modifyMatchUpNotice ( { tournamentId : tournamentRecord ?. tournamentId , eventId : event ?. eventId , drawDefinition , matchUp , } ) ; } if ( deletedMatchUpIds . length ) { deleteMatchUpsNotice ( { tournamentId : tournamentRecord ?. tournamentId , matchUpIds : deletedMatchUpIds , eventId : event ?. eventId , drawDefinition , } ) ; } if ( eventId ) { event . tieFormat = tieFormat ; } else if ( structure ) { structure . tieFormat = tieFormat ; } else if ( drawDefinition ) { drawDefinition . tieFormat = tieFormat ; } else if ( ! matchUp || ! drawDefinition ) { return { error : MISSING_DRAW_DEFINITION } ; } modifyDrawNotice ( { drawDefinition , eventId : event ?. eventId } ) ; return { ... SUCCESS , tieFormat , targetMatchUps , deletedMatchUpIds } ; }
collectionDefinition will be removed from an event tieFormat (if present)
 if a matchUpId is provided, will be removed from matchUp.tieFormat
 if a structureId is provided, will be removed from structure.tieFormat
 TODO: determine whether all contained instances of tieFormat should be updated
collectionDefinition will be removed from an event tieFormat ( if present ) if a matchUpId is provided will be removed from matchUp . tieFormat if a structureId is provided will be removed from structure . tieFormat TODO : determine whether all contained instances of tieFormat should be updated
// calculate new winCriteria for tieFormat // if existing winCriteria is aggregateValue, retain // if valueGoal has changed, force renaming of the tieFormat // check all scoped lineUps in the drawDefinition to identify collectionAssignments // all team matchUps in the structure which are completed or which have a tieFormat should not be modified // remove any collectionAssignments from LineUps that include collectionId // delete any tieMatchUps that contain collectionId // remove any matchUps which contain collectionId // notify subscribers that matchUps have been deleted // NOTE: there is not a modifyEventNotice