#!/usr/bin/env node
// Copy node_modules/quill/dist/{quill.js,quill.snow.css} → client/lib/quill/
// so compose can load Quill from disk instead of jsdelivr CDN. The CDN
// fetch was a 100-500 ms (network-dependent) tax on every compose open,
// since iframes ran a fresh `await loadScript(jsdelivr…)` chain even on
// warm starts. Local copy = single msger-protocol fetch = ~30 ms.
//
// Same shape as build-tinymce.js (sibling script). Build runs after
// `npm install`, before bundling, so the assets are present when the
// browser first reaches them.
import fs from "node:fs";
import path from "node:path";

const root = path.resolve(import.meta.dirname, "..");
const srcDir = path.join(root, "node_modules", "quill", "dist");
const dstDir = path.join(root, "client", "lib", "quill");

if (!fs.existsSync(srcDir)) {
    console.error(`build-quill: source not found at ${srcDir} — run npm install first`);
    process.exit(1);
}

// Only the two files the editor actually loads — Quill's other dist
// artifacts (bubble theme, core-only build, sourcemaps) aren't on the
// runtime path and would just bloat the client tree.
const files = ["quill.js", "quill.snow.css"];
fs.mkdirSync(dstDir, { recursive: true });
let totalBytes = 0;
for (const name of files) {
    const s = path.join(srcDir, name);
    const d = path.join(dstDir, name);
    fs.copyFileSync(s, d);
    totalBytes += fs.statSync(d).size;
}
console.log(`build-quill: copied ${files.length} files (${(totalBytes / 1024).toFixed(1)} KB) → client/lib/quill/`);
