'use strict';

const path = require('path');

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

/**
 * Get the env target, transform input envTarget to known files suffix or handle local / playground file
 *
 * @param {string} envTargetIn The build-time environment target provided to target specific routes for specific environment deployments
 * @param {boolean} build if the script is launched from a release pipeline OR from local CSDR playground environment (false in that case) * @return routeContent the text block transformed from the route object as input
 * @returns {string} the envTarget transformed
 */
module.exports.getEnvTarget = (envTargetIn, build) => {
  if (!envTargetIn) {
    // check if envTarget has been provided as cli args for stand-alone local build of CSDR apps
    const { envTarget } = tools.getArgs();

    if (!envTarget) {
      throw 'ENV_TARGET_NOT_FOUND';
    } else {
      envTargetIn = envTarget;
    }
  }

  let envTargetFinal = envTargetIn.toLowerCase();

  if (build) {
    // dirty fix as openid files do not respect envTarget names
    if (envTargetFinal === 'tst') {
      envTargetFinal = 'test';
    }

    // serve configuration based
  } else {
    if (envTargetIn.indexOf('local') > -1) {
      envTargetFinal = envTargetIn.replace('-openid', '');
    } else {
      envTargetFinal = 'local-' + envTargetFinal;
    }
  }

  return envTargetFinal;
};




/**
 * Replace route object and transform into an Angular / eUI route definition
 *
 * @param {route} route route object containing the result of the route translated into route props
 * @returns {string} routeContent the text block transformed from the route object as input
 */


/**
 * Get routes file of project
 *
 * PRIVATE
 *
 * @param {project} project The CSDR project object fetched from csdr global projects config
 * @param {string} euiVersion the eUI version of the current project used mainly in local CSDR playground environment (format : v.x)
 * @returns {string} routeContent the text block transformed from the route object as input
 */
module.exports.getRoutesFile = (project, euiVersion) => {
  tools.logInfo('Getting routes file for replacement');

  if (!project.externalRoutesSources) {
    tools.logError('project requires externalRoutesSources definition');
    return;
  }

  let routesFilePath;
  let srcRoutesFilePath;

  if (
    !project.externalRoutesSources.routesConfigNpmPkg ||
    !project.externalRoutesSources.routesFileSource ||
    !project.externalRoutesSources.routesFilePath
  ) {
    tools.logError('project requires externalRoutesSources.routesConfigNpmPkg, routesFileSources and routesFilePath definitions');
    return;
  }

  // check if package is locally cloned
  const localPackage = configUtils.packages.getPackages().filter((p) => {
    return p.npmPkg === project.externalRoutesSources.routesConfigNpmPkg;
  })[0];

  // if local package is found
  if (localPackage) {
    tools.logInfo('local package found, copying from local');

    tools.logInfo(`constructing route path with eUI version provided: ${euiVersion}`);

    srcRoutesFilePath = path.join(
      process.cwd(),
      'packages',
      localPackage.name,
      'assets',
      euiVersion,
      project.externalRoutesSources.routesFileSource
    );

    // if not sources are taken from the npm package def in node_modules
  } else {
    tools.logInfo('remote package found, copying from remote');
    const npmPkgScope = project.externalRoutesSources.routesConfigNpmPkg.substr(
      0,
      project.externalRoutesSources.routesConfigNpmPkg.indexOf('/')
    );
    const npmPkgName = project.externalRoutesSources.routesConfigNpmPkg.substr(
      project.externalRoutesSources.routesConfigNpmPkg.indexOf('/') + 1
    );

    tools.logInfo(`constructing route path with eUI version provided: ${euiVersion}`);

    srcRoutesFilePath = path.join(
      process.cwd(),
      'node_modules',
      npmPkgScope,
      npmPkgName,
      'assets',
      euiVersion,
      project.externalRoutesSources.routesFileSource
    );
  }

  routesFilePath = path.join(project.paths.rootPath, project.externalRoutesSources.routesFilePath);

  // copy source file to project destination
  tools.copy(srcRoutesFilePath, routesFilePath);

  // checking is routes path declared
  if (!tools.isFileExists(routesFilePath)) {
    tools.logError(`Unable to find ${routesFilePath}`);
    return;
  }

  return routesFilePath;
};
