"use strict";

const {
  eslintPackages,
  prettierPackages,
  eslintPrettierConfig,
  runInstall,
  installLintStaged
} = require('./lintSetup');

const {
  log
} = require('..');

const {
  eslintConfigFiles,
  prettierConfigFiles,
  getChoices
} = require('./checkExistingConfig');

const copyConfigs = require('./copyConfigs');

async function main() {
  const packages = [];
  const [isESLintAllowed, isPrettierAllowed, isHookInstallEnabled] = await getChoices();

  if (isESLintAllowed) {
    copyConfigs('eslintrc', eslintConfigFiles);
    packages.push(...eslintPackages);
  }

  if (isPrettierAllowed) {
    copyConfigs('prettierrc', prettierConfigFiles);
    packages.push(...prettierPackages);
  }

  if (isESLintAllowed && isPrettierAllowed) {
    packages.push(...eslintPrettierConfig);
  }

  if (packages.length) {
    const process = runInstall(packages);
    process.on('close', () => {
      if (isHookInstallEnabled) {
        installLintStaged();
      }
    });
  } else if (isHookInstallEnabled) {
    installLintStaged();
  }
}

log('[LINT SETUP] Beginning configuration...');
main();