// Generated by ReScript, PLEASE EDIT WITH CARE
'use strict';

var Curry = require("bs-platform/lib/js/curry.js");
var Intl$OptolithClient = require("./Intl.bs.js");
var ListH$OptolithClient = require("../../Data/ListH.bs.js");
var Function$OptolithClient = require("../../Data/Function.bs.js");

function searchByMulti(searchAccessors, filterText, xs) {
  if (searchAccessors) {
    return ListH$OptolithClient.filter((function (x) {
                  return ListH$OptolithClient.Foldable.any((function (pred) {
                                if (pred.TAG) {
                                  return ListH$OptolithClient.Foldable.any((function (x$prime) {
                                                return ListH$OptolithClient.isInfixOf(ListH$OptolithClient.Extra.lower(filterText), ListH$OptolithClient.Extra.lower(x$prime));
                                              }), Curry._1(pred._0, x));
                                } else {
                                  return ListH$OptolithClient.isInfixOf(ListH$OptolithClient.Extra.lower(filterText), ListH$OptolithClient.Extra.lower(Curry._1(pred._0, x)));
                                }
                              }), searchAccessors);
                }), xs);
  } else {
    return xs;
  }
}

function searchStrings(filterText, xs) {
  return ListH$OptolithClient.Foldable.elem(filterText, xs);
}

function sortByMulti(sortOptions, xs) {
  if (ListH$OptolithClient.Foldable.length(xs) < 2 || ListH$OptolithClient.Foldable.$$null(sortOptions)) {
    return xs;
  }
  var sortFunctions = ListH$OptolithClient.map((function (x) {
          if (!x.reverse) {
            return x.compare;
          }
          var partial_arg = x.compare;
          return function (param, param$1) {
            return Function$OptolithClient.flip(partial_arg, param, param$1);
          };
        }), sortOptions);
  return ListH$OptolithClient.sortBy(function (param, param$1) {
                var _sortFunctions = sortFunctions;
                while(true) {
                  var sortFunctions$1 = _sortFunctions;
                  if (!sortFunctions$1) {
                    return /* EQ */1;
                  }
                  var match = Curry._2(sortFunctions$1.hd, param, param$1);
                  if (match !== 1) {
                    return match;
                  }
                  _sortFunctions = sortFunctions$1.tl;
                  continue ;
                };
              })(xs);
}

function sortStrings(staticData, xs) {
  var partial_arg = Intl$OptolithClient.Collator.createWithOptions(staticData.messages.id, {
        numeric: true
      });
  return ListH$OptolithClient.sortBy(function (param, param$1) {
                return Intl$OptolithClient.Collator.compare(partial_arg, param, param$1);
              })(xs);
}

function searchAndSortByMulti(searchAccessors, sortOptions, filterText, xs) {
  return sortByMulti(sortOptions, searchByMulti(searchAccessors, filterText, xs));
}

exports.searchByMulti = searchByMulti;
exports.searchStrings = searchStrings;
exports.sortByMulti = sortByMulti;
exports.sortStrings = sortStrings;
exports.searchAndSortByMulti = searchAndSortByMulti;
/* No side effect */
