All files / tools/src/stateManagement/segmentation removeSegment.ts

0% Statements 0/22
0% Branches 0/9
0% Functions 0/4
0% Lines 0/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88                                                                                                                                                                               
import { getActiveSegmentIndex } from './getActiveSegmentIndex';
import { getSegmentation } from './getSegmentation';
import { getSegmentationRepresentations } from './getSegmentationRepresentation';
import { getViewportIdsWithSegmentation } from './getViewportIdsWithSegmentation';
import { clearSegmentValue } from './helpers/clearSegmentValue';
import { setActiveSegmentIndex } from './segmentIndex';
import { updateSegmentations } from './updateSegmentations';
 
/**
 * Removes a segment from a segmentation.
 *
 * @param segmentationId - The unique identifier of the segmentation.
 * @param segmentIndex - The index of the segment to be removed.
 * @param options - Additional options for segment removal.
 * @param options.setNextSegmentAsActive - Whether to set the next available segment as active after removal. Defaults to true.
 *
 * @remarks
 * This function performs the following steps:
 * 1. Clears the segment value from the segmentation data.
 * 2. Removes the segment from the list of segments.
 * 3. If the removed segment was active and setNextSegmentAsActive is true, it sets the next or previous segment as active.
 * 4. Updates the segmentation state with the modified segments.
 *
 */
export function removeSegment(
  segmentationId: string,
  segmentIndex: number,
  options: {
    setNextSegmentAsActive: boolean;
  } = {
    setNextSegmentAsActive: true,
  }
) {
  clearSegmentValue(segmentationId, segmentIndex);
 
  const isThisSegmentActive =
    getActiveSegmentIndex(segmentationId) === segmentIndex;
 
  const segmentation = getSegmentation(segmentationId);
  const { segments } = segmentation;
 
  // remove the segment from the list
  delete segments[segmentIndex];
 
  const updatedSegments = {
    ...segments,
  };
 
  updateSegmentations([
    {
      segmentationId,
      payload: {
        segments: updatedSegments,
      },
    },
  ]);
 
  if (isThisSegmentActive && options.setNextSegmentAsActive) {
    // set the next or previous segment as active
    const segmentIndices = Object.keys(segments)
      .map(Number)
      .sort((a, b) => a - b);
    const currentIndex = segmentIndices.indexOf(segmentIndex);
 
    const nextSegmentIndex = segmentIndices[currentIndex + 1];
    const previousSegmentIndex = segmentIndices[currentIndex - 1];
 
    if (nextSegmentIndex !== undefined) {
      setActiveSegmentIndex(segmentationId, nextSegmentIndex);
    } else if (previousSegmentIndex !== undefined) {
      setActiveSegmentIndex(segmentationId, previousSegmentIndex);
    }
  }
 
  // remove the segment from the viewport representations
  const viewportIds = getViewportIdsWithSegmentation(segmentationId);
 
  viewportIds.forEach((viewportId) => {
    const representations = getSegmentationRepresentations(viewportId, {
      segmentationId,
    });
 
    representations.forEach((representation) => {
      delete representation.segments[segmentIndex];
    });
  });
}