'use strict';

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

function get(config, pkg, inputOpt) {

  const DEFAULT_OPTS = {
    files: [],
    basePath: '../../../',
    packageRootPath: '../',
  };

  const opt = Object.assign(DEFAULT_OPTS, inputOpt);

    // transpiled app JS and map files
    const libPath = opt.packageRootPath + pkg + '/';

    const defaultConfig = {
        basePath: libPath,
        frameworks: ['jasmine', '@angular-devkit/build-angular'],
        plugins: [
          require('karma-jasmine'),
          require('karma-chrome-launcher'),
          require('karma-jasmine-html-reporter'),
          require('karma-coverage-istanbul-reporter'),
          require('@angular-devkit/build-angular/plugins/karma'),
          require('karma-coverage'),
          require('karma-remap-coverage')
        ],
        client: {
          clearContext: false // leave Jasmine Spec Runner output visible in browser
        },
        coverageIstanbulReporter: {
          // dir: require('path').join(__dirname, './coverage'),
          dir: require('path').join(opt.packageRootPath + pkg, 'test', 'reports', 'coverage'),
          reports: ['html', 'lcovonly'],
          fixWebpackSourcePaths: true
        },
        exclude: [],
        preprocessors: {},
        reporters: ['progress', 'coverage', 'remap-coverage'],

        htmlReporter: {
            outputDir: opt.packageRootPath + pkg + '/test/reports', // where to put the reports
            templatePath: null, // set if you moved jasmine_template.html
            focusOnFailures: true, // reports show failures on start
            namedFiles: true, // name files instead of creating sub-directories
            pageTitle: null, // page title for reports; browser info by default
            urlFriendlyName: false, // simply replaces spaces with _ for files/dirs
            reportName: 'test-report-summary', // report summary filename; browser info by default


            // experimental
            preserveDescribeNesting: false, // folded suites stay folded
            foldAll: false, // reports start folded (only with preserveDescribeNesting)
        },

        coverageReporter: {
            type: 'in-memory',
            includeAllSources: true,
        },

        remapCoverageReporter: {
            'text-summary': null,
            html: opt.packageRootPath + pkg + '/test/reports' + '/html',
            lcovonly: opt.packageRootPath + pkg + '/test/reports' + '/coverage-lcov/lcov.info',
            json: opt.packageRootPath + pkg + '/test/reports' + '/coverage/coverage-final.json',
        },

        customLaunchers: {
          MyHeadlessChrome: {
            base: 'ChromeHeadless',
            flags: [
              '--no-sandbox',
              '--remote-debugging-port=9223',
              '--disable-translate',
              '--disable-extensions'
            ]
          }
        },
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ['MyHeadlessChrome'],
        singleRun: true,
        browserDisconnectTolerance: 2,
        browserNoActivityTimeout: 50000
    };

    defaultConfig.preprocessors[opt.packageRootPath + pkg + '/src/lib/**/!(*.spec).js'] = 'coverage';

    return defaultConfig;
}

module.exports.get = get;
