GittHelpers.psm1

# GittHelpers.psm1 — module loader

$Private = Get-ChildItem -Path "$PSScriptRoot\Private\*.ps1" -ErrorAction SilentlyContinue
$Public  = Get-ChildItem -Path "$PSScriptRoot\Public\*.ps1"  -ErrorAction SilentlyContinue

foreach ($file in @($Private) + @($Public)) {
    try   { . $file.FullName }
    catch { Write-Error "Failed to import $($file.FullName): $_" }
}

# ── Argument completers ────────────────────────────────────────────────────────

# Helper: resolve bare repo root from current location (best-effort)
function _GittHelpers_GetRoot {
    try { Get-GitRoot -Path (Get-Location).Path } catch { $null }
}

# Branch name completer (for SourceBranch, ProtectedBranches, etc.)
$branchCompleter = {
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
    $root = _GittHelpers_GetRoot
    if (-not $root) { return }
    git -C $root branch --format '%(refname:short)' 2>$null |
        Where-Object { $_ -like "$wordToComplete*" } |
        ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) }
}

# Worktree path completer
$worktreeCompleter = {
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
    $root = _GittHelpers_GetRoot
    if (-not $root) { return }
    $wts = try { Get-WorktreeList -RepoPath $root } catch { return }
    $wts |
        Where-Object { -not $_.IsBare -and $_.Path -like "$wordToComplete*" } |
        ForEach-Object {
            $label = Split-Path $_.Path -Leaf
            [System.Management.Automation.CompletionResult]::new($_.Path, $label, 'ParameterValue', "$($_.Branch) — $($_.Path)")
        }
}

# gitt subcommand completer
$gittSubcommandCompleter = {
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
    @('clone','worktree','save','sync','prune') |
        Where-Object { $_ -like "$wordToComplete*" } |
        ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) }
}

# gitt worktree second-arg completer (add/remove)
$gittWorktreeActionCompleter = {
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
    @('add','remove') |
        Where-Object { $_ -like "$wordToComplete*" } |
        ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) }
}

Register-ArgumentCompleter -CommandName 'New-GitWorktree'          -ParameterName 'SourceBranch'       -ScriptBlock $branchCompleter
Register-ArgumentCompleter -CommandName 'Remove-StaleGitBranches'  -ParameterName 'ProtectedBranches'  -ScriptBlock $branchCompleter
Register-ArgumentCompleter -CommandName 'Remove-GitWorktree'       -ParameterName 'Path'               -ScriptBlock $worktreeCompleter
Register-ArgumentCompleter -CommandName 'Save-GitWorktree'         -ParameterName 'Path'               -ScriptBlock $worktreeCompleter
Register-ArgumentCompleter -CommandName 'Sync-GitBranches'         -ParameterName 'RepoPath'           -ScriptBlock $worktreeCompleter
Register-ArgumentCompleter -CommandName 'Invoke-Gitt'              -ParameterName 'Subcommand'         -ScriptBlock $gittSubcommandCompleter
Register-ArgumentCompleter -CommandName 'gitt'                     -ParameterName 'Subcommand'         -ScriptBlock $gittSubcommandCompleter