define([
  'underscore'
], function(
  _
) {
  function CavProcessor (cavList, crmList, extList) {
    _cavMap = {};
    _.each(cavList, function (value, key) {
      _cavMap['@MSDYNAMICS.' + key.toUpperCase() +'@'] = value;
    });
    _.each(crmList, function (value, key) {
      _cavMap['@CUSTOMER.' + key.toUpperCase() + '@'] = value;
    });
    _.each(extList, function (value, key) {
      _cavMap['@' + key.toUpperCase() + '@'] = value;
    });

    this.replaceCAVs = function  (text) {
      console.debug('cav.api:replaceCAVs', text);
      if (text) {
        text = text.replace(/@[^'*`&%#,!|"()^\/\\<>:;?\[\]{}+=]+@/ig, function(match) {
          var cavValue = _cavMap[match.toUpperCase()];
          console.debug('cav.api:replaceCAVs cavValue = ', cavValue, ', match = ', match);
          return cavValue ? cavValue : '';
        });
      }

      return text;
    };
  }

  function getCavProcessor (cavList, crmList, extList) {
    return new CavProcessor(cavList, crmList, extList);
  }

  return {
    getCavProcessor: getCavProcessor
  }
});
