'use strict';

/**
 * Broadcast module used for working with sorter.
 * Including:
 *   Sorter
 */

angular.module('system.services.sorter', [
    'config.services'
])
    .service('Sorter', [
        'Config',
        function() {
            var asc = "Ascending";
            var desc = "Descending";

            function Sorter(query) {
                this.query = query;

                this.sort = function(by, direction) {
                    if (by)
                        angular.extend(this.query, { sortBy: by });

                    if (this.query.sortBy == by)
                        this.query.sortDirection = (this.query.sortDirection == asc) ? desc : asc;
                    else
                        this.query.sortDirection = direction;
                };

                this.reset = function() {
                    this.query.sortBy = null;
                    this.query.sortDirection = null;
                };
            }

            var create = function(query) {
                if (!query)
                    throw new Error("Sorter > create(): method must receive non-nullable and non-undefined query parameter");

                var sorter = new Sorter(query);
                return sorter;
            };

            return {
                create: create
            };
        }
    ]);