"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = filter;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _excluded = ["anyOf", "allOf", "includesEvery", "includesSome"],
    _excluded2 = ["target", "exists", "equals", "unequals"];

var handleLegacyFilter = function handleLegacyFilter(_ref) {
  var anyOf = _ref.anyOf,
      allOf = _ref.allOf,
      includesEvery = _ref.includesEvery,
      includesSome = _ref.includesSome,
      other = (0, _objectWithoutProperties2["default"])(_ref, _excluded);
  return allOf ? {
    $and: allOf.map(function (filter) {
      return handleOther((0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, other), filter));
    })
  } : anyOf ? {
    $or: anyOf.map(function (filter) {
      return handleOther((0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, other), filter));
    })
  } : includesEvery ? {
    $and: includesEvery.map(function ($eq) {
      return handleOther((0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, other), {}, {
        $elemMatch: {
          $eq: $eq
        }
      }));
    })
  } : includesSome ? {
    $or: includesSome.map(function ($eq) {
      return handleOther((0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, other), {}, {
        $elemMatch: {
          $eq: $eq
        }
      }));
    })
  } : handleOther(other);
};

var handleOther = function handleOther(_ref2) {
  var target = _ref2.target,
      exists = _ref2.exists,
      equals = _ref2.equals,
      unequals = _ref2.unequals,
      other = (0, _objectWithoutProperties2["default"])(_ref2, _excluded2);
  return (0, _defineProperty2["default"])({}, target, exists !== undefined ? {
    $exists: exists
  } : equals !== undefined ? {
    $eq: equals
  } : unequals !== undefined ? {
    $ne: unequals
  } : other);
};

function filter(config) {
  var _config$filter, _config$filter$show, _config$filter2, _config$filter2$enabl, _config$filter3;

  if (typeof ((_config$filter = config.filter) === null || _config$filter === void 0 ? void 0 : (_config$filter$show = _config$filter.show) === null || _config$filter$show === void 0 ? void 0 : _config$filter$show.target) === 'number') {
    config.filter.show.target = String(config.filter.show.target);
  }

  if (typeof ((_config$filter2 = config.filter) === null || _config$filter2 === void 0 ? void 0 : (_config$filter2$enabl = _config$filter2.enable) === null || _config$filter2$enabl === void 0 ? void 0 : _config$filter2$enabl.target) === 'number') {
    config.filter.enable.target = String(config.filter.enable.target);
  }

  if (config.filter && Object.keys(config.filter).length > 0) {
    for (var _i = 0, _arr = ['show', 'enable']; _i < _arr.length; _i++) {
      var _config$filter$type;

      var type = _arr[_i];

      if ((_config$filter$type = config.filter[type]) === null || _config$filter$type === void 0 ? void 0 : _config$filter$type.target) {
        config.filter[type] = {
          query: handleLegacyFilter(config.filter[type])
        };
      }
    }
  }

  if ((_config$filter3 = config.filter) === null || _config$filter3 === void 0 ? void 0 : _config$filter3.customQuery) {
    for (var _i2 = 0, _arr2 = ['show', 'enable']; _i2 < _arr2.length; _i2++) {
      var _type = _arr2[_i2];

      if (config.filter.customQuery[_type]) {
        config.filter[_type] = {
          query: config.filter.customQuery[_type].query
        };
      }
    }
  }

  return config;
}
//# sourceMappingURL=filter.cjs.map