'use strict';

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

// Getting arguments if they are provided for the CI/CD pipeline
const { skipInstall, build } = tools.getArgs();



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

    // Generating virtual remote - experimental
    .then(() => {
      return initUtils.remotes.generateVirtualRemote(finalResponse.remote);
    })

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

    // 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) {
        if (finalResponse.remoteOnly) {
          return installUtils.localDev.installRemote(finalResponse.remote);
        } else {
          return installUtils.localDev.install();
        }
      }
    })

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