'use strict';

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

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

// fetching command parameters
const { dryRun, skipClone } = tools.getArgs();


module.exports.cloneMetadataRepo = (shallow = false) => {
  const configOptions = configUtils.global.getConfigOptions();

  tools.logTitle('Cloning devops metadata repo');

  if (dryRun || skipClone) {
    tools.logInfo('DRY-RUN: cloning skipped');
    return Promise.resolve();

  } else {
    return Promise.resolve()
      .then(() => {
        return gitUtils.cloneRepo(configOptions.DEVOPS_METADATA_REPOSITORY, configOptions.DEVOPS_METADATA_PATH, false, shallow);
      })
      .catch((e) => {
        throw e;
      })
  }
}


module.exports.cloneMetadataLocksRepo = () => {
  const configOptions = configUtils.global.getConfigOptions();

  tools.logTitle('Cloning devops metadata locks repo');

  if (dryRun || skipClone) {
    tools.logInfo('DRY-RUN: cloning skipped');
    return Promise.resolve();

  } else {
    return Promise.resolve()
      .then(() => {
        return gitUtils.cloneRepo(configOptions.DEVOPS_METADATA_LOCKS_REPOSITORY, configOptions.DEVOPS_METADATA_LOCKS_PATH, false, true);
      })
      .catch((e) => {
        throw e;
      })
  }
}


module.exports.cloneMetadataRepoStandalone = () => {
  const configOptions = configUtils.global.getConfigOptionsStandalone();

  tools.logTitle('Cloning devops metadata repo');

  return Promise.resolve()
    .then(() => {
      return gitUtils.cloneRepoStandalone(
        configOptions.DEVOPS_METADATA_REPOSITORY,
        configOptions.DEVOPS_METADATA_PATH
      );
    })
    .catch((e) => {
      throw e;
    })
}
