/*
Wraps APIs that are only available in the main process in IPC messages, so that the BrowserWindow can use them
*/

ipc.handle('test-invoke', function () {
  return 1
})

ipc.handle('reloadWindow', function () {
  mainWindow.webContents.reload()
})

ipc.handle('startFileDrag', function (e, path) {
  app.getFileIcon(path, {}).then(function (icon) {
    mainWindow.webContents.startDrag({
      file: path,
      icon: icon
    })
  })
})

ipc.handle('showFocusModeDialog1', function () {
  dialog.showMessageBox({
    type: 'info',
    buttons: [l('closeDialog')],
    message: l('isFocusMode'),
    detail: l('focusModeExplanation1') + ' ' + l('focusModeExplanation2')
  })
})

ipc.handle('showFocusModeDialog2', function () {
  dialog.showMessageBox({
    type: 'info',
    buttons: [l('closeDialog')],
    message: l('isFocusMode'),
    detail: l('focusModeExplanation2')
  })
})

ipc.handle('showOpenDialog', function (e, options) {
  return dialog.showOpenDialogSync(mainWindow, options)
})

ipc.handle('showSaveDialog', function (e, options) {
  return dialog.showSaveDialogSync(mainWindow, options)
})

ipc.handle('addWordToSpellCheckerDictionary', function (e, word) {
  session.fromPartition('persist:webcontent').addWordToSpellCheckerDictionary(word)
})

ipc.handle('downloadURL', function (e, url) {
  mainWindow.webContents.downloadURL(url)
})

ipc.handle('clearStorageData', function () {
  /* It's important not to delete data from file:// here, since that would also remove internal browser data (such as bookmarks) */
  return session.fromPartition('persist:webcontent').clearStorageData({ origin: 'http://' })
    .then(function () {
      session.fromPartition('persist:webcontent').clearStorageData({ origin: 'https://' })
    })
})

/* window actions */

ipc.handle('minimize', function (e) {
  mainWindow.minimize()
  // workaround for https://github.com/minbrowser/min/issues/1662
  mainWindow.webContents.send('minimize')
})

ipc.handle('maximize', function (e) {
  mainWindow.maximize()
  // workaround for https://github.com/minbrowser/min/issues/1662
  mainWindow.webContents.send('maximize')
})

ipc.handle('unmaximize', function (e) {
  mainWindow.unmaximize()
  // workaround for https://github.com/minbrowser/min/issues/1662
  mainWindow.webContents.send('unmaximize')
})

ipc.handle('close', function (e) {
  mainWindow.close()
})

ipc.handle('setFullScreen', function (e, fullScreen) {
  mainWindow.setFullScreen(e, fullScreen)
})

@@ -900,32 +900,38 @@
 showOpenDialog',
+ async
  function (e, op
@@ -933,38 +933,52 @@
 e, options) %7B%0A  
-return
+const result = await
  dialog.showOpen
@@ -979,28 +979,24 @@
 owOpenDialog
-Sync
 (mainWindow,
@@ -997,32 +997,58 @@
 indow, options)%0A
+  return result.filePaths%0A
 %7D)%0A%0Aipc.handle('
@@ -1063,16 +1063,22 @@
 Dialog',
+ async
  functio
@@ -1096,22 +1096,36 @@
 ns) %7B%0A  
-return
+const result = await
  dialog.
@@ -1142,12 +1142,8 @@
 alog
-Sync
 (mai
@@ -1160,16 +1160,41 @@
 ptions)%0A
+  return result.filePath%0A
 %7D)%0A%0Aipc.

switch to async dialog API to work around electron/electron#31152 (#1780)