/* oxlint-disable no-console */
import { readFile } from 'node:fs/promises';
import process from 'node:process';
import { createInterface } from 'node:readline';
import { parseOptions } from "../../shared/src/options.js";
import * as v from "../../shared/src/valita.js";
import { loadSchemaAndPermissions } from "../../zero-cache/src/scripts/permissions.js";
import { mapAST } from "../../zero-protocol/src/ast.js";
import { serverToClient } from "../../zero-schema/src/name-mapper.js";
import { astToZQL } from "./ast-to-zql.js";
import { formatOutput } from "./format.js";
const options = {
    schema: {
        type: v.string().optional(),
        desc: [
            'Path to the schema file. Use this to re-map the AST to client names.',
        ],
    },
};
const config = parseOptions(options);
let schema;
if (config.schema) {
    schema = (await loadSchemaAndPermissions(config.schema)).schema;
}
function isStdinPiped() {
    return !process.stdin.isTTY;
}
function readFromStdin() {
    return new Promise(resolve => {
        let data = '';
        const rl = createInterface({
            input: process.stdin,
            output: process.stdout,
            terminal: false,
        });
        rl.on('line', line => {
            data += line + '\n';
        });
        rl.on('close', () => {
            resolve(data);
        });
    });
}
async function main() {
    try {
        let input;
        if (isStdinPiped()) {
            input = await readFromStdin();
        }
        else if (process.argv.length > 2) {
            const filePath = process.argv[2];
            input = await readFile(filePath, 'utf-8');
        }
        else {
            console.error('Error: No input provided.');
            console.error('Usage:');
            console.error(`  cat ast.json | npx ast-to-zql`);
            console.error(`  npx ast-to-zql ast.json`);
            process.exit(1);
        }
        let ast = JSON.parse(input);
        if (schema) {
            const mapper = serverToClient(schema.tables);
            ast = mapAST(ast, mapper);
        }
        const zql = astToZQL(ast);
        const code = `query.${ast.table}${zql}`;
        console.log(await formatOutput(code));
    }
    catch (error) {
        console.error('Error processing input:', error);
        process.exit(1);
    }
}
await main();
//# sourceMappingURL=bin.js.map