Functions/Public/Reset-Stopwatch.ps1

function Reset-Stopwatch {
    <#
    .SYNOPSIS
        Resets (clears) stopwatch data.
 
    .DESCRIPTION
        Clears running stopwatches, completed results, or both.
        Useful for starting fresh timing sessions or cleaning up after tests.
 
    .PARAMETER Name
        Optional. The name of a specific stopwatch to reset.
        Supports wildcards. If not specified, resets all stopwatches.
 
    .PARAMETER Running
        If specified, only clears running (not yet stopped) stopwatches.
 
    .PARAMETER Completed
        If specified, only clears completed stopwatch results.
 
    .PARAMETER All
        Clears both running and completed stopwatches. This is the default
        when no switches are specified.
 
    .PARAMETER PassThru
        Returns the stopwatch data that was cleared before removal.
 
    .OUTPUTS
        None by default. PSCustomObject[] if -PassThru is specified.
 
    .EXAMPLE
        Reset-Stopwatch
        # Clears all stopwatch data
 
    .EXAMPLE
        Reset-Stopwatch -Name "Build*"
        # Clears all stopwatches matching "Build*"
 
    .EXAMPLE
        Reset-Stopwatch -Completed
        # Clears only completed results, keeps running stopwatches
 
    .EXAMPLE
        $old = Reset-Stopwatch -PassThru
        # Clears all and returns the cleared data
 
    .NOTES
        Author: Sune Alexandersen Narud
        Version: 1.0.0
        Date: February 2026
 
    .LINK
        Start-Stopwatch
        Stop-Stopwatch
        Get-Stopwatch
        Show-StopwatchSummary
    #>


    [CmdletBinding(SupportsShouldProcess)]
    [OutputType([void], [PSCustomObject[]])]
    param(
        [Parameter(Position = 0)]
        [SupportsWildcards()]
        [string]$Name = '*',

        [Parameter()]
        [switch]$Running,

        [Parameter()]
        [switch]$Completed,

        [Parameter()]
        [switch]$All,

        [Parameter()]
        [switch]$PassThru
    )

    # If no switches specified, default to All
    if (-not $Running -and -not $Completed) {
        $All = $true
    }

    $cleared = @()

    # Clear running stopwatches
    if ($Running -or $All) {
        if ($Global:OrionStopwatches) {
            $keysToRemove = @($Global:OrionStopwatches.Keys | Where-Object { $_ -like $Name })
            foreach ($key in $keysToRemove) {
                if ($PSCmdlet.ShouldProcess($key, 'Clear running stopwatch')) {
                    if ($PassThru) {
                        $startTime = $Global:OrionStopwatches[$key]
                        $elapsed = [DateTime]::Now - $startTime
                        $cleared += [PSCustomObject]@{
                            Name         = $key
                            Status       = 'Running'
                            StartTime    = $startTime
                            Duration     = $elapsed
                            TotalSeconds = $elapsed.TotalSeconds
                        }
                    }
                    $Global:OrionStopwatches.Remove($key)
                }
            }

            # Clean up empty dictionary
            if ($Global:OrionStopwatches.Count -eq 0) {
                Remove-Variable -Name OrionStopwatches -Scope Global -ErrorAction SilentlyContinue
            }
        }
    }

    # Clear completed results
    if ($Completed -or $All) {
        if ($Global:OrionStopwatchResults) {
            $keysToRemove = @($Global:OrionStopwatchResults.Keys | Where-Object { $_ -like $Name })
            foreach ($key in $keysToRemove) {
                if ($PSCmdlet.ShouldProcess($key, 'Clear completed stopwatch result')) {
                    if ($PassThru) {
                        $result = $Global:OrionStopwatchResults[$key]
                        $cleared += [PSCustomObject]@{
                            Name         = $result.Name
                            Status       = 'Completed'
                            StartTime    = $result.StartTime
                            EndTime      = $result.EndTime
                            Duration     = $result.Duration
                            TotalSeconds = $result.TotalSeconds
                        }
                    }
                    $Global:OrionStopwatchResults.Remove($key)
                }
            }

            # Clean up empty dictionary
            if ($Global:OrionStopwatchResults.Count -eq 0) {
                Remove-Variable -Name OrionStopwatchResults -Scope Global -ErrorAction SilentlyContinue
            }
        }
    }

    if ($PassThru) {
        return $cleared
    }
}