import { Capacitor, registerPlugin } from "@capacitor/core";

interface AndroidDisplay {
  id: number;
  name: string;
  width: number;
  height: number;
  refreshRate: number;
  isCurrent: boolean;
}

interface ThorDualScreenPlugin {
  getDisplays: () => Promise<{ currentDisplayId: number; displays: AndroidDisplay[] }>;
  forceBothDisplays: () => Promise<AndroidDisplay & { opened: boolean; topOnActivity: boolean }>;
  addListener: (eventName: "displayDisconnected", listener: () => void) => Promise<{ remove: () => Promise<void> }>;
}

const plugin = registerPlugin<ThorDualScreenPlugin>("ThorDualScreen");

export function shouldOwnNativeDisplays() {
  const params = new URLSearchParams(window.location.search);
  return Capacitor.isNativePlatform() && params.get("role") !== "presentation";
}

export function forceBothThorDisplays() {
  return plugin.forceBothDisplays();
}

export function listenForDisplayDisconnect(listener: () => void) {
  return plugin.addListener("displayDisconnected", listener);
}
