// Generated by CoffeeScript 2.3.2
(function() {
  var CompositeSorter, ConfigNode, flip;

  ({ConfigNode} = require("@l.degener/irma-config"));

  flip = function(sorter) {
    var d;
    d = sorter.direction();
    return sorter.direction((function() {
      switch (d) {
        case 'asc':
          return 'desc';
        case 'desc':
          return 'asc';
        default:
          throw new Error(`Bad direction: ${d}`);
      }
    })());
  };

  module.exports = CompositeSorter = class CompositeSorter extends ConfigNode {
    constructor(options) {
      var direction, sorters;
      ({sorters, direction = 'asc'} = options);
      super(options);
      this._sorters = sorters;
      this._direction = direction;
    }

    direction(string) {
      if (string != null) {
        return new CompositeSorter({
          sorters: this._sorters,
          direction: string
        });
      } else {
        return this._direction;
      }
    }

    sort() {
      return this._sorters.map((s) => {
        if (this._direction === 'desc') {
          return flip(s).sort();
        } else {
          return s.sort();
        }
      }).reduce((function(a, b) {
        return a.concat(b);
      }), []);
    }

  };

}).call(this);
