import { execFile } from 'node:child_process';
import { mkdir } from 'node:fs/promises';
import { basename, dirname, extname, resolve } from 'node:path';
import { promisify } from 'node:util';
const execFileAsync = promisify(execFile);
/**
 * Derive a default `.gresource` target path from the XML descriptor.
 * `foo.gresource.xml` → `foo.gresource` in the same directory.
 */
function defaultTargetFor(xmlPath) {
    const ext = extname(xmlPath);
    const stem = basename(xmlPath, ext);
    return resolve(dirname(xmlPath), stem);
}
export const gresourceCommand = {
    command: 'gresource <xml>',
    description: 'Compile a GResource XML descriptor into a binary .gresource bundle (wraps `glib-compile-resources`).',
    builder: (yargs) => {
        return yargs
            .positional('xml', {
            description: 'Path to the .gresource.xml descriptor',
            type: 'string',
            normalize: true,
            demandOption: true,
        })
            .option('sourcedir', {
            description: 'Directory containing the resource files referenced by <xml>',
            type: 'string',
            normalize: true,
        })
            .option('target', {
            alias: 't',
            description: 'Output .gresource file (default: <xml-without-.xml> next to <xml>)',
            type: 'string',
            normalize: true,
        })
            .option('verbose', {
            description: 'Print the underlying glib-compile-resources invocation',
            type: 'boolean',
            default: false,
        });
    },
    handler: async (args) => {
        const xmlPath = resolve(args.xml);
        const target = args.target ? resolve(args.target) : defaultTargetFor(xmlPath);
        const sourcedir = args.sourcedir ? resolve(args.sourcedir) : dirname(xmlPath);
        const cmdArgs = [`--sourcedir=${sourcedir}`, `--target=${target}`, xmlPath];
        if (args.verbose) {
            console.log(`[gjsify gresource] glib-compile-resources ${cmdArgs.join(' ')}`);
        }
        // Ensure the target directory exists — glib-compile-resources writes
        // a temporary file next to the target and fails with ENOENT otherwise.
        await mkdir(dirname(target), { recursive: true });
        try {
            const { stdout, stderr } = await execFileAsync('glib-compile-resources', cmdArgs);
            if (stdout)
                process.stdout.write(stdout);
            if (stderr)
                process.stderr.write(stderr);
            if (args.verbose) {
                console.log(`[gjsify gresource] wrote ${target}`);
            }
        }
        catch (err) {
            const e = err;
            if (e?.code === 'ENOENT') {
                console.error('[gjsify gresource] glib-compile-resources not found. Install it via your distro (package: glib2-devel / libglib2.0-dev).');
            }
            else {
                if (e?.stderr)
                    process.stderr.write(e.stderr);
                console.error(`[gjsify gresource] glib-compile-resources failed${e?.code !== undefined ? ` (exit ${e.code})` : ''}`);
            }
            process.exitCode = typeof e?.code === 'number' ? e.code : 1;
        }
    },
};
