"use strict";

var _path = _interopRequireDefault(require("path"));

var _utils = require("../utils");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function jsFileFilter(listOfObject) {
  let fileNameList = [];
  let curListName = process.cwd().split(_path.default.sep);
  let currentDirName = curListName[curListName.length - 1];
  listOfObject.forEach(fileObj => {
    let fileName = fileObj.FILEPATH;

    if (fileName.includes(currentDirName)) {
      fileName = fileName.split(currentDirName + '/')[1]; //eslint-disable-line

      if (fileName.includes('.js') && !fileName.includes('.json') && !fileName.includes('.docs.js') && !fileName.includes('.spec.js')) {
        fileNameList.push(fileName);
      }
    }
  });
  return fileNameList;
}

function coverage(devBranch, compareWith, serviceName, domain) {
  return new Promise(resolve => {
    (0, _utils.request)({
      url: `${domain}/impact/ImpactModuleAction.do?action=getCommitDiffFileDetail&serviceName=${serviceName}&masterBranchCommitHash=${compareWith}&buildCommitHash=${devBranch}`
    }).then(({
      body
    }) => {
      let res = JSON.parse(body);

      if (Object.keys(res).length > 0) {
        if (Object.prototype.hasOwnProperty.call(res, 'STATUS')) {
          if (res.STATUS) {
            let listOfFiles = [];

            if (Object.keys(res.DIFF_FILES).length > 0) {
              if (Object.prototype.hasOwnProperty.call(res.DIFF_FILES, 'ADDED')) {
                if (res.DIFF_FILES.ADDED.length > 0) {
                  listOfFiles = listOfFiles.concat(jsFileFilter(res.DIFF_FILES.ADDED));
                }
              }

              if (Object.prototype.hasOwnProperty.call(res.DIFF_FILES, 'UPDATED')) {
                if (res.DIFF_FILES.UPDATED.length > 0) {
                  listOfFiles = listOfFiles.concat(jsFileFilter(res.DIFF_FILES.UPDATED));
                }
              }

              if (listOfFiles.length > 0) {
                resolve({
                  STATUS: true,
                  LIST: listOfFiles,
                  REASON: 'success'
                });
              } else {
                resolve({
                  STATUS: false,
                  LIST: listOfFiles,
                  REASON: 'No modified js files'
                });
              }
            } else {
              resolve({
                STATUS: false,
                LIST: [],
                REASON: 'No changes with last changeset'
              });
            }
          } else {
            resolve({
              STATUS: false,
              LIST: [],
              REASON: 'Internal Error'
            });
          }
        } else {
          resolve({
            STATUS: false,
            LIST: [],
            REASON: 'Internal Error'
          });
        }
      } else {
        resolve({
          STATUS: false,
          LIST: [],
          REASON: 'Internal Error'
        });
      }
    }).catch(err => {
      (0, _utils.log)(err);
      resolve({
        STATUS: false,
        LIST: [],
        REASON: 'Service Down'
      });
    });
  });
}

module.exports = coverage;