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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.hexagonalArchOption = exports.cleanArchOption = void 0;

var _inquirer = require("inquirer");

var _yargs = _interopRequireDefault(require("yargs"));

var _createTemplate = require("./create-template");

var _recursiveCopyAdapter = require("./recursive-copy-adapter");

var _child_process = require("child_process");

var _cliSpinner = require("cli-spinner");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const cleanArchOption = 'Clean Architecture';
exports.cleanArchOption = cleanArchOption;
const hexagonalArchOption = 'Hexagonal Architecture';
exports.hexagonalArchOption = hexagonalArchOption;
const argv = (0, _yargs.default)(process.argv.slice(2)).usage('This is gecode').options({
  'init': {
    description: 'Start setup new project',
    alias: 'i',
    type: 'boolean'
  }
}).argv;

_yargs.default.showHelp();

console.log('\n\n\n');
const questions = [{
  type: 'list',
  name: 'architecture',
  message: 'What project template would you like to use?',
  choices: [cleanArchOption, hexagonalArchOption]
}, {
  type: 'input',
  name: 'projectName',
  message: "New project name?"
}, {
  type: 'confirm',
  name: 'startWithGit',
  message: 'Do you want to start with Git?',
  default: false
}, {
  type: 'confirm',
  name: 'installDependencies',
  message: 'Install dependencies?',
  default: false
}];

if (argv['init']) {
  (0, _inquirer.prompt)(questions).then(async answers => {
    const createTemplateResult = await (0, _createTemplate.createTemplate)({
      name: answers.projectName,
      template: getTemplate(answers.architecture)
    }, new _recursiveCopyAdapter.RecursiveCopyAdapter());

    if (createTemplateResult === _createTemplate.createTemplateError.PROJECT_ALREADY_EXIST) {
      console.log("\n");
      console.log('Project name already exist :(');
      return;
    }

    if (createTemplateResult === _createTemplate.createTemplateError.UNKNOWN_ERROR) {
      console.log("\n");
      console.log('Could not create project :(');
      return;
    }

    if (answers.startWithGit) {
      (0, _child_process.execSync)("git init " + createTemplateResult);
    }

    if (answers.installDependencies) {
      var spinner = new _cliSpinner.Spinner('Creating project... %s');
      spinner.setSpinnerString('|/-\\');
      spinner.start();
      (0, _child_process.execSync)("npm install " + "./" + createTemplateResult);
      spinner.stop();
      spinner.clearLine(spinner.stream);
      console.log("\n");
      console.log('Project created on successfully!!! 🚀');
      return;
    }

    console.log("\n");
    console.log('Project created on successfully!!! 🚀');
  });
}

function getTemplate(architecture) {
  let template;

  if (architecture === cleanArchOption) {
    template = _createTemplate.typeTemplate.CLEAN_ARCH;
  }

  if (architecture === hexagonalArchOption) {
    template = _createTemplate.typeTemplate.HEXA_ARCH;
  }

  return template;
}