"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
var _exportNames = {
  writeFile: true,
  makeDir: true,
  getInfoFromPublicPaths: true,
  getLibraryConflict: true,
  log: true,
  getOptions: true,
  createEventStream: true,
  getServerURL: true,
  getCurrentBranch: true,
  switchBranch: true,
  pullOrigin: true,
  request: true,
  jsonHelper: true,
  getDependenciesImpactList: true,
  getComponents: true,
  ssTestHack: true
};
Object.defineProperty(exports, "createEventStream", {
  enumerable: true,
  get: function () {
    return _createEventStream.default;
  }
});
Object.defineProperty(exports, "getComponents", {
  enumerable: true,
  get: function () {
    return _getComponents.default;
  }
});
Object.defineProperty(exports, "getCurrentBranch", {
  enumerable: true,
  get: function () {
    return _getCurrentBranch.default;
  }
});
Object.defineProperty(exports, "getDependenciesImpactList", {
  enumerable: true,
  get: function () {
    return _getDependenciesImpactList.default;
  }
});
exports.getLibraryConflict = exports.getInfoFromPublicPaths = void 0;
Object.defineProperty(exports, "getOptions", {
  enumerable: true,
  get: function () {
    return _getOptions.default;
  }
});
Object.defineProperty(exports, "getServerURL", {
  enumerable: true,
  get: function () {
    return _getServerURL.default;
  }
});
Object.defineProperty(exports, "jsonHelper", {
  enumerable: true,
  get: function () {
    return _jsonHelper.default;
  }
});
Object.defineProperty(exports, "log", {
  enumerable: true,
  get: function () {
    return _log.log;
  }
});
exports.makeDir = void 0;
Object.defineProperty(exports, "pullOrigin", {
  enumerable: true,
  get: function () {
    return _pullOrigin.default;
  }
});
Object.defineProperty(exports, "request", {
  enumerable: true,
  get: function () {
    return _request.default;
  }
});
Object.defineProperty(exports, "ssTestHack", {
  enumerable: true,
  get: function () {
    return _ssTestHack.default;
  }
});
Object.defineProperty(exports, "switchBranch", {
  enumerable: true,
  get: function () {
    return _switchBranch.default;
  }
});
exports.writeFile = void 0;

var _stream = require("stream");

var _fs = _interopRequireDefault(require("fs"));

var _log = require("./log");

var _getOptions = _interopRequireDefault(require("./getOptions"));

var _createEventStream = _interopRequireDefault(require("./createEventStream"));

var _getServerURL = _interopRequireDefault(require("./getServerURL"));

var _getCurrentBranch = _interopRequireDefault(require("./getCurrentBranch"));

var _switchBranch = _interopRequireDefault(require("./switchBranch"));

var _pullOrigin = _interopRequireDefault(require("./pullOrigin"));

var _request = _interopRequireDefault(require("./request"));

var _jsonHelper = _interopRequireDefault(require("./jsonHelper"));

var _reinstallDependencies = require("./reinstallDependencies");

Object.keys(_reinstallDependencies).forEach(function (key) {
  if (key === "default" || key === "__esModule") return;
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
  if (key in exports && exports[key] === _reinstallDependencies[key]) return;
  Object.defineProperty(exports, key, {
    enumerable: true,
    get: function () {
      return _reinstallDependencies[key];
    }
  });
});

var _cssURLReplacer = require("./cssURLReplacer");

Object.keys(_cssURLReplacer).forEach(function (key) {
  if (key === "default" || key === "__esModule") return;
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
  if (key in exports && exports[key] === _cssURLReplacer[key]) return;
  Object.defineProperty(exports, key, {
    enumerable: true,
    get: function () {
      return _cssURLReplacer[key];
    }
  });
});

var _initPreCommitHook = require("./initPreCommitHook");

Object.keys(_initPreCommitHook).forEach(function (key) {
  if (key === "default" || key === "__esModule") return;
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
  if (key in exports && exports[key] === _initPreCommitHook[key]) return;
  Object.defineProperty(exports, key, {
    enumerable: true,
    get: function () {
      return _initPreCommitHook[key];
    }
  });
});

var _getDependenciesImpactList = _interopRequireDefault(require("./getDependenciesImpactList"));

var _getComponents = _interopRequireDefault(require("./getComponents"));

var _ssTestHack = _interopRequireDefault(require("./ssTestHack"));

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

