import { Room } from 'matrix-js-sdk';
import { createContext, useContext } from 'react';

const RoomContext = createContext<Room | null>(null);

export const RoomProvider = RoomContext.Provider;

export function useRoom(): Room {
  const room = useContext(RoomContext);
  if (!room) throw new Error('Room not provided!');
  return room;
}

// «Strictly two-member room» — Element-Web tier-2 pattern via
// `room.getInvitedAndJoinedMemberCount() === 2`. Server-side authoritative,
// no race against `m.direct` account-data hydration. Drives peer-avatar
// fallback in the room header, DM call button visibility, and per-author
// username colour in reply chips. See docs/plans/dm_1x1_redesign.md §6.8.
const IsOneOnOneContext = createContext<boolean>(false);

export const IsOneOnOneProvider = IsOneOnOneContext.Provider;

export const useIsOneOnOne = () => useContext(IsOneOnOneContext);
