'use strict';

const tools = require('../../utils/tools');
const configUtils = require('../config/config-utils');
const installUtils = require('../install/install-utils');

const innerInitProject = require('./init-project');
const innerRemotes = require('./remotes');
const innerPackages = require('./packages');

const { skipInstall, build } = tools.getArgs();


let customConfig;

module.exports.init = (finalResponse) => {
  tools.logTitle('Starting init custom');

  return Promise.resolve()
    .then(() => {
      customConfig = configUtils.global.getCustomConfig();

      if (!customConfig) {
        throw new Error('Custom config not found... run the command "npm run init:custom" to generate your customized config');
      }
    })

    .then(() => {
      return customConfig.remotes.reduce((promise, remote) => {
        return promise.then(() => (
          innerRemotes.generateVirtualRemote(remote)
        ));
      }, Promise.resolve());
    })

    .then(() => {
      finalResponse.project = customConfig.project;
      finalResponse.euiVersion = customConfig.euiVersion;

      return innerInitProject.init(finalResponse);
    })

    // .then(() => {
    //   return innerPackages.importPackages(finalResponse);
    // })

    // .then(() => {
    //   if (!build && !skipInstall) {
    //     return installUtils.localDev.install(customConfig.deps);
    //   }
    // })

    .catch((e) => {
      throw e;
    });
}
