"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var getQueryParams = function getQueryParams(_ref) {
  var loadOptions = _ref.loadOptions,
    _ref$ignoreDeleted = _ref.ignoreDeleted,
    ignoreDeleted = _ref$ignoreDeleted === void 0 ? false : _ref$ignoreDeleted;
  // eslint-disable-next-line react-hooks/exhaustive-deps

  var params = [];

  //["attributes.name", "contains", "44"]

  ['sort', 'filter'].forEach(function (i) {
    if (i in loadOptions && isNotEmpty(loadOptions[i])) switch (i) {
      case 'sort':
        params.push(loadOptions.sort.map(function (s, index) {
          return "sort[".concat(index, "]=", "".concat(s.selector.replace('attributes.', ''), ":").concat(s.desc ? 'desc' : 'asc'));
        }).join('&'));
        break;
      case 'filter':
        params = params.concat(getFilter(loadOptions.filter)).map(function (p, index) {
          return p.replace('index', index);
        });
        break;
      default:
        break;
    }
  });
  if (ignoreDeleted) {
    params.push("filters[deleted][$eq][".concat(params.length + 1, "]=false"));
  }
  params = '&' + params.join('&');
  return params;
};
var getOperator = function getOperator(ops) {
  switch (ops) {
    case 'contains':
      return '$containsi';
    case 'notcontains':
      return '$notContainsi';
    case 'startswith':
      return '$startsWith';
    case 'endswith':
      return '$endsWith';
    case '=':
      return '$eqi';
    case '<>':
      return '$ne';
    case '<':
      return '$lt';
    case '<=':
      return '$lte';
    case '>':
      return '$gt';
    case '>=':
      return '$gte';
    case 'and':
      return '$between';
    default:
      break;
  }
};
var getFilter = function getFilter(filter, index) {
  var params = [];
  if (Array.isArray(filter[0])) {
    filter.filter(function (s) {
      return s !== 'and';
    }).forEach(function (f) {
      return params = params.concat(getFilter(f, index));
    });
  } else {
    return "filters[".concat("".concat(filter[0].replace('attributes.', ''), "][").concat(getOperator(filter[1]), "][index]=").concat(filter[2]));
  }
  return params;
};
var isNotEmpty = function isNotEmpty(value) {
  return value !== undefined && value !== null && value !== '';
};
var _default = exports["default"] = getQueryParams;