import { parseApiMessage, extractResources, createImgNumberer, } from '../im/lark/message-parser.js';
/**
 * Render a single quoted message into the JSON shape `botmux quoted` emits.
 *
 * Invariants this preserves:
 *   - Image and file counters in `[图片 N]` / `[文件 N]` placeholders align
 *     1:1 with the indices of the matching-type entries in `resources`
 *     (independent counters, mirrors `formatAttachmentsHint`).
 *   - For merge_forward messages, sub-message images/files are appended to
 *     `resources` and rendered inside the forwarded-XML tree using the same
 *     numberer, so placeholders inside the XML keep aligning with the
 *     overall list.
 */
export async function renderQuotedMessage(larkAppId, rawMessage, expandMergeForward) {
    const numberer = createImgNumberer();
    // Order: extractResources first so top-level keys get their numbers, then
    // parseApiMessage reuses them when rendering text content. Calling them in
    // the other order leaves resources unnumbered when extractTextContent runs
    // first (it only consults the cache, doesn't create entries for resources
    // that haven't been declared yet via extractResources).
    const resources = extractResources(rawMessage.msg_type ?? '', rawMessage.body?.content ?? '', numberer);
    const parsed = parseApiMessage(rawMessage, numberer);
    if (parsed.msgType === 'merge_forward') {
        const { extraResources } = await expandMergeForward(larkAppId, parsed.messageId, parsed, numberer);
        resources.push(...extraResources);
    }
    return { ...parsed, resources };
}
//# sourceMappingURL=quoted-render.js.map