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

contextBridge.exposeInMainWorld('oemTool', {
  diagnostic: (message, payload) => ipcRenderer.invoke('oem-tool:diagnostic', { message, payload }),
  pickImage: () => ipcRenderer.invoke('oem-tool:pick-image'),
  pathToFileUrl: (filePath) => {
    if (!filePath) return '';
    const normalized = String(filePath).replace(/\\/g, '/').replace(/#/g, '%23');
    return `file:///${encodeURI(normalized)}`;
  },
  runBatch: (jobs) => ipcRenderer.invoke('oem-tool:run-batch', jobs),
  cancel: () => ipcRenderer.invoke('oem-tool:cancel'),
  openOutputDir: () => ipcRenderer.invoke('oem-tool:open-output-dir'),
  getStatus: () => ipcRenderer.invoke('oem-tool:get-status'),
  onLog: (callback) => {
    const listener = (_event, payload) => callback(payload);
    ipcRenderer.on('oem-tool:log', listener);
    return () => ipcRenderer.removeListener('oem-tool:log', listener);
  },
});
