function Remove-PersonaConditionNode { <# .SYNOPSIS Removes a condition or nested group from a rule's condition tree (FR-028). .DESCRIPTION Two structural rules are enforced here rather than left to schema validation, because both would otherwise produce a document the schema calls invalid with no indication which editor action caused it: - The root group itself cannot be removed - a rule's `match` is required (RE-001). Delete the rule instead (Remove-PersonaConfigRule). - A group's last remaining condition cannot be removed - an empty `conditions` array violates the schema's `minItems: 1` on conditionGroup. Remove the parent group instead, or add a replacement first. .PARAMETER Group The rule's `match` condition group (the root). .PARAMETER Path Path (see Get-PersonaConditionNode) to the node to remove. Must not be empty. .OUTPUTS System.Object - the same Group, mutated in place, returned for convenience. #> [CmdletBinding()] [OutputType([object])] param( [Parameter(Mandatory)] [object] $Group, [Parameter(Mandatory)] [AllowEmptyCollection()] [int[]] $Path ) if (@($Path).Count -eq 0) { throw "The root condition group cannot be removed this way - delete the rule instead." } # Special-cased at length 1 because PowerShell's range operator treats 0..-1 as # the two-element descending range (0, -1), not an empty range - the naive slice # would silently pick up the wrong parent for a top-level path. $parentPath = (@($Path).Count -eq 1) ? @() : @($Path)[0..($Path.Count - 2)] $index = @($Path)[-1] $parent = Get-PersonaConditionNode -Group $Group -Path $parentPath $children = @($parent.conditions) if ($index -lt 0 -or $index -ge $children.Count) { throw "Path segment $index is out of range - this group has $($children.Count) condition(s)." } if ($children.Count -eq 1) { throw 'This is the only condition in its group. A group requires at least one condition - remove the group itself instead, or add a replacement first.' } # Removed by position, not by value or reference - two structurally identical # sibling conditions are otherwise indistinguishable. $remaining = [System.Collections.Generic.List[object]]::new() for ($i = 0; $i -lt $children.Count; $i++) { if ($i -ne $index) { $remaining.Add($children[$i]) } } $parent.conditions = $remaining.ToArray() $Group }