"use strict";

Liferay.Loader.define("@frontend-taglib-clay$clayui/drop-down@3.58.0/lib/DropDownWithItems", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/form', '@frontend-taglib-clay$clayui/shared', 'frontend-js-react-web$react', 'frontend-taglib-clay$warning', './Caption', './Divider', './DropDown', './Group', './Help', './Search'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    function _typeof(obj) {
      "@babel/helpers - typeof";
      if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
        _typeof = function _typeof(obj) {
          return typeof obj;
        };
      } else {
        _typeof = function _typeof(obj) {
          return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
        };
      }return _typeof(obj);
    }

    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.ClayDropDownWithItems = void 0;

    var _form = require("@frontend-taglib-clay$clayui/form");

    var _shared = require("@frontend-taglib-clay$clayui/shared");

    var _react = _interopRequireWildcard(require("frontend-js-react-web$react"));

    var _warning = _interopRequireDefault(require("frontend-taglib-clay$warning"));

    var _Caption = _interopRequireDefault(require("./Caption"));

    var _Divider = _interopRequireDefault(require("./Divider"));

    var _DropDown = _interopRequireDefault(require("./DropDown"));

    var _Group = _interopRequireDefault(require("./Group"));

    var _Help = _interopRequireDefault(require("./Help"));

    var _Search = _interopRequireDefault(require("./Search"));

    var _excluded = ["checked", "onChange"],
        _excluded2 = ["label", "onClick"],
        _excluded3 = ["items", "label", "spritemap"],
        _excluded4 = ["value"],
        _excluded5 = ["type"];

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

    function _getRequireWildcardCache(nodeInterop) {
      if (typeof WeakMap !== "function") return null;var cacheBabelInterop = new WeakMap();var cacheNodeInterop = new WeakMap();return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
      })(nodeInterop);
    }

    function _interopRequireWildcard(obj, nodeInterop) {
      if (!nodeInterop && obj && obj.__esModule) {
        return obj;
      }if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") {
        return { default: obj };
      }var cache = _getRequireWildcardCache(nodeInterop);if (cache && cache.has(obj)) {
        return cache.get(obj);
      }var newObj = {};var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;for (var key in obj) {
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
          var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;if (desc && (desc.get || desc.set)) {
            Object.defineProperty(newObj, key, desc);
          } else {
            newObj[key] = obj[key];
          }
        }
      }newObj.default = obj;if (cache) {
        cache.set(obj, newObj);
      }return newObj;
    }

    function _extends() {
      _extends = Object.assign || function (target) {
        for (var i = 1; i < arguments.length; i++) {
          var source = arguments[i];for (var key in source) {
            if (Object.prototype.hasOwnProperty.call(source, key)) {
              target[key] = source[key];
            }
          }
        }return target;
      };return _extends.apply(this, arguments);
    }

    function _slicedToArray(arr, i) {
      return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
    }

    function _nonIterableRest() {
      throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
    }

    function _unsupportedIterableToArray(o, minLen) {
      if (!o) return;if (typeof o === "string") return _arrayLikeToArray(o, minLen);var n = Object.prototype.toString.call(o).slice(8, -1);if (n === "Object" && o.constructor) n = o.constructor.name;if (n === "Map" || n === "Set") return Array.from(o);if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
    }

    function _arrayLikeToArray(arr, len) {
      if (len == null || len > arr.length) len = arr.length;for (var i = 0, arr2 = new Array(len); i < len; i++) {
        arr2[i] = arr[i];
      }return arr2;
    }

    function _iterableToArrayLimit(arr, i) {
      var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];if (_i == null) return;var _arr = [];var _n = true;var _d = false;var _s, _e;try {
        for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
          _arr.push(_s.value);if (i && _arr.length === i) break;
        }
      } catch (err) {
        _d = true;_e = err;
      } finally {
        try {
          if (!_n && _i["return"] != null) _i["return"]();
        } finally {
          if (_d) throw _e;
        }
      }return _arr;
    }

    function _arrayWithHoles(arr) {
      if (Array.isArray(arr)) return arr;
    }

    function _objectWithoutProperties(source, excluded) {
      if (source == null) return {};var target = _objectWithoutPropertiesLoose(source, excluded);var key, i;if (Object.getOwnPropertySymbols) {
        var sourceSymbolKeys = Object.getOwnPropertySymbols(source);for (i = 0; i < sourceSymbolKeys.length; i++) {
          key = sourceSymbolKeys[i];if (excluded.indexOf(key) >= 0) continue;if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;target[key] = source[key];
        }
      }return target;
    }

    function _objectWithoutPropertiesLoose(source, excluded) {
      if (source == null) return {};var target = {};var sourceKeys = Object.keys(source);var key, i;for (i = 0; i < sourceKeys.length; i++) {
        key = sourceKeys[i];if (excluded.indexOf(key) >= 0) continue;target[key] = source[key];
      }return target;
    }

    var findNested = function findNested(items, key) {
      return items.find(function (item) {
        if (item[key]) {
          return true;
        } // Ignore the search if the nested items are part of a contextual submenu
        // because it will be in another menu and the current menu does not need
        // to know the information of what exists inside the contextual one, like
        // knowing if there is an icon.


        if (item.items && item.type !== 'contextual') {
          return findNested(item.items, key);
        }

        return false;
      });
    };

    var Checkbox = function Checkbox(_ref) {
      var _ref$checked = _ref.checked,
          checked = _ref$checked === void 0 ? false : _ref$checked,
          _ref$onChange = _ref.onChange,
          _onChange = _ref$onChange === void 0 ? function () {} : _ref$onChange,
          otherProps = _objectWithoutProperties(_ref, _excluded);

      var _useState = (0, _react.useState)(checked),
          _useState2 = _slicedToArray(_useState, 2),
          value = _useState2[0],
          setValue = _useState2[1];

      return (/*#__PURE__*/_react.default.createElement(_DropDown.default.Section, null, /*#__PURE__*/_react.default.createElement(_form.ClayCheckbox, _extends({}, otherProps, {
          checked: value,
          onChange: function onChange() {
            setValue(function (val) {
              return !val;
            });

            _onChange(!value);
          }
        })))
      );
    };

    var ClayDropDownContext = /*#__PURE__*/_react.default.createContext({
      close: function close() {}
    });

    var Item = function Item(_ref2) {
      var label = _ref2.label,
          _onClick = _ref2.onClick,
          props = _objectWithoutProperties(_ref2, _excluded2);

      var _useContext = (0, _react.useContext)(ClayDropDownContext),
          close = _useContext.close;

      return (/*#__PURE__*/_react.default.createElement(_DropDown.default.Item, _extends({
          onClick: function onClick(event) {
            if (_onClick) {
              _onClick(event);
            }

            close();
          }
        }, props), label)
      );
    };

    var Group = function Group(_ref3) {
      var items = _ref3.items,
          label = _ref3.label,
          spritemap = _ref3.spritemap;
      return (/*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_Group.default, {
          header: label
        }), items && /*#__PURE__*/_react.default.createElement(DropDownContent, {
          items: items,
          spritemap: spritemap
        }))
      );
    };

    var Contextual = function Contextual(_ref4) {
      var items = _ref4.items,
          label = _ref4.label,
          spritemap = _ref4.spritemap,
          otherProps = _objectWithoutProperties(_ref4, _excluded3);

      var _useState3 = (0, _react.useState)(false),
          _useState4 = _slicedToArray(_useState3, 2),
          visible = _useState4[0],
          setVisible = _useState4[1];

      var _useContext2 = (0, _react.useContext)(ClayDropDownContext),
          _close = _useContext2.close;

      var triggerElementRef = (0, _react.useRef)(null);
      var menuElementRef = (0, _react.useRef)(null);
      var timeoutHandleRef = (0, _react.useRef)(null);

      var hasRightSymbols = _react.default.useMemo(function () {
        return items && !!findNested(items, 'symbolRight');
      }, [items]);

      var hasLeftSymbols = _react.default.useMemo(function () {
        return items && !!findNested(items, 'symbolLeft');
      }, [items]);

      return (/*#__PURE__*/_react.default.createElement(_DropDown.default.Item, _extends({}, otherProps, {
          onClick: function onClick(event) {
            if (event.currentTarget === event.target) {
              setVisible(true);
              clearTimeout(timeoutHandleRef.current);
              timeoutHandleRef.current = null;
            }
          },
          onMouseEnter: function onMouseEnter() {
            if (!visible) {
              timeoutHandleRef.current = setTimeout(function () {
                return setVisible(true);
              }, 500);
            }
          },
          onMouseLeave: function onMouseLeave() {
            setVisible(false);
            clearTimeout(timeoutHandleRef.current);
            timeoutHandleRef.current = null;
          },
          ref: triggerElementRef,
          spritemap: spritemap,
          symbolRight: "angle-right"
        }), label, items && /*#__PURE__*/_react.default.createElement(_DropDown.default.Menu, {
          active: visible,
          alignElementRef: triggerElementRef,
          alignmentPosition: 8,
          hasLeftSymbols: hasLeftSymbols,
          hasRightSymbols: hasRightSymbols,
          onSetActive: setVisible,
          ref: menuElementRef
        }, visible && /*#__PURE__*/_react.default.createElement(_shared.MouseSafeArea, {
          parentRef: menuElementRef
        }), /*#__PURE__*/_react.default.createElement(ClayDropDownContext.Provider, {
          value: {
            close: function close() {
              setVisible(false);

              _close();
            }
          }
        }, /*#__PURE__*/_react.default.createElement(DropDownContent, {
          items: items,
          spritemap: spritemap
        }))))
      );
    };

    var RadioGroupContext = /*#__PURE__*/_react.default.createContext({});

    var Radio = function Radio(_ref5) {
      var _ref5$value = _ref5.value,
          value = _ref5$value === void 0 ? '' : _ref5$value,
          otherProps = _objectWithoutProperties(_ref5, _excluded4);

      var _useContext3 = (0, _react.useContext)(RadioGroupContext),
          checked = _useContext3.checked,
          name = _useContext3.name,
          _onChange2 = _useContext3.onChange;

      return (/*#__PURE__*/_react.default.createElement(_DropDown.default.Section, null, /*#__PURE__*/_react.default.createElement(_form.ClayRadio, _extends({}, otherProps, {
          checked: checked === value,
          inline: true,
          name: name,
          onChange: function onChange() {
            return _onChange2(value);
          },
          value: value
        })))
      );
    };

    var RadioGroup = function RadioGroup(_ref6) {
      var items = _ref6.items,
          label = _ref6.label,
          name = _ref6.name,
          _ref6$onChange = _ref6.onChange,
          _onChange3 = _ref6$onChange === void 0 ? function () {} : _ref6$onChange,
          spritemap = _ref6.spritemap;

      var _useState5 = (0, _react.useState)(''),
          _useState6 = _slicedToArray(_useState5, 2),
          value = _useState6[0],
          setValue = _useState6[1];

      void 0;
      return (/*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, label && /*#__PURE__*/_react.default.createElement(_Group.default, {
          header: label
        }), items && /*#__PURE__*/_react.default.createElement("li", {
          "aria-label": label,
          role: "radiogroup"
        }, /*#__PURE__*/_react.default.createElement(RadioGroupContext.Provider, {
          value: {
            checked: value,
            name: name,
            onChange: function onChange(value) {
              _onChange3(value);

              setValue(value);
            }
          }
        }, /*#__PURE__*/_react.default.createElement(DropDownContent, {
          items: items,
          spritemap: spritemap
        }))))
      );
    };

    var DividerWithItem = function DividerWithItem() {
      return (/*#__PURE__*/_react.default.createElement(_Divider.default, null)
      );
    };

    var TYPE_MAP = {
      checkbox: Checkbox,
      contextual: Contextual,
      divider: DividerWithItem,
      group: Group,
      item: Item,
      radio: Radio,
      radiogroup: RadioGroup
    };

    var DropDownContent = function DropDownContent(_ref7) {
      var items = _ref7.items,
          spritemap = _ref7.spritemap;
      return (/*#__PURE__*/_react.default.createElement(_DropDown.default.ItemList, null, items.map(function (_ref8, key) {
          var type = _ref8.type,
              item = _objectWithoutProperties(_ref8, _excluded5);

          var Item = TYPE_MAP[type || 'item'];
          return (/*#__PURE__*/_react.default.createElement(Item, _extends({}, item, {
              key: key,
              spritemap: spritemap
            }))
          );
        }))
      );
    };

    var ClayDropDownWithItems = function ClayDropDownWithItems(_ref9) {
      var active = _ref9.active,
          alignmentByViewport = _ref9.alignmentByViewport,
          alignmentPosition = _ref9.alignmentPosition,
          caption = _ref9.caption,
          className = _ref9.className,
          closeOnClickOutside = _ref9.closeOnClickOutside,
          containerElement = _ref9.containerElement,
          _ref9$defaultActive = _ref9.defaultActive,
          defaultActive = _ref9$defaultActive === void 0 ? false : _ref9$defaultActive,
          footerContent = _ref9.footerContent,
          helpText = _ref9.helpText,
          items = _ref9.items,
          menuElementAttrs = _ref9.menuElementAttrs,
          menuHeight = _ref9.menuHeight,
          menuWidth = _ref9.menuWidth,
          offsetFn = _ref9.offsetFn,
          onActiveChange = _ref9.onActiveChange,
          _ref9$onSearchValueCh = _ref9.onSearchValueChange,
          onSearchValueChange = _ref9$onSearchValueCh === void 0 ? function () {} : _ref9$onSearchValueCh,
          renderMenuOnClick = _ref9.renderMenuOnClick,
          searchable = _ref9.searchable,
          searchProps = _ref9.searchProps,
          _ref9$searchValue = _ref9.searchValue,
          searchValue = _ref9$searchValue === void 0 ? '' : _ref9$searchValue,
          spritemap = _ref9.spritemap,
          trigger = _ref9.trigger;

      var _useInternalState = (0, _shared.useInternalState)({
        defaultName: 'defaultActive',
        defaultValue: defaultActive,
        handleName: 'onActiveChange',
        name: 'active',
        onChange: onActiveChange,
        value: active
      }),
          _useInternalState2 = _slicedToArray(_useInternalState, 2),
          internalActive = _useInternalState2[0],
          setInternalActive = _useInternalState2[1];

      var hasRightSymbols = _react.default.useMemo(function () {
        return !!findNested(items, 'symbolRight');
      }, [items]);

      var hasLeftSymbols = _react.default.useMemo(function () {
        return !!findNested(items, 'symbolLeft');
      }, [items]);

      var Wrap = footerContent ? 'form' : _react.default.Fragment;
      return (/*#__PURE__*/_react.default.createElement(_DropDown.default, {
          active: internalActive,
          alignmentByViewport: alignmentByViewport,
          alignmentPosition: alignmentPosition,
          className: className,
          closeOnClickOutside: closeOnClickOutside,
          containerElement: containerElement,
          hasLeftSymbols: hasLeftSymbols,
          hasRightSymbols: hasRightSymbols,
          menuElementAttrs: menuElementAttrs,
          menuHeight: menuHeight,
          menuWidth: menuWidth,
          offsetFn: offsetFn,
          onActiveChange: setInternalActive,
          renderMenuOnClick: renderMenuOnClick,
          trigger: trigger
        }, /*#__PURE__*/_react.default.createElement(ClayDropDownContext.Provider, {
          value: {
            close: function close() {
              return setInternalActive(false);
            }
          }
        }, helpText && /*#__PURE__*/_react.default.createElement(_Help.default, null, helpText), searchable && /*#__PURE__*/_react.default.createElement(_Search.default, _extends({}, searchProps, {
          onChange: function onChange(event) {
            return onSearchValueChange(event.target.value);
          },
          spritemap: spritemap,
          value: searchValue
        })), /*#__PURE__*/_react.default.createElement(Wrap, null, footerContent ? /*#__PURE__*/_react.default.createElement("div", {
          className: "inline-scroller"
        }, /*#__PURE__*/_react.default.createElement(DropDownContent, {
          items: items,
          spritemap: spritemap
        })) : /*#__PURE__*/_react.default.createElement(DropDownContent, {
          items: items,
          spritemap: spritemap
        }), caption && /*#__PURE__*/_react.default.createElement(_Caption.default, null, caption), footerContent && /*#__PURE__*/_react.default.createElement("div", {
          className: "dropdown-section"
        }, footerContent))))
      );
    };

    exports.ClayDropDownWithItems = ClayDropDownWithItems;
    ClayDropDownWithItems.displayName = 'ClayDropDownWithItems';
  }
});
//# sourceMappingURL=DropDownWithItems.js.map