# 把 mobile-src.html 的 CDN 引用内联为 vendor 本地内容，生成自包含 mobile-test.html
# 路径基于本脚本所在目录（项目根），可在任意机器/位置运行。
$root = $PSScriptRoot
$h = Get-Content "$root\mobile-src.html" -Raw -Encoding UTF8
$h = $h.Replace('<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@xterm/xterm@5.5.0/css/xterm.min.css" />', '<style>' + (Get-Content "$root\vendor\xterm.min.css" -Raw -Encoding UTF8) + '</style>')
$h = $h.Replace('<script src="https://cdn.jsdelivr.net/npm/@xterm/xterm@5.5.0/lib/xterm.min.js"></script>', '<script>' + (Get-Content "$root\vendor\xterm.min.js" -Raw -Encoding UTF8) + '</script>')
$h = $h.Replace('<script src="https://cdn.jsdelivr.net/npm/@xterm/addon-fit@0.10.0/lib/addon-fit.min.js"></script>', '<script>' + (Get-Content "$root\vendor\addon-fit.min.js" -Raw -Encoding UTF8) + '</script>')
$h = $h.Replace('<script src="https://cdn.jsdelivr.net/npm/@xterm/addon-webgl@0.18.0/lib/addon-webgl.min.js"></script>', '<script>' + (Get-Content "$root\vendor\addon-webgl.min.js" -Raw -Encoding UTF8) + '</script>')
$h = $h.Replace('<script src="https://cdn.jsdelivr.net/npm/@xterm/addon-unicode11@0.8.0/lib/addon-unicode11.min.js"></script>', '<script>' + (Get-Content "$root\vendor\addon-unicode11.min.js" -Raw -Encoding UTF8) + '</script>')
$h = $h.Replace('<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@highlightjs/cdn-assets@11.10.0/styles/github-dark.min.css" />', '<style>' + (Get-Content "$root\vendor\hljs-theme.css" -Raw -Encoding UTF8) + '</style>')
$h = $h.Replace('<script src="https://cdn.jsdelivr.net/npm/@highlightjs/cdn-assets@11.10.0/highlight.min.js"></script>', '<script>' + (Get-Content "$root\vendor\highlight.min.js" -Raw -Encoding UTF8) + '</script>')
$h = $h.Replace('<script src="https://cdn.jsdelivr.net/npm/marked@12.0.2/marked.min.js"></script>', '<script>' + (Get-Content "$root\vendor\marked.min.js" -Raw -Encoding UTF8) + '</script>')
$h = $h.Replace('<script src="https://cdn.jsdelivr.net/npm/dompurify@3.1.6/dist/purify.min.js"></script>', '<script>' + (Get-Content "$root\vendor\purify.min.js" -Raw -Encoding UTF8) + '</script>')
Set-Content "$root\mobile-test.html" -Value $h -Encoding UTF8 -NoNewline
$ext = (Select-String -Path "$root\mobile-test.html" -Pattern '(src|href)="https?://' -AllMatches).Matches.Count
Write-Output ("生成 mobile-test.html: " + [math]::Round((Get-Item "$root\mobile-test.html").Length/1KB, 0) + "KB, 外部引用 " + $ext)

# 语法门禁：抽出全部内联脚本块逐个过 node --check，任一报错即中止（防止带病页面上线）。
# 教训：v1.3.0 曾因单行 if-else 无分号语法错误导致整页空白，curl 查不出、编译期也发现不了。
$blocks = [regex]::Matches($h, '(?s)<script>(.*?)</script>')
$node = (Get-Command node -ErrorAction SilentlyContinue).Source
if (-not $node) { Write-Error '语法门禁需要 node（未在 PATH 找到）'; exit 1 }
$tmp = Join-Path $env:TEMP 'ccgui-mt-check'
Remove-Item $tmp -Recurse -Force -ErrorAction SilentlyContinue
New-Item $tmp -ItemType Directory | Out-Null
$fail = $false
for ($i = 0; $i -lt $blocks.Count; $i++) {
    $f = Join-Path $tmp ("block-$i.js")
    [System.IO.File]::WriteAllText($f, $blocks[$i].Groups[1].Value, (New-Object System.Text.UTF8Encoding($false)))
    & $node --check $f 2>&1 | Out-Null
    if ($LASTEXITCODE -ne 0) {
        $fail = $true
        Write-Output ("脚本块 $i 语法错误：")
        & $node --check $f 2>&1 | Select-Object -First 6
    }
}
Remove-Item $tmp -Recurse -Force -ErrorAction SilentlyContinue
if ($fail) { Write-Error 'mobile-test.html 存在 JS 语法错误，已中止（产物不可用，请修复后重跑）'; exit 1 }
Write-Output ("语法门禁通过（" + $blocks.Count + " 个内联脚本块）")
