function normText(text) {
    return text.trim().replace(/\s+/g, ' ');
}
/** Paths from Connect `send_message` + `queue` until DOM exposes file paths (queue thumbs are often blob:). */
export class QueuedMediaHints {
    hints = [];
    ttlMs = 10 * 60 * 1000;
    note(text, images) {
        const key = normText(text);
        const paths = images.filter((p) => p.trim().length > 0);
        if (!key || !paths.length)
            return;
        this.prune();
        this.hints = this.hints.filter((h) => h.text !== key);
        this.hints.push({ text: key, images: paths, at: Date.now() });
    }
    forget(text) {
        const key = normText(text);
        if (!key)
            return;
        this.hints = this.hints.filter((h) => h.text !== key);
    }
    /** Drop stale hints when queue rows disappear (e.g. removed in Cursor). */
    syncFromDom(queued) {
        this.prune();
        if (!queued.length) {
            this.hints = [];
            return queued;
        }
        const keys = new Set(queued.map((q) => normText(q.text)).filter(Boolean));
        this.hints = this.hints.filter((h) => {
            for (const k of keys) {
                if (h.text === k || k.includes(h.text) || h.text.includes(k))
                    return true;
            }
            return false;
        });
        return this.enrich(queued);
    }
    prune() {
        const cut = Date.now() - this.ttlMs;
        this.hints = this.hints.filter((h) => h.at >= cut);
    }
    enrich(queued) {
        if (!queued.length)
            return queued;
        this.prune();
        if (!this.hints.length)
            return queued;
        return queued.map((q) => {
            if (q.images?.length)
                return { ...q, hasImage: true };
            const key = normText(q.text);
            if (!key)
                return q;
            const hint = this.hints.find((h) => h.text === key || key.includes(h.text) || h.text.includes(key));
            if (!hint)
                return q;
            return { ...q, images: hint.images, hasImage: true };
        });
    }
}
