Liferay.Loader.define("segments-web@2.0.27/js/utils/odata.es", ['module', 'exports', 'require', './constants.es'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.buildQueryString = buildQueryString;
    exports.unescapeSingleQuotes = unescapeSingleQuotes;

    var _constants = require("./constants.es");

    /**
     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
     *
     * This library is free software; you can redistribute it and/or modify it under
     * the terms of the GNU Lesser General Public License as published by the Free
     * Software Foundation; either version 2.1 of the License, or (at your option)
     * any later version.
     *
     * This library is distributed in the hope that it will be useful, but WITHOUT
     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
     * details.
     */

    /**
     * Gets the type of the property from the property name.
     * @param {string} propertyName The property name to find.
     * @param {array} properties The list of defined properties to search in.
     * @returns {string} The property type.
     */
    var getTypeByPropertyName = function getTypeByPropertyName(propertyName, properties) {
      var type = null;

      if (propertyName && properties) {
        var property = properties.find(function (property) {
          return property.name === propertyName;
        });
        type = property ? property.type : null;
      }

      return type;
    };
    /**
     * Handles the single quotes present in the value.
     * Should escape the single quotes present in the value before hit the backend.
     * @param {string | *} value
     * @returns {string | *}
     */

    function escapeSingleQuotes(value) {
      if (typeof value !== 'string') {
        return value;
      }

      return "'".concat(value.replace(/'/g, "''"), "'");
    }
    /**
     * Handles the single quotes present in the value.
     * Should un-escape the single quotes present in odata format before rendering.
     * @param {string | *} value
     * @returns {string | *}
     */

    function unescapeSingleQuotes(value) {
      if (typeof value !== 'string') {
        return value;
      }

      return value.replace(/''/g, "'");
    }
    /**
     * Decides whether to add quotes to value.
     * @param {boolean | string} value
     * @param {boolean | date | number | string} type
     * @returns {string}
     */

    function valueParser(value, type) {
      var parsedValue;

      switch (type) {
        case _constants.PROPERTY_TYPES.BOOLEAN:
        case _constants.PROPERTY_TYPES.DATE:
        case _constants.PROPERTY_TYPES.DATE_TIME:
        case _constants.PROPERTY_TYPES.INTEGER:
        case _constants.PROPERTY_TYPES.DOUBLE:
          parsedValue = value;
          break;

        case _constants.PROPERTY_TYPES.COLLECTION:
        case _constants.PROPERTY_TYPES.STRING:
        default:
          parsedValue = escapeSingleQuotes(value);
          break;
      }

      return parsedValue;
    }
    /**
     * Recursively traverses the criteria object to build an oData filter query
     * string. Properties is required to parse the correctly with or without quotes
     * and formatting the query differently for certain types like collection.
     * @param {object} criteria The criteria object.
     * @param {string} queryConjunction The conjunction name value to be used in the
     * query.
     * @param {array} properties The list of property objects. See
     * ContributorBuilder for valid property object shape.
     * @returns An OData query string built from the criteria object.
     */

    function buildQueryString(criteria, queryConjunction, properties) {
      return criteria.filter(Boolean).reduce(function (queryString, criterion, index) {
        var conjunctionName = criterion.conjunctionName,
            items = criterion.items,
            operatorName = criterion.operatorName,
            propertyName = criterion.propertyName,
            value = criterion.value;

        if (index > 0) {
          queryString = queryString.concat(" ".concat(queryConjunction, " "));
        }

        if (conjunctionName) {
          queryString = queryString.concat("(".concat(buildQueryString(items, conjunctionName, properties), ")"));
        } else {
          var type = criterion.type || getTypeByPropertyName(propertyName, properties);
          var parsedValue = valueParser(value, type);

          if (isValueType(_constants.RELATIONAL_OPERATORS, operatorName)) {
            if (type === _constants.PROPERTY_TYPES.COLLECTION) {
              queryString = queryString.concat("".concat(propertyName, "/any(c:c ").concat(operatorName, " ").concat(parsedValue, ")"));
            } else {
              queryString = queryString.concat("".concat(propertyName, " ").concat(operatorName, " ").concat(parsedValue));
            }
          } else if (isValueType(_constants.FUNCTIONAL_OPERATORS, operatorName)) {
            if (type === _constants.PROPERTY_TYPES.COLLECTION) {
              queryString = queryString.concat("".concat(propertyName, "/any(c:").concat(operatorName, "(c, ").concat(parsedValue, "))"));
            } else {
              queryString = queryString.concat("".concat(operatorName, "(").concat(propertyName, ", ").concat(parsedValue, ")"));
            }
          } else if (isValueType(_constants.NOT_OPERATORS, operatorName)) {
            var baseOperator = operatorName.replace(/not-/g, '');
            // Not is wrapped in a group to simplify AST parsing.

            queryString = queryString.concat("(not (".concat(buildQueryString([{
              operatorName: baseOperator,
              propertyName: propertyName,
              type: type,
              value: value
            }], conjunctionName, properties), "))"));
          }
        }

        return queryString;
      }, '');
    }
    /**
     * Checks if the value is a certain type.
     * @param {object} types A map of supported types.
     * @param {*} value The value to validate.
     * @returns {boolean}
     */

    function isValueType(types, value) {
      return Object.values(types).includes(value);
    }
    //# sourceMappingURL=odata.es.js.map
  }
});
//# sourceMappingURL=odata.es.js.map