export const COMPLETED_REACTION_EMOJI_TYPE = 'GoGoGo';
export function isPendingResponseCardOpen(session) {
    return !!session.pendingResponseCardId && session.pendingResponseCardState === 'open';
}
/** Sync only the cross-process pending-response fields from a fresher Session. */
export function syncPendingResponseState(target, source) {
    if (!source)
        return;
    target.pendingResponseCardId = source.pendingResponseCardId;
    target.pendingResponseCardState = source.pendingResponseCardState;
    target.lastPatchedResponseCardId = source.lastPatchedResponseCardId;
}
export function mergePendingResponseState(incoming, existing) {
    if (!existing)
        return incoming;
    const hasExistingPendingState = existing.pendingResponseCardId !== undefined
        || existing.pendingResponseCardState !== undefined
        || existing.lastPatchedResponseCardId !== undefined;
    if (!hasExistingPendingState)
        return incoming;
    const incomingStartsNewOpenCard = incoming.pendingResponseCardState === 'open'
        && !!incoming.pendingResponseCardId
        && incoming.pendingResponseCardId !== existing.lastPatchedResponseCardId;
    if (incomingStartsNewOpenCard)
        return incoming;
    const incomingMarksPatched = incoming.pendingResponseCardState === 'patched';
    const existingHasNewerOpenCard = existing.pendingResponseCardState === 'open'
        && !!existing.pendingResponseCardId
        && incoming.lastPatchedResponseCardId !== existing.pendingResponseCardId;
    if (incomingMarksPatched && !existingHasNewerOpenCard)
        return incoming;
    return {
        ...incoming,
        pendingResponseCardId: existing.pendingResponseCardId,
        pendingResponseCardState: existing.pendingResponseCardState,
        lastPatchedResponseCardId: existing.lastPatchedResponseCardId,
    };
}
export function shouldPatchPendingOnExplicitSend(session, opts) {
    return opts.msgType === 'interactive'
        && !opts.sendTopLevel
        && !opts.overrideChatId
        && !opts.sendInto
        && !opts.hasNotificationMentions
        && isPendingResponseCardOpen(session);
}
export function shouldReplyPendingInThread(scope) {
    return scope === 'thread';
}
export function shouldMarkPendingAsMentionedSend(opts) {
    return opts.msgType === 'interactive'
        && !opts.sendTopLevel
        && !opts.overrideChatId
        && !opts.sendInto
        && opts.hasNotificationMentions;
}
export function shouldWithdrawPreviousPendingOnNewTurn(_session) {
    return false;
}
/** A patched marker means the Feishu PATCH returned, but session save may not have. */
export function shouldTreatPendingCardAsPatchedByMarker(pendingCardId, marker) {
    return !!pendingCardId && marker?.state === 'patched' && marker.cardId === pendingCardId;
}
export function createPendingResponseQueue() {
    const tails = new Map();
    return {
        run(key, work) {
            const prev = tails.get(key);
            const next = prev ? prev.catch(() => undefined).then(work) : work();
            const stored = next.finally(() => {
                if (tails.get(key) === stored)
                    tails.delete(key);
            });
            tails.set(key, stored);
            return next;
        },
        size() { return tails.size; },
    };
}
export function startPendingResponseTurn(session, messageId) {
    session.pendingResponseCardId = messageId;
    session.pendingResponseCardState = 'open';
}
/** Mark the current open placeholder as patched; keep its id for diagnostics. */
export function markPendingResponseCardPatched(session) {
    session.lastPatchedResponseCardId = session.pendingResponseCardId;
    session.pendingResponseCardId = undefined;
    session.pendingResponseCardState = 'patched';
}
export function markPendingResponseCardPatchedIfCurrent(session, cardId) {
    if (session.pendingResponseCardId !== cardId || session.pendingResponseCardState !== 'open')
        return false;
    markPendingResponseCardPatched(session);
    return true;
}
/** Read the open placeholder id without claiming it until PATCH succeeds. */
export function claimPendingResponseCard(session) {
    return isPendingResponseCardOpen(session) ? session.pendingResponseCardId : undefined;
}
//# sourceMappingURL=pending-response.js.map