/**
 * `dql slack serve` — boot the Slack slash-command bot.
 *
 *   dql slack serve [--port 3479] [--provider claude|openai|gemini|ollama]
 *
 * Reads `SLACK_SIGNING_SECRET` (required) + `SLACK_BOT_TOKEN` (optional, only
 * needed for outbound posts beyond response_url replies). The bot answers
 * `/dql ask <q>` and `/dql block <id>` via the existing block-first agent loop,
 * so Slack reuses the same Certified/Uncertified semantics as the desktop UI.
 */
import { findProjectRoot } from '../local-runtime.js';
export async function runSlack(sub, _rest, flags) {
    if (sub !== 'serve') {
        throw new Error('Usage: dql slack serve [--port 3479] [--provider claude|openai|gemini|ollama]');
    }
    const projectRoot = findProjectRoot(process.cwd());
    const signingSecret = process.env.SLACK_SIGNING_SECRET;
    if (!signingSecret) {
        throw new Error('SLACK_SIGNING_SECRET is required. Set it in your environment before running `dql slack serve`.');
    }
    const port = parsePort(flags.port);
    const provider = flags.provider;
    const { startSlackServer } = await import('@duckcodeailabs/dql-slack');
    const server = await startSlackServer({
        projectRoot,
        signingSecret,
        port,
        provider,
    });
    console.log(`  ✓ Slack bot listening on http://127.0.0.1:${server.port}`);
    console.log(`    POST /slack/commands  (slash commands)`);
    console.log(`    POST /slack/actions   (block-kit interactivity)`);
    console.log(`    GET  /health`);
    console.log('\n  Forward Slack to this port via ngrok or a similar tunnel.');
    // Keep the process alive on Ctrl+C.
    for (const sig of ['SIGINT', 'SIGTERM']) {
        process.on(sig, async () => {
            console.log('\n  Shutting down…');
            await server.close();
            process.exit(0);
        });
    }
}
function parsePort(raw) {
    if (raw === null || raw === undefined || raw === '')
        return 3479;
    const n = typeof raw === 'number' ? raw : Number.parseInt(raw, 10);
    if (!Number.isFinite(n) || n < 1 || n > 65535) {
        throw new Error(`Invalid --port "${String(raw)}"`);
    }
    return n;
}
//# sourceMappingURL=slack.js.map