#!/usr/bin/env bash
# Builds the wasm demo into the repo root: pkg/ (wasm-pack output) and
# default-fonts.js (base64 default fonts the HTML examples reference).
# Used by .github/workflows/static.yml (Pages deploy), demo-e2e.yml (headless
# tests) and locally: bash scripts/build-demo.sh
set -euo pipefail
cd "$(dirname "$0")/.."

wasm-pack build --target web --no-pack --no-typescript --release \
  --no-default-features --features "html,js-sys,gif,jpeg,png,pnm,tiff,bmp,webp"

{
  echo "// GENERATED by scripts/build-demo.sh — do not edit, not checked in."
  echo "export const DEFAULT_FONTS = {"
  for f in Helvetica.ttf Helvetica-Bold.ttf Helvetica-Oblique.ttf \
           Helvetica-BoldOblique.ttf Times.ttf Courier.ttf \
           RobotoMedium.ttf NotoSansJP-Regular.otf; do
    p="examples/assets/fonts/$f"
    [ -f "$p" ] || { echo "missing $p" >&2; exit 1; }
    printf '  "%s": "%s",\n' "$f" "$(base64 -w0 "$p")"
  done
  echo "};"
} > default-fonts.js

echo "demo built: pkg/ ($(du -sh pkg | cut -f1)) + default-fonts.js ($(du -sh default-fonts.js | cut -f1))"
