// Generated by CoffeeScript 2.3.2
(function() {
  var splice = [].splice;

  module.exports = function(sorters) {
    var ByRelevance, CompositeSorter;
    CompositeSorter = require("./config-types/composite-sorter");
    ByRelevance = require("./config-types/by-relevance");
    return function(s = "") {
      var reducer, tokens, usedSorters;
      if (s.trim() === "") {
        return new ByRelevance;
      }
      tokens = s.split(",");
      reducer = function(completedSorters, token) {
        var last, ref, ref1, rest;
        [...rest] = completedSorters, [last] = splice.call(rest, -1);
        switch (false) {
          case token !== "desc":
            return [...rest, last != null ? last.direction("desc") : void 0];
          case token !== "asc":
            return [...rest, last != null ? last.direction("asc") : void 0];
          case token[0] !== '~':
            return [...completedSorters, (ref = sorters[token.substr(1)]) != null ? ref.direction("desc") : void 0];
          case token[0] !== '^':
            return [...completedSorters, (ref1 = sorters[token.substr(1)]) != null ? ref1.direction("asc") : void 0];
          default:
            return [...completedSorters, sorters[token]];
        }
      };
      usedSorters = tokens.reduce(reducer, []).filter(function(x) {
        return x != null;
      });
      if (usedSorters.length === 1) {
        return usedSorters[0];
      } else {
        return new CompositeSorter({
          sorters: usedSorters
        });
      }
    };
  };

}).call(this);
