// Centralized handler for fire-and-forget IPC (and similar async) calls whose
// failure we want recorded but can't surface to the user — the optimistic store
// update has already happened, so there's nothing to roll back or prompt about.
// Replaces the swallowed `.catch(() => {})` sites the audit flagged (M29) with a
// single, consistent log format. `op` names the operation, e.g. 'addHistory'.
//
// The failure is both printed to the renderer console (dev/`--inspect`) and
// forwarded to the main process's file-backed diagnostic log (CC8), so it survives
// into a packaged build where the renderer console is unreachable.
export function logError(op: string): (e: unknown) => void {
  return (e) => {
    console.error(`[AeroFetch] ${op} failed:`, e)
    try {
      const detail = e instanceof Error ? e.message : String(e)
      // window.api is absent in the browser UI preview and in unit tests.
      if (typeof window !== 'undefined') window.api?.logError?.(op, detail)
    } catch {
      /* logging must never throw out of a catch handler */
    }
  }
}
