text: const packagesName = require('../constans/packages-names');
  const { INFO, ERROR, SUCCESS } = require('../constans/log-types');
  const inquirer = require('inquirer');
  const createLog = require('../helpers/createLog');
  const prompt = inquirer.createPromptModule();
  const getLastMainVersion = require('../helpers/getLastMainVersion');
  const chalk = require('chalk');
  const fs = require('fs');
  const path = require('path');
  
  const startWizard = async () => {
    createLog('\n @rummy-react-ui update CLI ⚛️ \n', INFO);
  
    const packagesMapped = packagesName.map((pck) => {
      return { name: Object.values(pck)[0] };
    });
  
    const questions = [
      {
        type: 'confirm',
        name: 'createPacakges',
        message: 'Do you want to create new version of @rummy-react-ui?',
        validate: (answers) => {
          if (!answers.createPacakges) {
            return false;
          }
          createLog(answers, INFO);
          return true;
        },
      },
      {
        type: 'confirm',
        name: 'deleteBuildFolder',
        message: 'Do you want to delete build folder?',
  
        when: () => {
          if (fs.existsSync(path.resolve(__dirname, '../../../../build'))) {
            return true;
          } else {
            return false;
          }
        },
        validate: (answers) => {
          if (answers.deleteBuildFolder) {
            return true;
          } else {
            return false;
          }
        },
      },
      {
        type: 'checkbox',
        name: 'bundler',
        message: 'Which bundler do you want to use?',
        choices: [
          {
            name: 'rollup',
          },
          {
            name: 'webpack',
          },
        ],
        when: (answers) => {
          if (!answers.createPacakges) {
            process.exit();
          } else {
            return true;
          }
        },
        validate: (answers) => {
          if (answers.length < 1) {
            return 'You have to specify which bundler you want to use. We recommend using rollup.';
          } else if (answers.length >= 2) {
            return 'You can only specify one type of bundler to use.';
          }
  
          return true;
        },
      },
  
      {
        type: 'confirm',
        name: 'isAllPackages',
        message: 'Do you want update all packages?',
        default: true,
  
        validate: (answers) => {
          if (answers.isAllPackages) {
            return true;
          } else {
            return false;
          }
        },
      },
      {
        type: 'checkbox',
        message: 'What packages do you want update?',
        name: 'packagesToUpdate',
        choices: packagesMapped,
        validate: (answers) => {
          if (answers.length < 1) 'You must choose at least one pacakges to update';
  
          return true;
        },
  
        when: (answers) => {
          if (answers.isAllPackages) return;
  
          if (!answers.createPacakges) {
            process.exit();
          } else {
            return true;
          }
        },
      },
      {
        type: 'text',
        message: 'Which version do you want to create?',
        name: 'version',
        validate: (value) => {
          const valid = !!value;
          return valid || 'You have to enter the version number';
        },
        when: (answers) => {
          if (!answers.createPacakges) {
            process.exit();
          } else {
            return true;
          }
        },
      },
    ];
  
    return prompt(questions).then((answers) => {
      if (!answers.createPacakges) {
        createLog('cli was stopped', INFO);
        process.exit();
      }
  
      if (answers.version === getLastMainVersion()) {
        createLog(
          `You picked the latest version ${chalk.bold(
            getLastMainVersion(),
          )}. Please choice another one.`,
          ERROR,
        );
        process.exit();
      } else {
        createLog(`You picked version ${chalk.bold(answers.version)}`, SUCCESS);
      }
  
      if (answers.isAllPackages) {
        answers.packagesToUpdate = packagesName.map((pck) => {
          return Object.values(pck)[0];
        });
      }
  
      return answers;
    });
  };
  
  module.exports = startWizard;
  