/**
 * Appends a numeric suffix to a filename before its extension.
 * e.g. uniquifySuffix("design-spec.pdf", 2) → "design-spec_2.pdf"
 *      uniquifySuffix("readme", 3) → "readme_3"
 */
function uniquifySuffix(filename, counter) {
    const dotIndex = filename.lastIndexOf(".");
    if (dotIndex === -1) {
        return `${filename}_${counter}`;
    }
    return `${filename.slice(0, dotIndex)}_${counter}${filename.slice(dotIndex)}`;
}
/**
 * Ensures all file-part filenames across a set of messages are globally unique.
 *
 * LLM providers like Bedrock require document names to be unique across all
 * messages in a single request. When the same attachment appears in multiple
 * turns (e.g. history + current) or the same file is referenced from both
 * promptContext and planContext, this function renames collisions with stable
 * numeric suffixes (_2, _3, …) rather than dropping any attachments.
 *
 * The first occurrence of each filename is kept as-is; subsequent collisions
 * are suffixed. Messages are shallow-cloned only when a rename is needed.
 */
export function uniquifyFilePartFilenames(messages) {
    // --- Pass 1: collect every original filename so generated suffixes
    //     never collide with a real filename that appears later. ---
    const taken = new Set();
    for (const msg of messages) {
        if (typeof msg.content === "string" || !Array.isArray(msg.content)) {
            continue;
        }
        for (const part of msg.content) {
            if (typeof part === "object" &&
                part !== null &&
                "type" in part &&
                part.type === "file" &&
                "filename" in part &&
                typeof part.filename === "string") {
                taken.add(part.filename);
            }
        }
    }
    // --- Pass 2: walk messages in order; first occurrence of each filename
    //     is kept, subsequent occurrences are given a unique suffix. ---
    const seen = new Set();
    const counters = new Map();
    let anyMutation = false;
    const result = messages.map((msg) => {
        if (typeof msg.content === "string" || !Array.isArray(msg.content)) {
            return msg;
        }
        let contentMutated = false;
        const newContent = msg.content.map((part) => {
            if (typeof part === "object" &&
                part !== null &&
                "type" in part &&
                part.type === "file" &&
                "filename" in part &&
                typeof part.filename === "string") {
                const filePart = part;
                const filename = filePart.filename;
                if (!seen.has(filename)) {
                    // First occurrence — keep original name.
                    seen.add(filename);
                    return part;
                }
                // Collision — find a suffixed name that is not yet taken.
                let counter = (counters.get(filename) ?? 1) + 1;
                let candidate = uniquifySuffix(filename, counter);
                while (taken.has(candidate) || seen.has(candidate)) {
                    counter++;
                    candidate = uniquifySuffix(filename, counter);
                }
                counters.set(filename, counter);
                seen.add(candidate);
                taken.add(candidate);
                contentMutated = true;
                anyMutation = true;
                return { ...part, filename: candidate };
            }
            return part;
        });
        if (contentMutated) {
            return { ...msg, content: newContent };
        }
        return msg;
    });
    return anyMutation ? result : messages;
}
//# sourceMappingURL=uniquify-filenames.js.map