"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.artifactCommand = void 0;
const constants_1 = require("../../constants");
const debugCommands_1 = require("../debugCommands");
const debugUtils_1 = require("./util/debugUtils");
const debugFailWrapper_1 = require("./util/debugFailWrapper");
const stepsArtifactsHandler_1 = require("./util/stepsArtifactsHandler");
async function handleArtifactCommand(args) {
    (0, debugUtils_1.assertTestRunId)(args.id);
    await (0, stepsArtifactsHandler_1.handleStepArtifact)({
        testRunId: args.id,
        stepRunId: args[constants_1.CommandArgStepRunId],
        type: args.type,
        before: args.before,
        print: args.print,
        head: args.head,
        tail: args.tail,
        bytes: args.bytes,
        query: args.query,
        textOnly: args['text-only'],
        outputFormat: args.output,
        apiKey: args['api-key'],
    });
}
exports.artifactCommand = (0, debugCommands_1.defineLeaf)({
    command: 'artifact <type> <id>',
    describe: 'Get a forensic artifact (console, dom, network, screenshot) for a test run step',
    builder: (yargs) => {
        yargs
            .positional('type', {
            describe: 'Artifact type to fetch',
            choices: stepsArtifactsHandler_1.STEP_ARTIFACT_TYPES,
            demandOption: true,
        })
            .positional('id', {
            type: 'string',
            describe: 'Test run ID, e.g. abc123-jr',
            demandOption: true,
        })
            .option(constants_1.CommandArgStepRunId, {
            type: 'string',
            demandOption: true,
            describe: 'Step run ID',
        })
            .option('before', {
            type: 'boolean',
            default: false,
            describe: 'Screenshot only — fetch the before-action snapshot rather than the post-action one',
        })
            .option('print', {
            type: 'boolean',
            default: false,
            describe: 'Stream the cached artifact contents to stdout (text artifacts only)',
        })
            .option('head', {
            type: 'number',
            describe: 'Stream the first N lines of the cached artifact to stdout. Note: console / network artifacts are typically a single JSON line, so --head N will return the whole blob; use --query for JSON, --bytes for a fixed-byte prefix. Mutually exclusive with --tail/--bytes/--print/--query/--text-only',
        })
            .option('tail', {
            type: 'number',
            describe: 'Stream the last N lines of the cached artifact to stdout. Same caveat as --head: single-line JSON artifacts ignore line counts. Mutually exclusive with --head/--bytes/--print/--query/--text-only',
        })
            .option('bytes', {
            type: 'number',
            describe: 'Stream the first N bytes of the cached artifact to stdout. Mutually exclusive with --head/--tail/--print/--query/--text-only',
        })
            .option('query', {
            type: 'string',
            describe: 'Run a jq expression against the JSON artifact and stream the result to stdout. Only valid for console and network. Requires `jq` on PATH.',
        })
            .option('text-only', {
            type: 'boolean',
            default: false,
            describe: 'DOM only — emit a one-line-per-element listing (role, name, id, data-testid) instead of the raw HTML',
        });
    },
    handler: (0, debugFailWrapper_1.debugFailWrapper)(handleArtifactCommand),
});
