"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractImages = extractImages;
exports.isSingleVariantRequestBatch = isSingleVariantRequestBatch;
exports.isMixedVariantBatch = isMixedVariantBatch;
exports.isStaticPreviewRefineBatch = isStaticPreviewRefineBatch;
const index_core_1 = require("../index-core");
const log = (0, index_core_1.createLogger)('ChangeBatchClassification');
/**
 * Extract base64 images from comment/chat changes into separate MCP image
 * content items. Returns the changes with `images` stripped (replaced by
 * `imageCount`) and an array of image content items to append to the tool
 * response. All other fields on the change item — including the optional
 * `variantRequest` marker on comments — pass through untouched.
 */
function extractImages(changes) {
    const imageContent = [];
    const strippedChanges = changes.map((change) => {
        if ((change.kind === 'comment' || change.kind === 'chat') &&
            change.images?.length) {
            let extractedCount = 0;
            for (const img of change.images) {
                const match = img.match(/^data:([^;]+);base64,(.+)$/s);
                if (match) {
                    imageContent.push({
                        type: 'image',
                        data: match[2],
                        mimeType: match[1],
                    });
                    extractedCount++;
                }
                else {
                    const preview = img.slice(0, 80);
                    log.warn(`Failed to extract image from ${change.kind} change: not a valid data URI (starts with "${preview}")`);
                }
            }
            const { images: _images, ...rest } = change;
            void _images;
            return { ...rest, imageCount: extractedCount };
        }
        return change;
    });
    return { changes: strippedChanges, imageContent };
}
/**
 * Returns true when an intent's pending batch is a single CommentChangeItem
 * carrying `variantRequest`. Such batches are routed through the variants
 * lifecycle instead of `beginApplyingRun`.
 */
function isSingleVariantRequestBatch(intent) {
    if (intent.changes.length !== 1)
        return false;
    const item = intent.changes[0];
    return item.kind === 'comment' && Boolean(item.variantRequest);
}
/**
 * Returns true when a batch contains at least one item with `variantRequest`
 * but is NOT a clean single-item variant-request batch. UI gating should make
 * this unreachable; the server boundary still rejects defensively.
 */
function isMixedVariantBatch(intent) {
    if (intent.changes.length <= 1)
        return false;
    return intent.changes.some((c) => c.kind === 'comment' && Boolean(c.variantRequest));
}
/** Returns true when a pending batch targets an HTML-only static preview. */
function isStaticPreviewRefineBatch(intent) {
    if (intent.staticPreviewTarget)
        return true;
    return intent.changes.some((change) => change.kind === 'comment' && Boolean(change.staticPreviewTarget));
}
//# sourceMappingURL=changeBatchClassification.js.map