"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleLogs = void 0;
const docker_1 = require("../../docker");
const ui_1 = require("../../utils/ui");
const options_1 = require("./options");
const KNOWN_FLAGS = ['--host', '--follow', '--service'];
async function handleLogs(args) {
    await (0, ui_1.initUI)();
    const parsed = (0, options_1.parseDockerLogsOptions)(args, KNOWN_FLAGS);
    const errors = [
        ...parsed.errors,
        ...(0, options_1.collectUnexpectedDockerArgs)(parsed.remainingArgs, {
            knownFlags: ['--follow'],
            maxPositionals: 0,
        }),
    ];
    if (errors.length > 0) {
        console.error((0, ui_1.box)((0, ui_1.fail)(errors.join('\n')), { title: 'Docker', padding: 1 }));
        process.exitCode = 1;
        return;
    }
    const executor = new docker_1.DockerExecutor();
    try {
        if (parsed.follow) {
            console.log((0, ui_1.info)(`Following Docker logs${parsed.host ? ` on ${parsed.host}` : ''}...`));
            await executor.logs({
                host: parsed.host,
                follow: true,
                service: parsed.service,
            });
            return;
        }
        const output = await executor.logs({
            host: parsed.host,
            follow: false,
            service: parsed.service,
        });
        console.log(output ?? '');
    }
    catch (error) {
        console.error((0, ui_1.box)((0, ui_1.fail)(error instanceof Error ? error.message : String(error)), {
            title: 'Docker',
            padding: 1,
        }));
        process.exitCode = 1;
    }
}
exports.handleLogs = handleLogs;
//# sourceMappingURL=logs-subcommand.js.map