#!/usr/bin/env node
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { parseArgs } from './c4s/args.js';
import { CliError } from './c4s/errors.js';
import { writeError } from './c4s/output.js';
import { runInlineMention } from './c4s/commands/inline-mention.js';
import { runSingleElement } from './c4s/commands/single-element.js';
import { runElementList } from './c4s/commands/element-list.js';
import { runTaggedList } from './c4s/commands/tagged-list.js';
import { runTaggedListMixed } from './c4s/commands/tagged-list-mixed.js';
import { runDetail } from './c4s/commands/detail.js';
import { runCatalog } from './c4s/commands/catalog.js';
import { runListTags } from './c4s/commands/list-tags.js';
import { runListSlugs } from './c4s/commands/list-slugs.js';
import { runResolve } from './c4s/commands/resolve.js';
import { runAsk } from './c4s/commands/ask.js';
const HELP = `Usage: c4s <command> [options]

Tag commands (1:1 with XML tag names):
  inline_mention --type <t> --slug <s>
  single_element --type <t> --slug <s>
  element_list --type <t> --slugs <s1,s2,...>
  tagged_list --type <t> --tags <t1,t2,...> [--filter and|or]
  tagged_list_mixed --tags <t1,t2,...> [--filter and|or]

Detail view (no XML counterpart):
  detail --type <t> --slug <s>

Utility:
  resolve <file.md> [--format inline|json]

Agent Q&A (requires a running \`npx claude4spec\` server):
  ask "<msg>" --ct chat
  ask "<msg>" --ct brief --brief <path>
  ask "<msg>" --thread <id>
    --server <url>    override server discovery (remote / one-off --port)

Discovery:
  catalog
  list-tags
  list-slugs --type <t>

Global flags:
  --project <path>    override project dir (default: walk-up to .claude4spec/)
  --format json|text  output format (default: json; resolve default: inline)
  --compact           minified JSON (for pipelines)
  --sort-keys         deterministic key order in JSON
  --version           print c4s version
  --help              show this help
`;
async function main() {
    const argv = process.argv.slice(2);
    if (argv.length === 0 || argv[0] === '--help' || argv[0] === '-h') {
        process.stdout.write(HELP);
        return;
    }
    if (argv[0] === '--version' || argv[0] === '-v') {
        process.stdout.write(`c4s ${readPackageVersion()}\n`);
        return;
    }
    const args = parseArgs(argv);
    if (!args.command) {
        throw new CliError('UNKNOWN_COMMAND', 'no command given', 'run `c4s --help`');
    }
    switch (args.command) {
        case 'inline_mention':
            return runInlineMention(args);
        case 'single_element':
            return runSingleElement(args);
        case 'element_list':
            return runElementList(args);
        case 'tagged_list':
            return runTaggedList(args);
        case 'tagged_list_mixed':
            return runTaggedListMixed(args);
        case 'detail':
            return runDetail(args);
        case 'resolve':
            return runResolve(args);
        case 'catalog':
            return runCatalog(args);
        case 'list-tags':
            return runListTags(args);
        case 'list-slugs':
            return runListSlugs(args);
        case 'ask':
            return runAsk(args);
        default:
            throw new CliError('UNKNOWN_COMMAND', `unknown command '${args.command}'`, 'run `c4s --help`');
    }
}
function readPackageVersion() {
    try {
        const here = path.dirname(fileURLToPath(import.meta.url));
        const candidates = [
            path.resolve(here, '..', 'package.json'),
            path.resolve(here, '..', '..', 'package.json'),
        ];
        for (const pkgPath of candidates) {
            if (fs.existsSync(pkgPath)) {
                const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
                if (pkg.version)
                    return pkg.version;
            }
        }
    }
    catch {
        /* ignore */
    }
    return 'unknown';
}
main().catch((err) => {
    if (err instanceof CliError) {
        writeError(err);
        process.exit(codeToExit(err.code));
    }
    const message = err instanceof Error ? err.message : String(err);
    writeError(new CliError('UNKNOWN_COMMAND', message));
    process.exit(1);
});
function codeToExit(code) {
    switch (code) {
        case 'PROJECT_NOT_FOUND':
            return 2;
        case 'ENTITY_NOT_FOUND':
            return 3;
        case 'INVALID_TYPE':
        case 'INVALID_ARGS':
            return 4;
        case 'FILE_NOT_FOUND':
            return 5;
        case 'SCHEMA_OUT_OF_DATE':
            return 6;
        default:
            return 1;
    }
}
//# sourceMappingURL=c4s.js.map