// eslint-disable-next-line no-duplicate-imports
let writeFile = (outputPath, src, isPath = false) => new Promise((resolve, reject) => {
  let inStr;

  if (isPath) {
    inStr = _fs.default.createReadStream(src);
  } else {
    inStr = new _stream.Readable();

    inStr._read = () => true;

    inStr.push(src);
    inStr.push(null);
  }

  let outStr = _fs.default.createWriteStream(outputPath);

  outStr.on('error', err => {
    reject(err);
  });
  outStr.on('finish', () => {
    resolve();
  });
  inStr.pipe(outStr);
});

exports.writeFile = writeFile;

let makeDir = paths => {
  if (typeof paths === 'string') {
    //eslint-disable-next-line
    paths = [paths];
  }

  paths.forEach(path => {
    if (!_fs.default.existsSync(path)) {
      _fs.default.mkdirSync(path);
    }
  });
};

exports.makeDir = makeDir;

let getInfoFromPublicPaths = publicPaths => {
  let rootFolder = '',
      image = '',
      font = '';

  if (publicPaths && !Array.isArray(publicPaths) && typeof publicPaths === 'object' && !publicPaths.callback) {
    let [cdn] = Object.keys(publicPaths);
    rootFolder = `${cdn}/`;

    if (publicPaths[cdn].font) {
      font = publicPaths[cdn].font; //eslint-disable-line
    }

    if (publicPaths[cdn].image) {
      image = publicPaths[cdn].image; //eslint-disable-line
    }
  }

  return {
    rootFolder,
    image,
    font
  };
};

exports.getInfoFromPublicPaths = getInfoFromPublicPaths;

let getLibraryConflict = (moduleObject, impactObj, changesOnly) => {
  let srcChanges = [];
  let libraryChanges = [];
  let overallSourceList = [];
  let allImpactArray = [];
  let impactThere = true;
  let whichLibrary = "";
  moduleObject = JSON.parse(moduleObject);
  Object.keys(impactObj).forEach(library => {
    Object.keys(impactObj[library]).forEach(action => {
      if (action !== 'REMOVED') {
        impactObj[library][action].forEach(srcObject => {
          let fileName = srcObject["FILEPATH"];

          if (library == "AppSource") {
            fileName = fileName.replace("jsapps/supportapp", ".");
            srcChanges.push(fileName);
          } else {
            fileName = fileName.replace("jsapps/" + library + "/src", "./node_modules/@zohodesk/" + library + "/lib");
            libraryChanges.push(fileName);
          }
        });
      }
    });
  });

  if (!changesOnly) {
    srcChanges = [];

    if (libraryChanges.length > 0) {
      libraryChanges.forEach(mdataLibKey => {
        if (moduleObject[mdataLibKey]) {
          moduleObject[mdataLibKey].referencedby.forEach(refByFileName => {
            if (refByFileName.indexOf("@zohodesk") == -1) {
              if (srcChanges.indexOf(refByFileName) == -1) {
                srcChanges.push(refByFileName);
              }
            }
          });
        }
      });
    } else {}
  }

  if (srcChanges.length > 0) {
    srcChanges.forEach(srcFile => {
      let cacheObject = {};
      cacheObject.sourceName = srcFile;
      let cacheLibraryList = [];

      if (libraryChanges.length > 0) {
        libraryChanges.forEach(mdataKey => {
          if (moduleObject[mdataKey]) {
            if (moduleObject[mdataKey].referencedby.indexOf(srcFile) > -1) {
              mdataKey = mdataKey.replace("./node_modules/", "");
              cacheLibraryList.push(mdataKey);
            }
          }
        });
      } else {
        impactThere = false;
        whichLibrary = 'No library file changes between target branch!';
      }

      if (cacheLibraryList.length > 0) {
        cacheObject.impactedBy = cacheLibraryList;
        allImpactArray.push(cacheObject);
      } else {
        impactThere = false;
        whichLibrary = "No component file changes!";
      }
    });
  } else {
    impactThere = false;
    whichLibrary = "No component file changes!";
  }

  (0, _log.log)('\n');
  (0, _log.log)('You can see the HTML out at coverageTest/impactLibrary.html!');
  (0, _log.log)('\x1b[33m%s\x1b[0m', '************************************************************ \n ');
  return {
    response: allImpactArray,
    result: {
      status: impactThere,
      message: whichLibrary
    }
  };
};

exports.getLibraryConflict = getLibraryConflict;