Functions/GenXdev.Data.Preferences/Get-GenXdevPreference.ps1

################################################################################
<#
.SYNOPSIS
Retrieves a preference value from the GenXdev preferences store.
 
.DESCRIPTION
The function implements a two-tier preference retrieval system. It first checks
the local store for a preference value. If not found, it falls back to the
default store. If still not found, returns the provided default value.
 
.PARAMETER Name
The name of the preference to retrieve from the preference stores.
 
.PARAMETER DefaultValue
The fallback value to return if the preference is not found in any store.
 
.PARAMETER SessionOnly
Use alternative settings stored in session for Data preferences like Language,
Database paths, etc.
 
.PARAMETER ClearSession
Clear the session setting (Global variable) before retrieving.
 
.PARAMETER PreferencesDatabasePath
Database path for preference data files.
 
.PARAMETER SkipSession
Dont use alternative settings stored in session for Data preferences like
Language, Database paths, etc.
 
.EXAMPLE
Get-GenXdevPreference -Name "Theme" -DefaultValue "Dark"
 
.EXAMPLE
getPreference "Theme" "Dark"
#>

function Get-GenXdevPreference {

    [CmdletBinding(
        DefaultParameterSetName = 'Default'
    )]
    [Alias("getPreference")]
    param(
        ###############################################################################
        [Parameter(
            Mandatory = $true,
            Position = 0,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage = "The name of the preference to retrieve"
        )]
        [ValidateNotNullOrEmpty()]
        [Alias("PreferenceName")]
        [string] $Name,
        ###############################################################################
        [Parameter(
            Mandatory = $false,
            Position = 1,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage = "The default value if preference is not found"
        )]
        [AllowNull()]
        [AllowEmptyString()]
        [Alias("DefaultPreference")]
        [string] $DefaultValue,
        ###############################################################################
        [Parameter(
            Mandatory = $false,
            HelpMessage = ("Database path for preference data files")
        )]
        [string] $PreferencesDatabasePath,
        ###############################################################################
        [Parameter(
            Mandatory = $false,
            HelpMessage = ("Use alternative settings stored in session for Data " +
                "preferences like Language, Database paths, etc")
        )]
        [switch] $SessionOnly,
        ###############################################################################
        [Parameter(
            Mandatory = $false,
            HelpMessage = ("Clear the session setting (Global variable) before " +
                "retrieving")
        )]
        [switch] $ClearSession,
        ###############################################################################
        [Parameter(
            Mandatory = $false,
            HelpMessage = ("Dont use alternative settings stored in session for " +
                "Data preferences like Language, Database paths, etc")
        )]
        [Alias("FromPreferences")]
        [switch] $SkipSession
        ###############################################################################
    )

    begin {

        # copy identical parameter values to prepare for database path lookup
        $params = GenXdev.Helpers\Copy-IdenticalParamValues `
            -BoundParameters $PSBoundParameters `
            -FunctionName "GenXdev.Data\Get-GenXdevPreferencesDatabasePath" `
            -DefaultValues (Microsoft.PowerShell.Utility\Get-Variable `
                -Scope Local `
                -ErrorAction SilentlyContinue)

        # resolve the actual database path using the helper function
        $preferencesDatabasePath = `
            GenXdev.Data\Get-GenXdevPreferencesDatabasePath @params

        # create global variable name for this preference
        $globalVariableName = "GenXdevPreference_$Name"

        # output verbose information about the database path being used
        Microsoft.PowerShell.Utility\Write-Verbose `
            "Using database path: $preferencesDatabasePath"

        Microsoft.PowerShell.Utility\Write-Verbose `
            "Starting preference retrieval for: $Name"

        # handle clearing session variables first if requested
        if ($ClearSession) {
            Microsoft.PowerShell.Utility\Set-Variable `
                -Name $globalVariableName `
                -Value $null `
                -Scope Global `
                -Force
            Microsoft.PowerShell.Utility\Write-Verbose `
                "Cleared session preference setting: $globalVariableName"
        }
    }

    process {

        # initialize the return value
        $value = $null

        # check global variable first (unless SkipSession is specified)
        if (-not $SkipSession) {
            $globalValue = Microsoft.PowerShell.Utility\Get-Variable `
                -Name $globalVariableName `
                -Scope Global `
                -ValueOnly `
                -ErrorAction SilentlyContinue

            if (-not ([string]::IsNullOrEmpty($globalValue))) {
                # use global variable if available and not empty
                Microsoft.PowerShell.Utility\Write-Verbose `
                    "Found session value for '$Name': $globalValue"
                return $globalValue
            }
        }

        # fallback to persistent preferences (unless SessionOnly is specified)
        if (-not $SessionOnly) {

            # fallback to persistent preferences when not session only
            try {
                Microsoft.PowerShell.Utility\Write-Verbose `
                    "Checking local store for preference '$Name'"

                # copy identical parameter values for Get-ValueByKeyFromStore (local)
                $getLocalParams = GenXdev.Helpers\Copy-IdenticalParamValues `
                    -BoundParameters $PSBoundParameters `
                    -FunctionName "GenXdev.Data\Get-ValueByKeyFromStore" `
                    -DefaultValues (Microsoft.PowerShell.Utility\Get-Variable `
                        -Scope Local `
                        -ErrorAction SilentlyContinue)

                # assign specific parameters for local store retrieval
                $getLocalParams.StoreName = "GenXdev.PowerShell.Preferences"
                $getLocalParams.KeyName = $Name
                $getLocalParams.SynchronizationKey = "Local"
                $getLocalParams.DatabasePath = $preferencesDatabasePath
                $getLocalParams.ErrorAction = "Stop"

                # attempt to get value from local store
                $value = GenXdev.Data\Get-ValueByKeyFromStore @getLocalParams

                # if local store lookup failed, try defaults store
                if ([string]::IsNullOrEmpty($value)) {
                    Microsoft.PowerShell.Utility\Write-Verbose `
                        "Preference not found locally, checking defaults store"

                    # copy identical parameter values for Get-ValueByKeyFromStore (defaults)
                    $getDefaultParams = GenXdev.Helpers\Copy-IdenticalParamValues `
                        -BoundParameters $PSBoundParameters `
                        -FunctionName "GenXdev.Data\Get-ValueByKeyFromStore" `
                        -DefaultValues (Microsoft.PowerShell.Utility\Get-Variable `
                            -Scope Local `
                            -ErrorAction SilentlyContinue)

                    # assign specific parameters for defaults store retrieval
                    $getDefaultParams.StoreName = "GenXdev.PowerShell.Preferences"
                    $getDefaultParams.KeyName = $Name
                    $getDefaultParams.SynchronizationKey = "Defaults"
                    $getDefaultParams.DatabasePath = $preferencesDatabasePath
                    $getDefaultParams.ErrorAction = "Stop"

                    # attempt to get value from defaults store
                    $value = GenXdev.Data\Get-ValueByKeyFromStore @getDefaultParams
                }

                # if we found a value in persistent storage, return it
                if (-not ([string]::IsNullOrEmpty($value))) {
                    Microsoft.PowerShell.Utility\Write-Verbose "Returning persistent value: $value"
                    return $value
                }
            }
            catch {
                Microsoft.PowerShell.Utility\Write-Verbose `
                    "Error accessing preference stores: $_"
            }
        }

        # if no value found in any store, use provided default
        Microsoft.PowerShell.Utility\Write-Verbose `
            "Using provided default value: $DefaultValue"
        return $DefaultValue
    }

    end {
    }
}
################################################################################