import { ipcRenderer } from 'electron';

function dialogSelectFile(
  config: Electron.OpenDialogOptions
): Promise<Electron.OpenDialogReturnValue> {
  return ipcRenderer.invoke('dialog:selectFile', config);
}

function dialogSaveFile(
  config: Electron.SaveDialogOptions
): Promise<Electron.SaveDialogReturnValue> {
  return ipcRenderer.invoke('dialog:saveFile', config);
}

// win prcess
function startWinProcess(processCount: number, loadPageUrl: string) {
  return ipcRenderer.send('start-win-process', processCount, loadPageUrl);
}
function stopWinProcess() {
  return ipcRenderer.send('stop-win-process');
}
function closeProcessWindow(winId: number) {
  return ipcRenderer.send('close-process-window', winId);
}
function onStopRunning(callback: () => void) {
  // return ipcRenderer.on('stop-running', callback);
  return ipcRenderer.on('stop-running', () => callback());
}

// silence-authority
function onSilenceAuthority(callback: (authKey: string) => void) {
  return ipcRenderer.on(
    'silence-authority',
    (event: Electron.IpcRendererEvent, authKey: string) => callback(authKey)
  );
}
function checkSilenceAuthority(): Promise<string> {
  return ipcRenderer.invoke('check-silence-authority');
}

const electronApi = {
  dialogSelectFile,
  dialogSaveFile,
  // win prcess
  startWinProcess,
  stopWinProcess,
  closeProcessWindow,
  onStopRunning,
  // silence-authority
  onSilenceAuthority,
  checkSilenceAuthority,
};

export type ElectronApi = typeof electronApi;

export default electronApi;
