Public/Metaverse/Get-JIMMetaverseAttribute.ps1

# Copyright (c) Tetron Limited. All rights reserved.
# Licensed under the Tetron Commercial License. See LICENSE file in the project root.

function Get-JIMMetaverseAttribute {
    <#
    .SYNOPSIS
        Gets Metaverse Attributes from JIM.
 
    .DESCRIPTION
        Retrieves Metaverse Attribute definitions from JIM. Can retrieve all attributes
        or a specific attribute by ID or name.
 
    .PARAMETER Id
        The ID of the attribute to get.
 
    .PARAMETER Name
        The name of the attribute to get, matched exactly but ignoring case.
 
    .PARAMETER Page
        The page of attributes to return, counting from 1.
 
    .PARAMETER PageSize
        How many attributes each page holds.
 
    .OUTPUTS
        By default, one page of attributes, each in summary with the Object Types it is bound to. With -Id or -Name,
        the attribute in full, with its rendering hint and Standard Mappings.
 
    .EXAMPLE
        Get-JIMMetaverseAttribute
 
        Gets all Metaverse Attributes.
 
    .EXAMPLE
        Get-JIMMetaverseAttribute -Id 1
 
        Gets the Attribute with ID 1.
 
    .EXAMPLE
        Get-JIMMetaverseAttribute -Name 'DisplayName'
 
        Gets the Attribute named 'DisplayName'.
 
    .LINK
        Get-JIMMetaverseObject
        Get-JIMMetaverseObjectType
    #>

    [CmdletBinding(DefaultParameterSetName = 'List')]
    [OutputType('JIM.MetaverseAttributeHeader', ParameterSetName = 'List')]
    [OutputType('JIM.MetaverseAttributeDetailDto', ParameterSetName = 'ById')]
    [OutputType('JIM.MetaverseAttributeDetailDto', ParameterSetName = 'ByName')]
    param(
        [Parameter(Mandatory, ParameterSetName = 'ById', ValueFromPipelineByPropertyName)]
        [int]$Id,

        [Parameter(Mandatory, ParameterSetName = 'ByName')]
        [ValidateNotNullOrEmpty()]
        [string]$Name,

        [Parameter(ParameterSetName = 'List')]
        [ValidateRange(1, [int]::MaxValue)]
        [int]$Page = 1,

        [Parameter(ParameterSetName = 'List')]
        [ValidateRange(1, 1000)]
        [int]$PageSize = 100
    )

    process {
        # Check connection first
        if (-not $script:JIMConnection) {
            Write-Error "You are not connected to JIM. Run Connect-JIM -Url <your JIM URL> to authenticate, then try again."
            return
        }

        # Resolve name to ID if using ByName parameter set
        if ($PSCmdlet.ParameterSetName -eq 'ByName') {
            try {
                $resolvedAttr = Resolve-JIMMetaverseAttribute -Name $Name
                $Id = $resolvedAttr.id
            }
            catch {
                Write-Error $_
                return
            }
        }

        switch ($PSCmdlet.ParameterSetName) {
            { $_ -in 'ById', 'ByName' } {
                Write-Verbose "Getting Metaverse Attribute with ID: $Id"
                $result = Invoke-JIMApi -Endpoint "/api/v1/metaverse/attributes/$Id"
                $result
            }

            'List' {
                Write-Verbose "Getting all Metaverse Attributes"
                $queryParams = @(
                    "page=$Page",
                    "pageSize=$PageSize"
                )
                $queryString = $queryParams -join '&'
                $response = Invoke-JIMApi -Endpoint "/api/v1/metaverse/attributes?$queryString"

                # Handle paginated response
                $attributes = if ($response.items) { $response.items } else { $response }

                # Output each attribute individually for pipeline support
                foreach ($attr in $attributes) {
                    $attr
                }
            }
        }
    }
}