'use strict';

// GLOBAL
const path = require('path');

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

// ARGS
const { registry } = tools.getArgs();


const processResolutionsInjection = module.exports.processResolutionsInjection = (packageJsonPath, euiVersion) => {
  let euiResolutionsInjected = false;
  let resolutionsJsonFile;

  if (!euiVersion || euiVersion === 'DEFAULT') {
    tools.logInfo('Default eUI version found or empty...skipping');

  } else {
    tools.logInfo(`Processing resolutions replacement for eUI version found : ${euiVersion}`);
    const packageJsonFile = path.join(packageJsonPath, 'package.json');
    const packageJson = require(packageJsonFile);
    resolutionsJsonFile = path.join(__dirname, 'resources', euiVersion, 'resolutions.json');

    if (tools.isFileExists(resolutionsJsonFile)) {
      const resolutionsJson = require(resolutionsJsonFile);

      tools.logInfo('Injecting resolutions content');
      console.log(resolutionsJson);
      packageJson.resolutions = resolutionsJson;

      tools.logInfo('Updating root package.json');
      tools.writeJsonFileSync(packageJsonFile, packageJson);

      euiResolutionsInjected = true;
    }
  }

  tools.logTitle('Processing injections for detected nodeJS version');

  let nodeVersion = process.versions.node;
  nodeVersion = `${nodeVersion.split('.')[0]}.x`;

  const rootPackageJsonFile = path.join(process.cwd(), 'package.json');
  const rootPackageJson = require(rootPackageJsonFile);
  const nodeVersionResource = `nodejs-${nodeVersion}`;
  resolutionsJsonFile = path.join(__dirname, 'resources', nodeVersionResource, 'resolutions.json');

  tools.logInfo(`Checking nodejsResolution resource for path: ${resolutionsJsonFile}`);

  if (tools.isFileExists(resolutionsJsonFile)) {
    const resolutionsJson = require(resolutionsJsonFile);

    tools.logInfo('Injecting resolutions content');
    console.log(resolutionsJson);
    rootPackageJson.resolutions = resolutionsJson;
    tools.logInfo('Updating root package.json');
    tools.writeJsonFileSync(rootPackageJsonFile, rootPackageJson);

  } else {
    if (!euiResolutionsInjected) {
      tools.logInfo('Resetting resolutions...');
      tools.logInfo('Updating root package.json');
      rootPackageJson.resolutions = {};
      tools.writeJsonFileSync(rootPackageJsonFile, rootPackageJson);
    }
  }
}


module.exports.initRootFilesAndResolutions = (euiVersionInput) => {
 tools.logTitle('Processing injections for local installed eUI versions if needed');

  return Promise.resolve()
    .then(() => {
      let euiVersion;

      if (euiVersionInput) {
        euiVersion = euiVersionInput;
      } else {
        euiVersion = configUtils.global.getLocalEuiVersion();
      }

      processResolutionsInjection(process.cwd(), euiVersion);

      if (!euiVersion || euiVersion === 'DEFAULT') {
        tools.logInfo('euiVersion not found skippint yarn.lock replacement');

      } else {
        tools.logInfo(`Processing .browserlistrc replacement for eUI version found : ${euiVersion}`);

        const blResourcesFile = path.join(__dirname, 'resources', euiVersion, 'browserslistrc');
        const blDestFile = path.join(process.cwd(), '.browserslistrc');

        if (tools.isFileExists(blResourcesFile)) {
          tools.copy(blResourcesFile, blDestFile);
        } else {
          tools.logInfo('Not found...skipping');
        }

        tools.logInfo(`Processing yarn.lock replacement for eUI version found : ${euiVersion}`);

        if (registry) {
            tools.logInfo('Alternate registry found...skipping yarn.lock remplacement');
            return;
        }

        const yResourcesFile = path.join(__dirname, 'resources', euiVersion, 'yarn.lock');
        const yDestFile = path.join(process.cwd(), 'yarn.lock');

        if (tools.isFileExists(yResourcesFile)) {
          tools.copy(yResourcesFile, yDestFile);
        } else {
          tools.logInfo('Not found...skipping');
        }
      }
    })

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


module.exports.initRemoteFilesAndResolutions = (packageJsonPath, euiVersion) => {
  tools.logTitle('Processing remote injections for local installed eUI versions if needed');

  processResolutionsInjection(packageJsonPath, euiVersion);

  if (!euiVersion || euiVersion === 'DEFAULT') {
    tools.logInfo('euiVersion not found skippint yarn.lock replacement');

  } else {
    tools.logInfo(`Processing yarn.lock replacement for eUI version found : ${euiVersion}`);

    if (registry) {
        tools.logInfo('Alternate registry found...skipping yarn.lock remplacement');
        return;
    }

    const yResourcesFile = path.join(__dirname, 'resources', euiVersion, 'yarn.lock');
    const yDestFile = path.join(packageJsonPath, 'yarn.lock');

    if (tools.isFileExists(yResourcesFile)) {
      tools.copy(yResourcesFile, yDestFile);
    } else {
      tools.logInfo('Not found...skipping');
    }
  }
 }

