const chalk = require('chalk')
const shouldBackend = require('./util/shouldBackend')
const loadUserOptions = require('./loadUserOptions')
const { error, stopSpinner, log, logWithSpinner, execa } = require('@vue/cli-shared-utils')

async function component (name, options = {}, context = process.cwd()) {
  const userOptions = await loadUserOptions(context)

  const isShouldBackend = await shouldBackend(userOptions.type)

  log()
  if (isShouldBackend) {

  }

  log()
  logWithSpinner('🔥', `Generating component... \n \n`)
  log()
  await execa(
    `php artisan component:new ${name}`,
    { cwd: context, stdio: 'inherit', shell: true }
  )
    .then(result => {
      log()
      // log(result)
      stopSpinner()
    })

  stopSpinner()
  log(`🎉  Successfully created ${chalk.yellow(name)} component on ${chalk.yellow(userOptions.siteName)}.`)
  log()
}

module.exports = (...args) => {
  return component(...args).catch(err => {
    stopSpinner(false) // do not persist
    error(err)
    if (!process.env.CONTROLLA_CLI_TEST) {
      process.exit(1)
    }
  })
}
