#!/usr/bin/env node
"use strict";

// CLI wrapper for jso-protector-electron compileDirectory.

const fs = require("node:fs");
const path = require("node:path");
const electron = require("../index.js");

async function main(argv) {
    const opts = parseArgs(argv);
    if (opts.help) { printHelp(); return 0; }
    if (!opts.input)  { process.stderr.write("--input <dir> is required\n");  return 2; }
    if (!opts.output) { process.stderr.write("--output <dir> is required\n"); return 2; }

    try {
        const result = await electron.compileDirectory({
            input: opts.input,
            output: opts.output,
            mode: opts.mode || "auto",
            extensions: opts.extensions,
        });
        if (opts.json) {
            process.stdout.write(JSON.stringify({
                mode: result.mode,
                processed: result.processed.length,
                skipped: result.skipped.length,
                manifest: result.manifestPath,
            }, null, 2) + "\n");
        } else {
            process.stderr.write("jso-protector-electron: mode=" + result.mode + " processed=" + result.processed.length + " skipped=" + result.skipped.length + " manifest=" + result.manifestPath + "\n");
            if (result.skipped.length > 0) {
                for (const s of result.skipped) {
                    process.stderr.write("  SKIPPED " + s.in + ": " + s.reason + "\n");
                }
            }
        }
        return 0;
    } catch (x) {
        process.stderr.write("jso-protector-electron: " + x.message + "\n");
        return 1;
    }
}

function parseArgs(argv) {
    const out = { extensions: undefined };
    for (let i = 0; i < argv.length; i++) {
        const a = argv[i];
        switch (a) {
        case "-h": case "--help":    out.help = true; break;
        case "--input":              out.input = argv[++i]; break;
        case "--output":             out.output = argv[++i]; break;
        case "--mode":               out.mode = argv[++i]; break;
        case "--extensions":         out.extensions = String(argv[++i]).split(",").map(function (s) { return s.trim(); }).filter(Boolean); break;
        case "--json":               out.json = true; break;
        case "--version":            process.stdout.write(require("../package.json").version + "\n"); process.exit(0); break;
        default:
            if (a.startsWith("--")) { process.stderr.write("Unknown flag: " + a + "\n"); out.help = true; break; }
        }
    }
    return out;
}

function printHelp() {
    process.stdout.write(
`jso-protector-electron --input <dir> --output <dir> [options]

Compile JSO-protected JavaScript files to V8 bytecode (.jsc) for
Electron consumption. Pair with jso-protector to layer obfuscation
plus bytecode: V8-specific bytecode is FAR harder to reverse than
even minified-obfuscated JS, and binds the artefact to the target
Electron release.

Options:
  --input <dir>       Source directory (typically jso-protector's --output)
  --output <dir>      Where to write .jsc + bytecode.manifest.json
  --mode <m>          'auto' (default) | 'embedded' | 'scaffold-only'
                      embedded:        actually emit .jsc (requires Electron)
                      scaffold-only:   emit .js + .bytecode-meta.json
                      auto:            embedded under Electron, else scaffold
  --extensions <list> Comma-separated extensions to compile (default .js)
  --json              Emit a JSON summary on stdout
  --version           Print package version
  -h, --help          Show this help

Embedded mode (Electron in your CI):
  electron $(npm bin)/jso-protector-electron \\
    --input dist-protected --output dist-bytecode

Scaffold-only (preview the build matrix from plain Node):
  jso-protector-electron \\
    --input dist-protected --output dist-bytecode --mode scaffold-only
`);
}

if (require.main === module) {
    main(process.argv.slice(2)).then(function (code) { process.exit(code); });
}

module.exports = { main: main };
