$ErrorActionPreference = "Stop"

$root = Split-Path -Parent $MyInvocation.MyCommand.Path
$indexPath = Join-Path $root "WEB\index.html"

if (-not (Test-Path $indexPath)) {
  Add-Type -AssemblyName PresentationFramework
  [System.Windows.MessageBox]::Show(
    "Le dossier WEB\index.html est introuvable a cote du lanceur FactureOS.",
    "ReFone FactureOS",
    "OK",
    "Error"
  ) | Out-Null
  exit 1
}

$edgeCandidates = @(
  (Join-Path $env:ProgramFiles "Microsoft\Edge\Application\msedge.exe"),
  (Join-Path ${env:ProgramFiles(x86)} "Microsoft\Edge\Application\msedge.exe")
) | Where-Object { $_ -and (Test-Path $_) }

$appUri = [System.Uri]::new((Resolve-Path $indexPath))

if ($edgeCandidates.Count -gt 0) {
  Start-Process -FilePath $edgeCandidates[0] -ArgumentList "--app=$($appUri.AbsoluteUri)"
  exit 0
}

Start-Process -FilePath $appUri.AbsoluteUri
