Liferay.Loader.define("commerce-frontend-js@3.0.33/utilities/index", ['module', 'exports', 'require', 'frontend-js-web', './odata'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.getAcceptLanguageHeaderParam = getAcceptLanguageHeaderParam;
    exports.getData = getData;
    exports.liferayNavigate = liferayNavigate;
    exports.getValueFromItem = getValueFromItem;
    exports.excludeFromList = excludeFromList;
    exports.executeAsyncAction = executeAsyncAction;
    exports.formatActionUrl = formatActionUrl;
    exports.getRandomId = getRandomId;
    exports.createSortingString = createSortingString;
    exports.getFiltersString = getFiltersString;
    exports.loadData = loadData;
    exports.serializeParameters = serializeParameters;
    exports.sortByKey = sortByKey;
    exports.fetchParams = exports.fetchHeaders = void 0;

    var _frontendJsWeb = require("frontend-js-web");

    var _odata = _interopRequireDefault(require("./odata"));

    function _interopRequireDefault(obj) {
      return obj && obj.__esModule ? obj : { default: obj };
    }

    function _toConsumableArray(arr) {
      return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
    }

    function _nonIterableSpread() {
      throw new TypeError("Invalid attempt to spread non-iterable instance");
    }

    function _iterableToArray(iter) {
      if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
    }

    function _arrayWithoutHoles(arr) {
      if (Array.isArray(arr)) {
        for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) {
          arr2[i] = arr[i];
        }return arr2;
      }
    }

    function ownKeys(object, enumerableOnly) {
      var keys = Object.keys(object);if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);if (enumerableOnly) symbols = symbols.filter(function (sym) {
          return Object.getOwnPropertyDescriptor(object, sym).enumerable;
        });keys.push.apply(keys, symbols);
      }return keys;
    }

    function _objectSpread(target) {
      for (var i = 1; i < arguments.length; i++) {
        var source = arguments[i] != null ? arguments[i] : {};if (i % 2) {
          ownKeys(Object(source), true).forEach(function (key) {
            _defineProperty(target, key, source[key]);
          });
        } else if (Object.getOwnPropertyDescriptors) {
          Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
        } else {
          ownKeys(Object(source)).forEach(function (key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
          });
        }
      }return target;
    }

    function _defineProperty(obj, key, value) {
      if (key in obj) {
        Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });
      } else {
        obj[key] = value;
      }return obj;
    }

    function getAcceptLanguageHeaderParam() {
      var browserLang = navigator.language || navigator.userLanguage;
      var themeLang = Liferay.ThemeDisplay.getLanguageId().replace('_', '-');

      if (browserLang === themeLang) {
        return browserLang;
      }

      return "".concat(browserLang, ", ").concat(themeLang, ";q=0.8");
    }

    var fetchHeaders = new Headers({
      Accept: 'application/json',
      'Accept-Language': getAcceptLanguageHeaderParam(),
      'Content-Type': 'application/json'
    });
    exports.fetchHeaders = fetchHeaders;
    var fetchParams = {
      headers: Liferay.staticEnvHeaders || fetchHeaders
    };
    exports.fetchParams = fetchParams;

    function getData(apiUrl, query, page, pageSize) {
      var url = new URL(apiUrl, Liferay.ThemeDisplay.getPortalURL());

      if (query) {
        url.searchParams.append('search', query);
      }

      if (page) {
        url.searchParams.append('page', page);
      }

      if (pageSize) {
        url.searchParams.append('pageSize', pageSize);
      }

      return (0, _frontendJsWeb.fetch)(url, _objectSpread({}, fetchParams)).then(function (data) {
        return data.json();
      });
    }

    function liferayNavigate(url) {
      if (Liferay.SPA) {
        Liferay.SPA.app.navigate(url);
      } else {
        window.location.href = url;
      }
    }

    function getValueFromItem(item, fieldName) {
      if (!fieldName || typeof item === 'string') {
        return null;
      }

      if (Array.isArray(fieldName)) {
        return fieldName.reduce(function (acc, key) {
          if (key === 'LANG') {
            return acc[Liferay.ThemeDisplay.getLanguageId()] || acc[Liferay.ThemeDisplay.getDefaultLanguageId()];
          }

          return acc[key];
        }, item);
      }

      return item[fieldName];
    }

    function excludeFromList(matchingList, againstList) {
      var matcher = JSON.stringify(matchingList);
      return againstList.filter(function (item) {
        return !matcher.includes(JSON.stringify(item));
      });
    }

    function executeAsyncAction(url) {
      var method = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'GET';
      var body = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
      return (0, _frontendJsWeb.fetch)(url, _objectSpread(_objectSpread({}, fetchParams), {}, {
        body: body,
        method: method
      }));
    }

    function formatActionUrl(url, item) {
      var regex = new RegExp('{(.*?)}', 'mg');
      var replacedUrl = url.replace(regex, function (matched) {
        return getValueFromItem(item, matched.substring(1, matched.length - 1).split('.'));
      });
      regex = new RegExp('(%7B.*?%7D)', 'mg');
      replacedUrl = replacedUrl.replace(regex, function (matched) {
        return getValueFromItem(item, matched.substring(3, matched.length - 3).split('.'));
      });
      return replacedUrl;
    }

    function getRandomId() {
      return Math.random().toString(36).substr(2, 9);
    }

    function createSortingString(values) {
      if (!values.length) {
        return null;
      }

      return values.map(function (value) {
        return "".concat(Array.isArray(value.fieldName) ? value.fieldName[0] : value.fieldName, ":").concat(value.direction);
      }).join(',');
    }

    function getFiltersString(filters, providedFilters) {
      var filtersString = '';

      if (filters.length || providedFilters) {
        filtersString = '&filter=';
      }

      if (providedFilters) {
        filtersString += providedFilters;
      }

      if (providedFilters && filters.length) {
        filtersString += ' and ';
      }

      if (filters.length) {
        filtersString += (0, _odata.default)(filters);
      }

      return filtersString;
    }

    function loadData(apiUrl, currentUrl, delta, filters) {
      var page = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
      var searchQuery = arguments.length > 5 ? arguments[5] : undefined;
      var sorting = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : [];
      var formattedUrl = apiUrl;
      var providedFilters = '';
      var authParam = "p_auth=".concat(window.Liferay.authToken);
      var currentUrlParam = "&currentUrl=".concat(encodeURIComponent(currentUrl));
      var pageSizeParam = "&pageSize=".concat(delta);
      var pageParam = "&page=".concat(page);
      var searchParam = searchQuery ? "&search=".concat(searchQuery) : '';
      var sortingParam = sorting.length ? "&sort=".concat(sorting.map(function (item) {
        return "".concat(item.key, ":").concat(item.direction);
      }).join(',')) : "";
      var regex = new RegExp('[?|&]filter=(.*)[&.+]?', 'mg');
      formattedUrl = formattedUrl.replace(regex, function (matched) {
        providedFilters = matched.replace(/[?|&]filter=/, '');
        return '';
      });
      var filtersParam = getFiltersString(filters, providedFilters);
      var url = "".concat(formattedUrl).concat(formattedUrl.indexOf('?') > -1 ? '&' : '?').concat(authParam).concat(currentUrlParam).concat(pageSizeParam).concat(pageParam).concat(sortingParam).concat(searchParam).concat(filtersParam);
      return executeAsyncAction(url, 'GET').then(function (response) {
        return response.json();
      });
    }

    function serializeParameters(parameters) {
      return Array.isArray(parameters) ? "?".concat(parameters.join('&')) : '';
    }

    function sortByKey(items, keyName) {
      var arrangedItems = items.reduce(function (data, item) {
        if (typeof item[keyName] === 'number') {
          return _objectSpread(_objectSpread({}, data), {}, {
            sorted: _objectSpread(_objectSpread({}, data.sorted), {}, _defineProperty({}, item[keyName], item))
          });
        } else {
          return _objectSpread(_objectSpread({}, data), {}, {
            unsortable: data.unsortable.concat(item)
          });
        }
      }, {
        sorted: {},
        unsortable: []
      });
      var sortedItems = [].concat(_toConsumableArray(Object.values(arrangedItems.sorted)), _toConsumableArray(arrangedItems.unsortable));
      return sortedItems;
    }
    //# sourceMappingURL=index.js.map
  }
});
//# sourceMappingURL=index.js.map