PowerShellUniversal.Component.BarcodeScanner.psm1

# Register JavaScript assets with PowerShell Universal
$AssetId = $null
Get-ChildItem "$PSScriptRoot\*.js" | ForEach-Object {
    $Item = [UniversalDashboard.Services.AssetService]::Instance.RegisterAsset($_.FullName)
    if ($_.Name.StartsWith("index.") -and $_.Name.EndsWith(".bundle.js")) {
        $AssetId = $Item
    }
}


function New-UDBarcodeScanner {
    <#
.SYNOPSIS
Creates a new barcode/ISBN scanner component for PowerShell Universal apps.

.DESCRIPTION
This component uses the device camera to scan barcodes and ISBN numbers from books.
When a barcode is detected, it triggers the OnScan event with the scanned data.

.PARAMETER Id
The ID of the component. If not specified, a new GUID will be generated.

.PARAMETER OnScan
A script block that will be executed when a barcode is scanned.
The scanned data will be available in $EventData.

.PARAMETER Width
The width of the scanner component. Default is '100%'.

.PARAMETER Height
The height of the scanner component in pixels. Default is 400.

.PARAMETER FacingMode
The camera to use. 'environment' for back camera (default), 'user' for front camera.

.PARAMETER ScanDelay
The delay between scans in milliseconds. Default is 500.

.PARAMETER Enabled
Whether the scanner is enabled. Default is $true.

.EXAMPLE
New-UDBarcodeScanner -OnScan {
    $ISBN = $EventData
    Show-UDToast -Message "Scanned ISBN: $ISBN" -Duration 5000
    
    # Add to database or session variable
    $Session:ScannedBooks += $ISBN
}

.EXAMPLE
# With custom styling
New-UDBarcodeScanner -Width '500px' -Height 300 -FacingMode 'environment' -OnScan {
    # Look up book information
    $BookInfo = Invoke-RestMethod -Uri "https://openlibrary.org/isbn/$EventData.json"
    Show-UDToast -Message "Found: $($BookInfo.title)" -Duration 5000
    
    # Store in database
    Invoke-SqlCmd -Query "INSERT INTO Books (ISBN, Title) VALUES (@ISBN, @Title)" `
                  -Parameters @{ISBN = $EventData; Title = $BookInfo.title}
}
#>

    param(
        [Parameter()]
        [string]$Id = (New-Guid).ToString(),
        
        [Parameter()]
        [Endpoint]$OnScan,
        
        [Parameter()]
        [string]$Width = '100%',
        
        [Parameter()]
        [int]$Height = 400,
        
        [Parameter()]
        [ValidateSet('environment', 'user')]
        [string]$FacingMode = 'environment',
        
        [Parameter()]
        [int]$ScanDelay = 500,
        
        [Parameter()]
        [bool]$Enabled = $true,
        
        [Parameter()]
        [hashtable]$ContainerStyle = @{},
        
        [Parameter()]
        [hashtable]$VideoStyle = @{}
    )

    # Register the endpoint if provided
    if ($OnScan) {
        $OnScan.Register($Id, $PSCmdlet)
    }

    @{
        assetId        = $AssetId 
        isPlugin       = $true 
        type           = "ud-barcodescanner"
        id             = $Id
        onScan         = $OnScan
        width          = $Width
        height         = $Height
        facingMode     = $FacingMode
        scanDelay      = $ScanDelay
        enabled        = $Enabled
        containerStyle = $ContainerStyle
        videoStyle     = $VideoStyle
    }
}