import { writeFile } from 'node:fs/promises';
import path from 'node:path';
import { z } from 'zod';
import { inspectTables } from './inspect.js';
import { formatTableInspection, formatTablePayloadJson } from './output.js';
import { createInspectPayload } from '../inspect/shared.js';
import { writeStream } from '../utils.js';
import { parsePositiveInt, parseHeaderRow } from '../cli-validation.js';
const TableCommandOptionsSchema = z
    .object({
    format: z.string().optional(),
    output: z.string().optional(),
    ci: z.boolean().optional(),
    table: z.string().optional(),
    sampleRows: z.string().optional(),
    headerRow: z.string().optional(),
})
    .passthrough();
function getOptions(command) {
    return TableCommandOptionsSchema.parse(command.optsWithGlobals());
}
async function emit(output, options) {
    if (options.output) {
        await writeFile(options.output, output);
        return;
    }
    await writeStream(process.stdout, output);
}
export function registerTableCommands(program) {
    const table = program.command('table').description('Extract and inspect table content from documents');
    const inspect = table
        .command('inspect <file>')
        .description('Extract structured table data from DOCX, XLSX, PPTX, ODT, or ODP documents');
    inspect
        .option('--format <type>', 'output format: tabular or json', 'tabular')
        .option('-o, --output <file>', 'write output to file')
        .option('--ci', 'ASCII-only output, no colors')
        .option('--table <n>', 'extract specific table (1-based index)')
        .option('--sample-rows <n>', 'max rows per table', '20')
        .option('--header-row <mode>', 'header detection: auto, none, or row number', 'auto');
    inspect.action(async (file, _options, command) => {
        const options = getOptions(command);
        const resolved = path.resolve(file);
        const inspectOptions = {
            sampleRows: parsePositiveInt(options.sampleRows, 20, 'sample row count'),
            table: options.table ? parsePositiveInt(options.table, 1, 'table number') : undefined,
            headerRow: parseHeaderRow(options.headerRow),
        };
        const result = await inspectTables(resolved, inspectOptions);
        const payload = createInspectPayload(resolved, {
            command: 'table.inspect',
            sampleRows: inspectOptions.sampleRows,
            table: inspectOptions.table,
            headerRow: inspectOptions.headerRow,
        }, result);
        const output = options.format === 'json' ? formatTablePayloadJson(payload) : formatTableInspection(result, options.ci);
        await emit(output.endsWith('\n') ? output : `${output}\n`, options);
    });
}
//# sourceMappingURL=command.js.map