#! /usr/bin/env node
'use strict';

import {processIM, processChannel, processGroup} from './commons.js';
import PleasantProgress from 'pleasant-progress';

export function slackHistoryExport(args) {
  const progress = new PleasantProgress();
  progress.start('working');
  if(args.type === 'dm' || args.username) {
    processIM(args).then(result => {
      progress.stop();
      if (result.length) {
        console.log(`Done exporting history: files saved at ${result[0].path}`);
      } else {
        console.log(`Done exporting history: file saved at ${result.path}`);
      }
    }).catch(error => {
      console.error(error);
      progress.stop();
    });
  } else if (args.type === 'channel' || args.channel) {
    processChannel(args).then(result => {
      progress.stop();
      if (result.length) {
        console.log(`Done exporting history: files saved at ${result[0].dir}`);
      } else {
        console.log(`Done exporting history: file saved at ${result.path}`);
      }
    }).catch((error) => {
      progress.stop();
      console.error(error);
    });
  } else if (args.type === 'group' || args.group) {
    processGroup(args).then(result => {
      progress.stop();
      if (result.length) {
        console.log(`Done exporting history: files saved at ${result[0].path}`);
      } else {
        console.log(`Done exporting history: file saved at ${result.path}`);
      }
    }).catch((error) => {
      progress.stop();
      console.error(error);
    });
  }
}

@@ -240,37 +240,8 @@
 ();%0A
-  progress.start('working');%0A
   if
@@ -276,24 +276,55 @@
 username) %7B%0A
+    progress.start('working');%0A
     processI
@@ -714,24 +714,55 @@
 .channel) %7B%0A
+    progress.start('working');%0A
     processC
@@ -1154,24 +1154,55 @@
 gs.group) %7B%0A
+    progress.start('working');%0A
     processG
@@ -1537,22 +1537,22 @@
 error);%0A
-
     %7D);%0A
   %7D%0A%7D%0A
@@ -1545,14 +1545,164 @@
     %7D);%0A
+  %7D else %7B%0A    console.log('Error: Unknown argument format. Use %22slack-history-export --help%22 to know valid options/arguments');%0A    process.exit(1);%0A
   %7D%0A%7D%0A

Fix error when incomplete arguments are supplied