/* globals alert */
'use strict';

/**
 * @ngdoc service
 * @name annotationAppApp.utils
 * @description
 * # utils
 * Service in the annotationAppApp.
 */
angular.module('app')
	.service('Utils', function($log, $q, TextReview) {
		return {
			// TODO: evaluate if the following method is used
			Reviews: {
				indexOf: function(review, product, languages, dataSource) {
					var def,
						index;

					def = $q.defer();

					TextReview.getDistinctIdsByProductAndLanguages(product, languages, dataSource).then(function(ids) {

						index = ids.sort().indexOf(review._id);

						if (index > -1) {
							index = index + 1;
						}

						def.resolve({
							index: index,
							total: ids.length
						});

					}, function(err) {
						def.reject(err);
						alert(JSON.stringify(err, null, 4));
					});
					return def.promise;
				}
			}
		};
	});