"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

let eliminateDuplicates = inputList => {
  let outputList = [];
  let tempObj = {};

  for (let i = 0; i < inputList.length; i++) {
    tempObj[inputList[i]] = 1;
  }

  for (let j in tempObj) {
    if (tempObj[j]) {
      outputList.push(j);
    }
  }

  outputList = outputList.filter(item => item);
  return outputList;
};

let removeExtn = componentObjectList => {
  let fileNameArray = [];
  componentObjectList.forEach(fileObject => {
    let filename = fileObject.name.includes('.docs.js') ? fileObject.name.replace('.docs.js', '') : fileObject.name.replace('.js', '');
    fileNameArray.push(filename);
  });
  return fileNameArray;
};

let getChangedFileName = impactedObject => {
  let changedComponentsList = [];

  if (impactedObject.ADDED && impactedObject.ADDED.length) {
    changedComponentsList = changedComponentsList.concat(removeExtn(impactedObject.ADDED));
  }

  if (impactedObject.UPDATED && impactedObject.UPDATED.length) {
    changedComponentsList = changedComponentsList.concat(removeExtn(impactedObject.UPDATED));
  }

  if (impactedObject.REMOVED && impactedObject.REMOVED.length) {
    changedComponentsList = changedComponentsList.concat(removeExtn(impactedObject.REMOVED));
  }

  return changedComponentsList;
};

let referencedbyList = [];

let getReferencedBy = (moduleDataObject, fileName) => {
  if (moduleDataObject[fileName]) {
    let referenceObject = moduleDataObject[fileName];
    let referencedbyObject = referenceObject.referencedby;

    if (referencedbyObject.length) {
      if (!referencedbyList.includes(referenceObject.docsName)) {
        referencedbyList.push(referenceObject.docsName);
      }

      referencedbyObject.forEach(referencedByFile => {
        if (!referencedbyList.includes(moduleDataObject[referencedByFile].docsName)) {
          let result = getReferencedBy(moduleDataObject, referencedByFile);

          if (typeof result === 'string') {
            referencedbyList.push(result);
          }
        }
      });
    } else {
      return fileName;
    }
  }

  return referencedbyList;
};

let checkDocs = AppComponentObject => {
  let unDocsComponentNameList = [];
  Object.keys(AppComponentObject).forEach(actionKey => {
    AppComponentObject[actionKey].forEach(compObj => {
      if (compObj.FILEPATH.includes('/src/components/')) {
        if (compObj.name.includes('.js') || compObj.name.includes('.css')) {
          if (!compObj.name.includes('.docs.')) {
            if (!compObj.FILEPATH.includes('/src/components/docs/')) {
              unDocsComponentNameList.push(process.cwd() + compObj.FILEPATH.replace('jsapps/supportapp', ''));
            }
          }
        }
      }
    });
  });
  return unDocsComponentNameList;
};

var _default = (mDataStr, ComponentObj) => {
  let mDataObject = JSON.parse(mDataStr);
  let impactedComponentList = [];
  let unDocsComList = [];
  Object.keys(ComponentObj).forEach(libraryName => {
    if (libraryName === 'AppSource') {
      unDocsComList = checkDocs(ComponentObj[libraryName]);
    }

    let totalChangedFilesList = getChangedFileName(ComponentObj[libraryName]);
    totalChangedFilesList.forEach(changedFileName => {
      let referencedByComponents = getReferencedBy(mDataObject, changedFileName);

      if (Array.isArray(referencedByComponents)) {
        impactedComponentList = impactedComponentList.concat(referencedByComponents);
      } else if (typeof referencedByComponents === 'string') {
        impactedComponentList.push(referencedByComponents);
      }
    });
  });
  return {
    impactedComponentList: eliminateDuplicates(impactedComponentList),
    unDocsComList: unDocsComList
  };
};

exports.default = _default;