import { useNavigate } from 'react-router';
import { MsgType } from 'matrix-js-sdk';
import { useMatrixClient } from './useMatrixClient';
import { useTauriEvent } from './useTauri';

/** Payload of the `lotus-notification-activate` event (a plain body click). */
interface ActivateDetail {
  path?: string;
}

/** Payload of the `lotus-notification-reply` event (the inline reply box). */
interface ReplyDetail {
  roomId?: string;
  text?: string;
}

/**
 * P5-41 / P5-35 — wire the native WinRT toast's click + quick-reply back into the
 * client. The Rust side (`show_rich_toast`) dispatches DOM CustomEvents via
 * `emit_to_web`:
 *  - `lotus-notification-activate` → route to the room the toast was for, reusing
 *    the same `useNavigate(path)` mechanism the web `notificationclick` path uses
 *    (see ClientNonUIFeatures).
 *  - `lotus-notification-reply` → send the typed reply straight to the room.
 * No-op outside Tauri (the events never fire).
 */
export function useTauriToastActions(): void {
  const navigate = useNavigate();
  const mx = useMatrixClient();

  useTauriEvent<ActivateDetail>('lotus-notification-activate', ({ path }) => {
    if (path) navigate(path);
  });

  useTauriEvent<ReplyDetail>('lotus-notification-reply', ({ roomId, text }) => {
    if (!roomId || !text) return;
    mx.sendMessage(roomId, { msgtype: MsgType.Text, body: text }).catch(() => undefined);
  });
}
