const { contextBridge, ipcRenderer, webUtils } = require("electron");

contextBridge.exposeInMainWorld("desktopStore", {
  load: () => ipcRenderer.invoke("store:load"),
  save: (payload) => ipcRenderer.invoke("store:save", payload),
  reset: () => ipcRenderer.invoke("store:reset"),
  openReplayer: () => ipcRenderer.invoke("replayer:open"),
  openHudOverlay: () => ipcRenderer.invoke("hud:open"),
  syncHudOverlays: (enabled) => ipcRenderer.invoke("hud:sync", enabled),
  setHudPayload: (payload) => ipcRenderer.invoke("hud:set-payload", payload),
  getHudPayload: () => ipcRenderer.invoke("hud:get-payload"),
  setHudSlotScale: (input) => ipcRenderer.invoke("hud:set-slot-scale", input),
  setHudSlotPosition: (input) => ipcRenderer.invoke("hud:set-slot-position", input),
  setHudOpacity: () => ipcRenderer.invoke("hud:set-opacity"),
  setHudMouseEvents: (interactive) => ipcRenderer.invoke("hud:set-mouse-events", interactive),
  setHudShape: (rects) => ipcRenderer.invoke("hud:set-shape", rects),
  setHudDragging: (active) => ipcRenderer.invoke("hud:set-dragging", active),
  onHudPayload: (callback) => {
    const listener = (_event, payload) => callback(payload);
    ipcRenderer.on("hud:payload", listener);
    return () => ipcRenderer.removeListener("hud:payload", listener);
  },
  onHudScale: (callback) => {
    const listener = (_event, scale) => callback(scale);
    ipcRenderer.on("hud:scale", listener);
    return () => ipcRenderer.removeListener("hud:scale", listener);
  },
  detectPokerTables: () => ipcRenderer.invoke("hud:detect-tables"),
  chooseHandHistoryFolder: () => ipcRenderer.invoke("hands:choose-folder"),
  detectHandHistoryFolders: () => ipcRenderer.invoke("hands:detect-folders"),
  scanHandHistoryFolder: (folderPath, seen) => ipcRenderer.invoke("hands:scan-folder", folderPath, seen),
  getFilePath: (file) => webUtils.getPathForFile(file),
  readRarEntries: (archivePath) => ipcRenderer.invoke("archive:read-rar", archivePath),
});
