import * as os from 'os';
import { execSync } from 'child_process';
import * as fse from 'fs-extra';

export class DefaultFolder {
  static downloads(): string {
    switch (os.platform()) {
      case 'win32':
        return `${process.env.USERPROFILE}/Downloads`;
      case 'darwin':
        return `${process.env.HOME}/Downloads`;
      case 'linux': {
        let dir: Buffer;
        try {
          dir = execSync('xdg-user-dir DOWNLOAD', { stdio: [0, 3, 3] });
        } catch (_) {}

        if (dir) {
          return dir.toString('utf-8');
        }

        let stat: fse.Stats;
        const homeDownloads = `${process.env.HOME}/Downloads`;
        try {
          stat = fse.statSync(homeDownloads);
        } catch (_) {}
        if (stat) {
          return homeDownloads;
        }
        return '/tmp/';
      }

      default:
        break;
    }
  }
}
