Private/UI/Invoke-EwWorkbenchLibraryRegistration.ps1

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

function Invoke-EwWorkbenchLibraryRegistration {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)][Collections.IDictionary]$State,
        [Parameter()][Collections.IDictionary]$Parameters
    )

    try {
        Invoke-EwWorkbenchLibraryRegistrationState -State $State
        if (-not $State.AddToLibraryButton.IsEnabled) {
            Write-EwWorkbenchLog -State $State -Message ([string]$State.AddToLibraryButton.ToolTip) -Level Warning
            return
        }
        if ($null -eq $Parameters) {
            $filters = @{}
            foreach ($key in $State.ArtifactFilterControls.Keys) {
                $values = @($State.ArtifactFilterControls[$key].CheckBoxes |
                    Where-Object { [bool]$_.IsChecked } | ForEach-Object { [string]$_.Tag })
                if ($values.Count -gt 0) { $filters[$key] = $values }
            }
            $Parameters = @{ Path = [string]$State.LibraryPathBox.Text; Name = [string]$State.CurrentApp.Name; Filter = $filters }
        }
        if ([string]$State.LibraryPathBox.Text -cne $Parameters.Path -or [string]$State.CurrentApp.Name -cne $Parameters.Name) {
            throw 'The selected Application or library changed. Review the selection and add again.'
        }
        $State.LibraryRegistrationContext = $Parameters
        Invoke-EwWorkbenchJob -State $State -Kind LibraryRegistration -Name "Save library registration for $($Parameters.Name)" -ArgumentList @($Parameters) -ScriptBlock {
            param($Registration)
            try {
                $plan = & (Get-Module -Name EvergreenUI) {
                    param($InputParameters)
                    $parameters = @{ Path = $InputParameters.Path; Name = $InputParameters.Name; Filter = $InputParameters.Filter }
                    foreach ($key in 'RegistrationName', 'Replace') {
                        if ($InputParameters.Contains($key)) { $parameters[$key] = $InputParameters[$key] }
                    }
                    Get-EwLibraryRegistrationPlan @parameters
                } $Registration
            }
            catch [ArgumentException] {
                if (-not $Registration.Contains('RegistrationName') -or $Registration.Contains('Replace')) { throw }
                $validationError = $_.Exception.Message
                $plan = & (Get-Module -Name EvergreenUI) {
                    param($InputParameters)
                    Get-EwLibraryRegistrationPlan -Path $InputParameters.Path -Name $InputParameters.Name -Filter $InputParameters.Filter
                } $Registration
                return [PSCustomObject]@{
                    Plan = $plan; Result = $null; Items = @(); RefreshError = ''
                    InitialName = $Registration.RegistrationName; ValidationError = $validationError
                }
            }
            if ($plan.Status -in @('Updated', 'ChoiceRequired') -and -not $Registration.Contains('Replace')) {
                return [PSCustomObject]@{ Plan = $plan; Result = $null; Items = @(); RefreshError = ''; InitialName = ''; ValidationError = '' }
            }
            if (-not $Registration.Contains('ExpectedLibraryHash')) { $Registration.ExpectedLibraryHash = $plan.LibraryHash }
            $result = Add-EwLibraryApp @Registration -Confirm:$false -ErrorAction Stop
            $items = @()
            $refreshError = ''
            try { $items = @(Get-EwLibrary -Path $Registration.Path -ErrorAction Stop) }
            catch { $refreshError = $_.Exception.Message }
            [PSCustomObject]@{ Plan = $null; Result = $result; Items = $items; RefreshError = $refreshError; InitialName = ''; ValidationError = '' }
        }
    }
    catch {
        $State.LibraryRegistrationContext = $null
        Write-EwWorkbenchLog -State $State -Message "Could not save library registration: $($_.Exception.Message)" -Level Error
    }
}