#!/usr/bin/env node
/**
 * Copy `dictionary-en/index.aff` + `index.dic` into `client/lib/dict/`
 * so the compose iframe can `fetch()` them via msger's custom protocol
 * (msger only serves files under `contentDir = client/`). Done at build
 * time rather than runtime so a fresh install doesn't need to read the
 * dictionary from node_modules — the published package ships the dict.
 */
import fs from "node:fs";
import path from "node:path";

const root = path.resolve(import.meta.dirname, "..");
const src = path.join(root, "node_modules", "dictionary-en");
const dst = path.join(root, "client", "lib", "dict");

if (!fs.existsSync(path.join(src, "index.aff"))) {
    console.error(`build-spellcheck-dict: source not found at ${src} — run npm install first`);
    process.exit(1);
}
fs.mkdirSync(dst, { recursive: true });
for (const name of ["index.aff", "index.dic"]) {
    const dstName = name.replace("index", "en");
    fs.copyFileSync(path.join(src, name), path.join(dst, dstName));
}
console.log(`build-spellcheck-dict: copied dictionary-en → ${path.relative(root, dst)}/{en.aff,en.dic}`);
