//go:build sqliteimport && !js && !wasm && !baremetal

package main

import (
	"encoding/base64"
	"fmt"
	"os"
	"strings"
	"time"
)

// writeSnapshotJS writes the tinySQL GOB snapshot as a plain JS file assigning
// a global string constant. A .js file (not JSON) keeps tiles-demo.html's
// script tags identical to the rest of cmd/query_files_wasm, which loads its
// data files the same way (see web/function_data.js in cmd/wasm_browser).
func writeSnapshotJS(path string, snapshot []byte) error {
	var b strings.Builder
	b.WriteString("// Generated by cmd/mbtilesdemo -- do not edit by hand.\n")
	fmt.Fprintf(&b, "// Regenerate with: go run -tags=sqliteimport ./cmd/mbtilesdemo\n")
	fmt.Fprintf(&b, "// Generated at %s\n", time.Now().UTC().Format(time.RFC3339))
	b.WriteString("window.TILES_DEMO_SNAPSHOT = \"")
	b.WriteString(base64.StdEncoding.EncodeToString(snapshot))
	b.WriteString("\";\n")
	return os.WriteFile(path, []byte(b.String()), 0o644)
}
