import { useCallback } from 'react';
import { useSetAtom } from 'jotai';
import { mediaViewerAtom, MediaViewerEntry } from '../mediaViewer';
import { userRoomProfileAtom } from '../userRoomProfile';
import { roomMembersSheetAtom } from '../roomMembersSheet';

export const useOpenMediaViewer = (): ((entry: MediaViewerEntry) => void) => {
  const setMedia = useSetAtom(mediaViewerAtom);
  // Close other right-pane sheets when opening media — profile / members
  // / media all fight for the same desktop slot and would stack into two
  // horseshoes on mobile. Mutual exclusion is the simplest contract
  // until the user asks for stacking.
  const setProfile = useSetAtom(userRoomProfileAtom);
  const setRoomMembersSheet = useSetAtom(roomMembersSheetAtom);
  return useCallback(
    (entry) => {
      setProfile(undefined);
      setRoomMembersSheet(undefined);
      setMedia(entry);
    },
    [setMedia, setProfile, setRoomMembersSheet]
  );
};

export const useCloseMediaViewer = (): (() => void) => {
  const setMedia = useSetAtom(mediaViewerAtom);
  return useCallback(() => {
    setMedia(undefined);
  }, [setMedia]);
};
