'use strict';

const tools = require('../../utils/tools');
const initUtils = require('./init-utils');
const installUtils = require('../install/install-utils');
const configUtils = require('../config/config-utils');
const injectionUtils = require('../../utils/pre-build/injection/injection-utils');

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


module.exports.init = (finalResponse) => {
  let prj;

  // if build is passed through programatic init (standalone build)
  if (finalResponse.build) {
    build = finalResponse.build;
  }

  return Promise.resolve()
    // Initializing .euirc.json local file - the core of CSDR operations
    .then(() => {
      // Initialize config
      return configUtils.init.run(finalResponse);
    })

    .then(() => {
      // get project object from config
      prj = configUtils.projects.getProject(finalResponse.project);

      tools.logInfo('Project object constructed : ');
      console.log(prj);
    })

    // clone or install linked host package to project if any
    .then(() => {
      return initUtils.packages.cloneHostPackage(prj);
    })

    // Initializing .meta based on .euirc.json for local respoisitories cloning
    .then(() => {
      return initUtils.meta.init();
    })

    // Cloning repositories based on the .meta file generated
    .then(() => {
      if (!skipClone) {
        return initUtils.repos.init(finalResponse);
      }
    })

    // Importing external sources
    .then(() => {
      return injectionUtils.externals.injectExternalAppSources(prj, false, true, finalResponse.euiVersion);
    })

    // Importing packages and generating definitions
    .then(() => {
      return initUtils.packages.importPackages(finalResponse);
    })

    // adapt and inject particular root data/config for eUI version detected on project
    // if multiple versions of eUI are detected on the same project, an exception is thrown
    .then(() => {
      return initUtils.global.initRootFilesAndResolutions();
    })

    // Install deps based on current config generated => take last know snapshots
    .then(() => {
      // this is only executed for local dev installation,
      // in case of release the initialisation is managed by the application / package themselves
      if (!build && !skipInstall) {
        return installUtils.localDev.install();
      }
    })

    // Attaching app scripts to root package.json
    .then(() => {
      return initUtils.projects.importScripts();
    })


    // Attaching extra tsconfig from project
    .then(() => {
      return initUtils.projects.importExtraTsConfig();
    })


    // Attaching external features from project
    .then(() => {
      if (!build) {
        return initUtils.projects.importExternalFeatures();
      }
    })


    // Attaching external mock repo for project
    .then(() => {
      if (!build) {
        return initUtils.projects.importExternalMock();
      }
    })

    .then(() => {
      tools.logSuccess('OK => Project successfully initialized');
    })

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