import { r as logVerbose } from "./globals-BN_Alz5D.js";
import "./runtime-env-COGTWrDs.js";
import { u as getIMessageRuntime } from "./monitor-reply-cache-C_LqYGNd.js";
import { createHash } from "node:crypto";
//#region extensions/imessage/src/monitor/persisted-echo-cache.ts
const IMESSAGE_SENT_ECHOES_TTL_MS = 720 * 60 * 1e3;
const IMESSAGE_SENT_ECHOES_NAMESPACE = "imessage.sent-echoes";
function normalizeText(text) {
	return text?.replace(/\r\n?/g, "\n").trim() || void 0;
}
function normalizeMessageId(messageId) {
	const normalized = messageId?.trim();
	if (!normalized || normalized === "ok" || normalized === "unknown") return;
	return normalized;
}
let mirror = null;
let persistenceFailureLogged = false;
function reportFailure(scope, err) {
	if (persistenceFailureLogged) return;
	persistenceFailureLogged = true;
	logVerbose(`imessage echo-cache: ${scope} disabled after first failure: ${String(err)}`);
}
function resolveIMessageSentEchoEntryKey(entry) {
	return createHash("sha256").update(JSON.stringify([
		entry.scope,
		entry.text ?? "",
		entry.messageId ?? "",
		entry.timestamp
	])).digest("hex").slice(0, 32);
}
function openPersistedEchoStore() {
	return getIMessageRuntime().state.openSyncKeyedStore({
		namespace: IMESSAGE_SENT_ECHOES_NAMESPACE,
		maxEntries: 256
	});
}
function remainingTtlMs(timestamp) {
	const remaining = IMESSAGE_SENT_ECHOES_TTL_MS - Math.max(0, Date.now() - timestamp);
	return remaining > 0 ? remaining : void 0;
}
function loadMirrorFromStore() {
	try {
		const cutoff = Date.now() - IMESSAGE_SENT_ECHOES_TTL_MS;
		mirror = openPersistedEchoStore().entries().map(({ value }) => value).filter((entry) => entry.timestamp >= cutoff).toSorted((a, b) => a.timestamp - b.timestamp).slice(-256);
	} catch (err) {
		reportFailure("read", err);
		mirror = [];
	}
}
function readRecentEntries() {
	loadMirrorFromStore();
	return mirror ?? [];
}
function persistEntry(entry) {
	const ttlMs = remainingTtlMs(entry.timestamp);
	if (!ttlMs) return;
	try {
		openPersistedEchoStore().register(resolveIMessageSentEchoEntryKey(entry), entry, { ttlMs });
	} catch (err) {
		reportFailure("write", err);
	}
}
function rememberPersistedIMessageEcho(params) {
	const text = normalizeText(params.text);
	const messageId = normalizeMessageId(params.messageId);
	const entry = {
		scope: params.scope,
		timestamp: Date.now(),
		...text ? { text } : {},
		...messageId ? { messageId } : {}
	};
	if (!entry.text && !entry.messageId) return;
	loadMirrorFromStore();
	persistEntry(entry);
	const cutoff = Date.now() - IMESSAGE_SENT_ECHOES_TTL_MS;
	mirror = [...mirror ?? [], entry].filter((candidate) => candidate.timestamp >= cutoff).slice(-256);
}
function hasPersistedIMessageEcho(params) {
	const text = normalizeText(params.text);
	const messageId = normalizeMessageId(params.messageId);
	if (!text && !messageId) return false;
	for (const entry of readRecentEntries()) {
		if (entry.scope !== params.scope) continue;
		if (messageId && entry.messageId === messageId) return true;
		if (text && entry.text === text) return true;
	}
	return false;
}
//#endregion
export { resolveIMessageSentEchoEntryKey as a, rememberPersistedIMessageEcho as i, IMESSAGE_SENT_ECHOES_TTL_MS as n, hasPersistedIMessageEcho as r, IMESSAGE_SENT_ECHOES_NAMESPACE as t };
