Liferay.Loader.define("@liferay/commerce-product-search-web@3.0.19/js/sort/view", ['module', 'exports', 'require', 'frontend-js-web'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.default = _default;

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

    /**
     * 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.
     */
    function changeOrderBy(currentURL, portletDisplayId, label) {
      var newUrl = (0, _frontendJsWeb.createPortletURL)(currentURL, {
        orderByCol: label,
        p_p_id: portletDisplayId
      });
      (0, _frontendJsWeb.navigate)(newUrl.toString());
    }

    function _default(_ref) {
      var currentURL = _ref.currentURL,
          namespace = _ref.namespace,
          portletDisplayId = _ref.portletDisplayId;
      var optionLinks = document.querySelectorAll(".sortWidgetOptions[id^=".concat(namespace, "]"));
      var commerceSortByButton = document.getElementById('commerce-order-by');
      var dropdownSortByButton = document.getElementById("".concat(namespace, "commerce-dropdown-order-by"));

      function handleOptionLinks(event) {
        event.preventDefault();
        changeOrderBy(currentURL, portletDisplayId, event.target.getAttribute('arialabel'));
      }

      optionLinks.forEach(function (link) {
        link.addEventListener('click', handleOptionLinks);
      });

      var closeDropDownOnClickOutside = function closeDropDownOnClickOutside(event) {
        if (event.target !== commerceSortByButton && dropdownSortByButton.classList.contains('show') && event.target.parentNode !== dropdownSortByButton) {
          dropdownSortByButton.classList.remove('show');
        }
      };

      window.addEventListener('mouseup', closeDropDownOnClickOutside);

      var clickOnSortButton = function clickOnSortButton() {
        if (dropdownSortByButton) {
          dropdownSortByButton.classList.toggle('show');
        }
      };

      commerceSortByButton.addEventListener('click', clickOnSortButton);
      return {
        dispose: function dispose() {
          window.removeEventListener('mouseup', closeDropDownOnClickOutside);
          commerceSortByButton.removeEventListener('click', clickOnSortButton);
          optionLinks === null || optionLinks === void 0 ? void 0 : optionLinks.forEach(function (link) {
            link.removeEventListener('click', handleOptionLinks);
          });
        }
      };
    }
    //# sourceMappingURL=view.js.map
  }
});
//# sourceMappingURL=view.js.map