'use strict';

// local
const tools = require('../../utils/tools');
const metadataUtils = require('../metadata/metadata-utils');

// inner modules
const innerCommon = require('./common');


module.exports.getNewVersion = (project, envTarget) => {
  let versionsMetadata;
  let envsMetadata;

  return Promise.resolve()
    // fetch versions history and store
    .then(() => {
      return metadataUtils.appVersions.getMetadata(project);
    })
    .then((metadata) => {
      versionsMetadata = metadata;
    })

    // fetch envs metadata and store
    .then(() => {
      return metadataUtils.appEnvs.getMetadata(project);
    })
    .then((metadata) => {
      envsMetadata = metadata;
    })


    // generating new version based on the env target and the envs cycle type
    .then(() => {
      switch(project.options.BUILD_VERSION_STRATEGY_ENVS_CYCLE) {
        case 'DEV_TST_ACC_PROD':
          return innerCommon.getNewVersionDTAP(envTarget, versionsMetadata, envsMetadata);

        default:
          return innerCommon.getNewVersionDefault(envTarget, versionsMetadata, envsMetadata);
      }
    })

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


