'use strict';

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

contextBridge.exposeInMainWorld('shattercut', Object.freeze({
  openDialog: () => ipcRenderer.invoke('dialog:open'),
  confirmDiscard: () => ipcRenderer.invoke('dialog:confirm-discard'),
  acceptPath: (candidate) => ipcRenderer.invoke('file:accept', candidate),
  pathForFile: (file) => webUtils.getPathForFile(file),
  getState: () => ipcRenderer.invoke('state:get'),
  updateState: (update) => ipcRenderer.invoke('state:update', update),
  toggleFullscreen: () => ipcRenderer.invoke('window:fullscreen'),
  fitVideo: (request) => ipcRenderer.invoke('window:fit-video', request),
  updateMenu: (state) => ipcRenderer.send('menu:state', state),
  setProgress: (value) => ipcRenderer.send('window:progress', value),
  showTimelineMenu: (payload) => ipcRenderer.invoke('timeline:menu', payload),
  setTitle: (title) => ipcRenderer.send('window:title', title),
  markReady: (rendererMs) => ipcRenderer.send('perf:ready', rendererMs),
  markMediaReady: (metrics) => ipcRenderer.send('perf:media-ready', metrics),
  markFirstFrame: (metrics) => ipcRenderer.send('perf:first-frame', metrics),
  copySource: (source) => ipcRenderer.invoke('file:copy', source),
  beginExport: (suggestedName, options) => ipcRenderer.invoke('export:begin', suggestedName, options),
  beginClipExport: (sessionId, fileName, options) => ipcRenderer.invoke('export:begin-clip', sessionId, fileName, options),
  endExportSession: (sessionId, canceled) => ipcRenderer.invoke('export:end-session', sessionId, canceled),
  writeExportChunk: (id, data) => ipcRenderer.invoke('export:chunk', id, data),
  endExport: (id) => ipcRenderer.invoke('export:end', id),
  cancelExport: (id) => ipcRenderer.invoke('export:cancel', id),
  losslessExport: (options) => ipcRenderer.invoke('lossless:export', options),
  cancelLosslessExport: () => ipcRenderer.send('lossless:cancel'),
  sourceReleased: () => ipcRenderer.send('file:released'),
  onReleaseSource: (callback) => ipcRenderer.on('file:release-source', () => callback()),
  onLosslessProgress: (callback) => ipcRenderer.on('lossless:progress', (_event, ratio) => callback(ratio)),
  onCommand: (callback) => ipcRenderer.on('app:command', (_event, command, payload) => callback(command, payload)),
  onOpen: (callback) => ipcRenderer.on('file:open', (_event, filePath) => callback(filePath)),
  onStats: (callback) => ipcRenderer.on('app:stats', (_event, stats) => callback(stats)),
}));
