package jsbundler

import (
	"fmt"
	"os"
	"path/filepath"
	"testing"
)

func benchRead(b *testing.B, rel string) string {
	b.Helper()
	data, err := os.ReadFile(filepath.Join("..", "..", "frontend", "src", rel))
	if err != nil {
		b.Skipf("cannot read %s: %v", rel, err)
	}
	return string(data)
}

var benchComponents = []string{
	"ui/AutoTable.tsx",              // ~212 KB
	"ui/Forms.tsx",                  // ~75 KB
	"pages/app/user/Profile.tsx",    // ~26 KB
	"pages/public/PublicLayout.tsx", // ~27 KB
}

// BenchmarkCompileDev times the full dev HMR compile (Babel solid + solid-refresh
// in goja) per component, busting the cache each iteration so every run is a real
// recompile — the cost the browser waits on for a single-file HMR update. This is
// the harness for judging whether a compile-path change actually moves the needle;
// as of this writing the goja/Babel transform dominates (seconds for large files).
func BenchmarkCompileDev(b *testing.B) {
	for _, rel := range benchComponents {
		src := benchRead(b, rel)
		b.Run(rel, func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				id := fmt.Sprintf("bench/c%d.tsx", i) // unique id -> cache miss
				if _, err := CompileDev(src, id); err != nil {
					b.Fatal(err)
				}
			}
		})
	}
}
