'use strict';

/**
 * @ngdoc service
 * @name brandDashboardApp.dataSourceFilter
 * @description
 * # dataSourceFilter
 * Service in the brandDashboardApp.
 */
angular.module('DataManagement',[])
	.service('DataSourceFilter', function($log) {

		return {
			isCallCenterChannel:function(channel){
				if (!channel){
					return null;
				}
				return channel.match(/call\-center$/gi);
			},
			getRegexToMatchStringNotEndingInCallCenter:function(){
				return /^((?!call.center).)*$/gi;
			},
			// TODO: change who is calling this fuction: parameter was removed
			_getCallCenterDataFilteringCallback:function(){
				var self=this;
				return function(review){
					return review.review && review.review.channel && self.isCallCenterChannel(review.review.channel);
				};
			},
			_getRatingAndReviewsDataFilteringCallback:function(){
				var self=this;
				return function(review){
					return review.review && review.review.channel && !self.isCallCenterChannel(review.review.channel);
				};
			},
			getFilteringCallback:function(enabledDataSources){

				if (enabledDataSources.indexOf('call-center')>=0){
					return this._getCallCenterDataFilteringCallback();
				}

				return this._getRatingAndReviewsDataFilteringCallback();
			},
			run: function(reviews, enabledDataSources) {

				var result;

				if (!reviews){
					return [];
				}

				$log.info('DataSourceFilter: Initial Reviews are', reviews.length);
				
				
				if (enabledDataSources.length===2){
					result=reviews;
				}else {
					result=reviews.filter(this.getFilteringCallback(enabledDataSources));
				}


				$log.info('DataSourceFilter: Filtered Reviews are', result.length);

				return result;


			}
		};

	});