| 593 | } |
| 594 | |
| 595 | function applySegmentOperationsForMock( |
| 596 | data: Segment['data'], |
| 597 | operations: SegmentMembershipOperation[] |
| 598 | ): Segment['data'] { |
| 599 | const next: Segment['data'] = { |
| 600 | rules: structuredClone(data.rules ?? []), |
| 601 | include: structuredClone(data.include ?? {}), |
| 602 | exclude: structuredClone(data.exclude ?? {}), |
| 603 | }; |
| 604 | |
| 605 | for (const operation of operations) { |
| 606 | const map = next[operation.field] ?? {}; |
| 607 | const entityValues = map[operation.entity] ?? {}; |
| 608 | const values = entityValues[operation.attribute] ?? []; |
| 609 | if (operation.action === 'add') { |
| 610 | entityValues[operation.attribute] = values.concat(operation.value); |
| 611 | } else { |
| 612 | entityValues[operation.attribute] = values.filter( |
| 613 | value => value.value !== operation.value.value |
| 614 | ); |
| 615 | } |
| 616 | map[operation.entity] = entityValues; |
| 617 | next[operation.field] = map; |
| 618 | } |
| 619 | |
| 620 | return next; |
| 621 | } |