import { collectRecentWorkspaceArtifacts, createArtifactZipBundle, formatArtifactSummary, isTelegramImagePreview, persistWorkspaceArtifactReport, } from "../../artifacts/artifacts.js";
import { artifactDeliveryPolicy } from "../../artifacts/artifact-delivery.js";
import { isEmptyArtifactReport } from "./bot-rendering.js";
export async function deliverChannelCliArtifacts(options) {
    if (!options.config.artifactsEnabled) {
        return;
    }
    if (!options.startedAt || !options.turnId) {
        return;
    }
    if (options.state?.artifactsDeliveredForTurnId === options.turnId ||
        options.state?.artifactsDeliveryInFlightForTurnId === options.turnId) {
        return;
    }
    if (options.state) {
        options.state.artifactsDeliveryInFlightForTurnId = options.turnId;
    }
    try {
        const workspace = options.session.getInfo().workspace;
        const report = await collectRecentWorkspaceArtifacts(workspace, {
            since: options.startedAt,
            until: new Date(),
            maxFileSize: options.config.maxFileSize,
            limit: 5,
            ignoreDirs: options.config.artifactIgnoreDirs,
            ignoreGlobs: options.config.artifactIgnoreGlobs,
        });
        if (isEmptyArtifactReport(report)) {
            if (options.state)
                options.state.artifactsDeliveredForTurnId = options.turnId;
            return;
        }
        const persistedReport = await persistWorkspaceArtifactReport(workspace, options.turnId, report).catch((error) => {
            console.error(`Failed to persist ${options.logPrefix} CLI artifact report:`, error);
            return null;
        });
        const policy = options.deliveryPolicy ?? artifactDeliveryPolicy(options.autoSend ? "auto-files" : "manual-only");
        const summary = formatArtifactSummary(report.artifacts, report.skippedCount, report.omittedCount);
        if (policy.sendSummary || options.sendSummaryWhenAutoSendDisabled) {
            await options.sendSummary(summary);
        }
        if (policy.autoSendZip) {
            const zip = await createArtifactZipBundle(persistedReport?.artifacts ?? report.artifacts, persistedReport?.outDir ?? workspace, {
                maxFileSize: options.config.maxFileSize,
                bundleName: `nordrelay-artifacts-${options.turnId}.zip`,
            });
            if (zip) {
                await options.sendArtifact(zip);
            }
        }
        else if (policy.autoSendFiles) {
            const artifacts = (persistedReport?.artifacts ?? report.artifacts)
                .filter((artifact) => !policy.imagesOnly || isTelegramImagePreview(artifact))
                .slice(0, 5);
            for (const artifact of artifacts) {
                await options.sendArtifact(artifact);
            }
        }
        const info = options.session.getInfo();
        options.appendActivity({
            source: "cli",
            status: "info",
            type: policy.autoSendFiles || policy.autoSendZip ? "artifacts_sent" : "artifacts_detected",
            contextKey: options.contextKey,
            threadId: info.threadId,
            workspace: info.workspace,
            agentId: info.agentId,
            actor: { channel: "cli", label: `${info.agentLabel} CLI` },
            detail: `${policy.mode}: ${summary}`,
        });
        if (options.state)
            options.state.artifactsDeliveredForTurnId = options.turnId;
    }
    finally {
        if (options.state?.artifactsDeliveryInFlightForTurnId === options.turnId) {
            options.state.artifactsDeliveryInFlightForTurnId = null;
        }
    }
}
