/**
 * Created on 06/02/19 for the PhpServer project.
 * @copyright (c) 2019 exclusive for Visisoft OHG. All rights reserved.
 */

/** @namespace SharedLib */

/**
 * @typedef SharedLib~PriorityQueueFactory
 * @function
 * @template T
 * @param {function(T, T): Boolean} comparator Comparison function like for <code>Array.prototype.sort</code>
 * @return {{pop: function(Array<T>): Array<Array<T>| T>, push: function(Array<T>, T): Array<T>}} an object containing the functions to manage the queue
 * @public
 */

(function (root, factory) {
	if (typeof define === 'function' && define.amd) {
		define([], factory);
	}
	else if (typeof module === 'object' && module.exports) {
		module.exports = factory();
	}
	else {
		root.returnExports = factory();
	}
}(typeof self !== 'undefined' ? self : this,
	function () {

		/** @type {PriorityQueueFactory} */
		function priorityQueueFactory(comparator) {
			const
				push = function(queue, item) {
					const
						clonedQueue = queue.slice();

					clonedQueue.push(item);

					return clonedQueue.sort(comparator);
				},

				pop = function(queue) {
					const
						clonedQueue = queue.slice(1);

					// since [][0] == undefined is it not wise to put undefined in this priority queue
					return [clonedQueue, queue[0]];
				};

			return {
				push: push,
				pop: pop
			};
		}

		return priorityQueueFactory;
	}));