"use strict";

var esprima = _interopRequireWildcard(require("esprima"));

var _escodegen = _interopRequireDefault(require("escodegen"));

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

let start = {
  type: 'ExpressionStatement',
  expression: {
    type: 'CallExpression',
    callee: {
      type: 'MemberExpression',
      computed: false,
      object: {
        type: 'MemberExpression',
        computed: false,
        object: {
          type: 'MemberExpression',
          computed: false,
          object: {
            type: 'Identifier',
            name: 'window'
          },
          property: {
            type: 'Identifier',
            name: 'ClientCoverage'
          }
        },
        property: {
          type: 'Identifier',
          name: 'JS'
        }
      },
      property: {
        type: 'Identifier',
        name: 'visitMethod'
      }
    },
    arguments: [{
      type: 'Literal',
      value: 'filename',
      raw: '\'filename\''
    }, {
      type: 'Literal',
      value: 'functionName',
      raw: '\'functionName\''
    }]
  }
};

let scriptInstrumentLoader = function (source) {
  let parsed = esprima.parseModule(source, {
    jsx: true
  });

  for (let i in parsed.body) {
    if (parsed.body[i].type === 'FunctionDeclaration' || parsed.body[i].type === 'ExportDefaultDeclaration' || parsed.body[i].type === 'ExportNamedDeclaration') {
      let ParsedTemp;

      if (parsed.body[i].type === 'FunctionDeclaration') {
        ParsedTemp = parsed.body[i];
      } else {
        ParsedTemp = parsed.body[i].declaration;
      }

      if (ParsedTemp) {
        if (ParsedTemp.id) {
          let funcname = ParsedTemp.id.name;

          if (funcname !== 'mapStateToProps') {
            if (!funcname.includes('_')) {
              let startFunc = JSON.parse(JSON.stringify(start));
              startFunc.expression.arguments[0].value = this.resourcePath;
              startFunc.expression.arguments[0].raw = this.resourcePath;
              startFunc.expression.arguments[1].value = funcname;
              startFunc.expression.arguments[1].raw = funcname;
              ParsedTemp.body.body.unshift(startFunc);
            }
          }
        }
      }
    }
  }

  return _escodegen.default.generate(parsed); // function removeDuplicates(arr) {
  //   let uniqueArray = [];
  //   for (let i = 0; i < arr.length; i++) {
  //     if (uniqueArray.indexOf(arr[i]) == -1) {
  //       uniqueArray.push(arr[i]);
  //     }
  //   }
  //   return uniqueArray;
  // }
  // funcArray = removeDuplicates(funcArray);
};

module.exports = scriptInstrumentLoader; // let start = {
//   type: 'ExpressionStatement',
//   expression: {
//     type: 'CallExpression',
//     callee: {
//       type: 'Identifier',
//       name: 'getMethodName'
//     },
//     arguments: [
//       {
//         type: 'Literal',
//         value: 'filename',
//         raw: '"filename"'
//       },
//       {
//         type: 'Literal',
//         value: 'functionname',
//         raw: '"functionname"'
//       }
//     ]
//   }
// };