'use strict';

const app = require('ampersand-app');
const bluebird = require('bluebird');
const { dialog } = require('electron');
const rimraf = require('rimraf');

const rimrafAsync = bluebird.promisify(rimraf);

function warnAndDelete(options, dir) {
  /**
   * dialog#showMessageBox doesn't conform to Node.js-style callbacks. Manually
   * wrap it with a Promise.
   *
   * {@link http://electron.atom.io/docs/api/dialog/#dialogshowmessageboxbrowserwindow-options-callback}
   */
  return new Promise(resolve => {
    dialog.showMessageBox(Object.assign({
      type: 'warning',
      buttons: ['Delete', 'Cancel'],
      defaultId: 1,
      cancelId: 1,
    }, options), resolve);
  })
    .then(response => {
      if (response === 0) {
        return rimrafAsync(dir).then(() => app.core.teardown());
      }
    });
}

module.exports = {
  computations: function cleanComputations() {
    return warnAndDelete(
      {
        detail: 'Computations can be re-downloaded',
        message: 'Delete saved computations',
      },
      app.core.getComputationsDirectory()
    );
  },

  userData: function cleanUserData(username) {
    return warnAndDelete(
      {
        detail: 'You can not undo this action',
        message: 'Delete user data',
      },
      app.core.getDatabaseDirectory(username)
    );
  },
};


@@ -212,53 +212,187 @@
 );%0A%0A
-function warnAndDelete(options, dir
+module.exports = %7B%0A  /**%0A   * Clean user data from disk.%0A   *%0A   * @param %7Bstring%7D username%0A   * @returns %7BPromise%7D%0A   */%0A  userData: function cleanUserData(username
 ) %7B%0A  
+ 
+ 
 /**%0A
+  
    *
@@ -470,16 +470,18 @@
 ally%0A   
+  
 * wrap i
@@ -497,26 +497,30 @@
 Promise.%0A   
+ 
+ 
 *%0A
+  
    * %7B@link 
@@ -615,21 +615,25 @@
 ack%7D%0A   
+  
 */%0A  
+ 
+ 
 return n
@@ -660,16 +660,18 @@
 %3E %7B%0A    
+  
 dialog.s
@@ -688,24 +688,12 @@
 Box(
-Object.assign(
 %7B%0A
+  
     
@@ -711,16 +711,103 @@
 rning',%0A
+        detail: 'You can not undo this action',%0A        message: 'Delete user data',%0A  
       bu
@@ -841,16 +841,18 @@
 ,%0A      
+ 
+ 
 defaultI
@@ -853,24 +853,26 @@
 faultId: 1,%0A
+  
       cancel
@@ -886,20 +886,12 @@
     
+  
 %7D,
- options),
  res
@@ -897,16 +897,18 @@
 solve);%0A
+  
   %7D)%0A   
@@ -908,16 +908,18 @@
  %7D)%0A    
+ 
+ 
 .then(re
@@ -926,24 +926,26 @@
 sponse =%3E %7B%0A
+  
       if (re
@@ -964,24 +964,26 @@
 ) %7B%0A        
+ 
+ 
 return rimra
@@ -993,417 +993,61 @@
 ync(
-dir).then(() =%3E app.core.teardown());%0A      %7D%0A    %7D);%0A%7D%0A%0Amodule.exports = %7B%0A  computations: function cleanComputations() %7B%0A    return warnAndDelete(%0A      %7B%0A        detail: 'Computations can be re-downloaded',%0A        message: 'Delete saved computations',%0A      %7D,%0A      app.core.getComputationsDirectory()%0A    );%0A  %7D,%0A%0A  userData: function cleanUserData(username) %7B%0A    return warnAndDelete(
+app.core.getDatabaseDirectory(username))
 %0A    
-  %7B%0A
         
 deta
@@ -1046,144 +1046,59 @@
     
-detail: 'You can not undo this action',%0A        message: 'Delete user data',%0A
+.then(() =%3E app.core.teardown());%0A  
       %7D
-,
 %0A      
-app.core.getDatabaseDirectory(username)%0A    
+%7D
 );%0A 

Remove computation clean method.