import { atom } from 'jotai';
import { EncryptedAttachmentInfo } from 'browser-encrypt-attachment';
import { IImageInfo, IVideoInfo } from '../../types/matrix/common';

// Open state for the room media viewer — bottom-up sheet on mobile,
// floating draggable window on desktop/tablet (RoomViewMediaWindow).
// Mirror of `settingsSheetAtom`. The
// atom-driven shell is mounted in `Room.tsx`; `ImageContent` /
// `VideoContent` open the atom when the host context from
// `mediaViewerHostContext` is non-null (Room timeline path);
// otherwise they fall back to legacy local-state `<Overlay>` or
// inline native playback (pin-menu, message search — surfaces where
// the horseshoe shell isn't mounted).
export type MediaViewerKind = 'image' | 'video';

export type MediaViewerEntry = {
  roomId: string;
  eventId: string;
  kind: MediaViewerKind;
  // mxc:// URL (unresolved). The viewer body resolves + decrypts
  // internally using the same code paths the inline thumbnail uses
  // (`mxcUrlToHttp` + `downloadEncryptedMedia` + `decryptFile`), so
  // the atom payload stays cheap to construct from a thumbnail-tap
  // event handler without awaiting decryption.
  url: string;
  body: string;
  info?: IImageInfo | IVideoInfo;
  encInfo?: EncryptedAttachmentInfo;
  mimeType?: string;
};

export const mediaViewerAtom = atom<MediaViewerEntry | undefined>(undefined);
