import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { s as styleInject, P as PricePreview } from './index-bbdfb97a.js';
import classnames from 'classnames';
import { observer } from 'mobx-react-lite';
import { useState } from 'react';
import Dropdown from 'react-dropdown';
import RALFilter from './index-7f2e9c31.js';
import Searchbar from './index-2f37b9f2.js';
import Arrow from './index-69271b96.js';
import InfoIcon from './index-04a7a9b4.js';
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
import '@babel/runtime/helpers/defineProperty';
import 'react-dynamic-multi-render';
import '@crystaldesign/diva-core';
import '@babel/runtime/helpers/asyncToGenerator';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/regenerator';
import '@babel/runtime/helpers/classCallCheck';
import '@babel/runtime/helpers/createClass';
import '@babel/runtime/helpers/toConsumableArray';
import '@crystaldesign/diva-utils';
import 'mobx';
import '@crystaldesign/web-planner-logic';
import 'qrcode.react';
import '@babel/runtime/helpers/typeof';
import 'flat';
import 'react-switch';
import 'react-countup';
import '@babel/runtime/helpers/objectDestructuringEmpty';
import './index-4b3e6bbf.js';
import './index-9a965a68.js';
import '@crystaldesign/content-box';
import './index-e9a81262.js';

var container = "container-3iO9b";
var dropdownRoot = "Dropdown-root-kZ9N2";
var dropdownControl = "Dropdown-control-jkVK3";
var dropdownMenu = "dropdown-menu-mp8ZW";
var row = "row-TkDND";
var colum = "colum-XPmyn";
var dropOption = "dropOption-oQa-Z";
var text = "text-UGO5O";
var textWithPrice = "text-with-price-NctN6";
var wrapper = "wrapper-AqbFO";
var css_248z = ".container-3iO9b {\n  display: inline-block;\n  width: 100%;\n}\n.container-3iO9b .Dropdown-root-kZ9N2 {\n  box-shadow: var(--boxShadowHover);\n}\n\n.container-3iO9b .Dropdown-control-jkVK3 {\n  border-radius: var(--globalBorderRadius);\n  border-color: white;\n  padding: 2px 52px 3px 10px;\n  line-height: 20.8px;\n  -webkit-font-smoothing: antialiased;\n  -webkit-text-stroke: 0.1px;\n  color: var(--secondaryColor);\n}\n.container-3iO9b .dropdown-menu-mp8ZW {\n  max-height: 400px !important;\n  min-width: -moz-fit-content !important;\n  min-width: fit-content !important;\n}\n\n.row-TkDND {\n  width: 100%;\n  height: -moz-fit-content;\n  height: fit-content;\n  position: relative;\n  display: flex;\n  padding-bottom: 4px;\n  padding-top: 4px;\n  justify-content: space-between;\n  align-items: center;\n  --infoIconSize: 24px;\n}\n.colum-XPmyn {\n  width: calc(50% - 32px);\n  margin: 3px;\n}\n\n.dropOption-oQa-Z {\n  width: 100%;\n  position: relative;\n}\n\n.text-UGO5O {\n  width: -moz-fit-content;\n  width: fit-content;\n}\n.text-with-price-NctN6 {\n  padding-right: 64px;\n}\n\n.wrapper-AqbFO .Dropdown-root {\n  position: relative;\n}\n\n.wrapper-AqbFO .Dropdown-control {\n  position: relative;\n  overflow: hidden;\n  box-sizing: border-box;\n  color: #333;\n  cursor: default;\n  outline: none;\n  padding: 2px 24px 2px 10px;\n  transition: all 200ms ease;\n  background-color: #fff;\n  border-radius: var(--globalBorderRadius);\n  font-size: 14px;\n}\n\n.mobile-soo0m .Dropdown-control {\n  font-size: 12px;\n}\n\n.tablet-MzfbG .Dropdown-control {\n  font-size: 15px;\n}\n\n.wrapper-AqbFO .Dropdown-control:hover {\n  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.06);\n}\n\n.wrapper-AqbFO .Dropdown-arrow {\n  border-color: #999 transparent transparent;\n  border-style: solid;\n  border-width: 5px 5px 0;\n  content: ' ';\n  display: block;\n  height: 0;\n  margin-top: -ceil(2.5);\n  position: absolute;\n  right: 8px;\n  top: 15px;\n  width: 0;\n}\n\n.wrapper-AqbFO .is-open .Dropdown-arrow {\n  border-color: transparent transparent #999;\n  border-width: 0 5px 5px;\n}\n\n.wrapper-AqbFO .Dropdown-menu {\n  background-color: white;\n  box-sizing: border-box;\n  border: 1px solid #ddd;\n  margin-top: -1px;\n  max-height: 200px;\n  overflow-y: auto;\n  position: absolute;\n  top: 100%;\n  width: 100%;\n  z-index: 1100;\n  -webkit-overflow-scrolling: touch;\n}\n\n.wrapper-AqbFO .Dropdown-menu .Dropdown-group > .Dropdown-title {\n  padding: 8px 10px;\n  color: rgba(51, 51, 51, 1);\n  font-weight: 300;\n  text-transform: capitalize;\n}\n\n.wrapper-AqbFO .Dropdown-option {\n  box-sizing: border-box;\n  color: rgba(51, 51, 51, 0.8);\n  cursor: pointer;\n  display: block;\n  padding: 8px 10px;\n}\n\n.wrapper-AqbFO .Dropdown-option:last-child {\n  border-bottom-right-radius: 2px;\n  border-bottom-left-radius: 2px;\n}\n\n.wrapper-AqbFO .Dropdown-option:hover {\n  background-color: #f2f9fc;\n}\n\n.wrapper-AqbFO .Dropdown-option.is-selected {\n  background-color: #f2f9fc;\n  color: #333;\n}\n\n.wrapper-AqbFO .Dropdown-noresults {\n  box-sizing: border-box;\n  color: #ccc;\n  cursor: default;\n  display: block;\n  padding: 8px 10px;\n}\n";
styleInject(css_248z);

function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
var GroupDropdown = function GroupDropdown(_ref) {
  var _filteredGroups$, _filteredGroups$index, _filteredGroups$index2;
  var filteredGroups = _ref.filteredGroups,
    PaletteComponent = _ref.PaletteComponent,
    id = _ref.id,
    item = _ref.item,
    showSearchBar = _ref.showSearchBar,
    contentInnerClassName = _ref.contentInnerClassName;
  var _useState = useState(null),
    _useState2 = _slicedToArray(_useState, 2),
    actGroup = _useState2[0],
    setActGroup = _useState2[1];
  var options = [];
  var selectedItem;
  var indexOfSelGroup = -1;
  var indexOfSelGroupItem = -1;
  var indexOfActGroup = -1;
  var displayParentNameInDropDown = false;
  var actDropdownValue = '---';
  if (filteredGroups[0] && (_filteredGroups$ = filteredGroups[0]) !== null && _filteredGroups$ !== void 0 && (_filteredGroups$ = _filteredGroups$.items) !== null && _filteredGroups$ !== void 0 && _filteredGroups$[0]) {
    var _filteredGroups$2;
    if (((_filteredGroups$2 = filteredGroups[0]) === null || _filteredGroups$2 === void 0 || (_filteredGroups$2 = _filteredGroups$2.items) === null || _filteredGroups$2 === void 0 ? void 0 : _filteredGroups$2[0]).parentName) {
      displayParentNameInDropDown = true;
    }
    var itemGroups = item.currentItemsFilter ? item.groups : filteredGroups;
    var _loop = function _loop() {
      var itemGroup = itemGroups[i];
      if (!selectedItem) {
        selectedItem = itemGroup.items.find(function (item) {
          return item.sel;
        });
        if (selectedItem) {
          indexOfSelGroup = i;
        }
      }
      var firstOpv = itemGroup.items[0];
      if (displayParentNameInDropDown) {
        var group = options.find(function (option) {
          return option.key == firstOpv.parentName;
        });
        if (!group) {
          group = {
            type: 'group',
            key: firstOpv.parentName,
            name: /*#__PURE__*/jsxs("div", {
              className: dropOption,
              children: [/*#__PURE__*/jsx("div", {
                className: classnames(text, firstOpv.pricePreviewPromise ? textWithPrice : undefined),
                dangerouslySetInnerHTML: {
                  __html: firstOpv.parentName
                }
              }), firstOpv.pricePreviewPromise !== undefined && /*#__PURE__*/jsx(PricePreview, {
                asyncPricePreview: firstOpv.pricePreviewPromise
              })]
            }),
            items: []
          };
          options.push(group);
        }
        group.items.push({
          label: /*#__PURE__*/jsx("div", {
            className: dropOption,
            children: /*#__PURE__*/jsx("div", {
              className: classnames(text, firstOpv.pricePreviewPromise ? textWithPrice : undefined),
              dangerouslySetInnerHTML: {
                __html: itemGroup.name
              }
            })
          }),
          value: itemGroup.name
        });
        if (indexOfSelGroup == i) {
          indexOfSelGroupItem = group.items.length - 1;
          indexOfSelGroup = options.findIndex(function (group) {
            return group.key == firstOpv.parentName;
          });
        }
      } else {
        options.push({
          label: /*#__PURE__*/jsxs("div", {
            className: dropOption,
            children: [/*#__PURE__*/jsx("div", {
              className: classnames(text, firstOpv.pricePreviewPromise ? textWithPrice : undefined),
              dangerouslySetInnerHTML: {
                __html: itemGroup.name
              }
            }), firstOpv.pricePreviewPromise !== undefined && /*#__PURE__*/jsx(PricePreview, {
              asyncPricePreview: firstOpv.pricePreviewPromise
            })]
          }),
          value: itemGroup.name
        });
      }
    };
    for (var i = 0; i < itemGroups.length; i++) {
      _loop();
    }
    indexOfActGroup = indexOfSelGroup;
    if (actGroup) {
      indexOfActGroup = filteredGroups.findIndex(function (val) {
        return val.name == actGroup;
      });
    }
    if (item.currentItemsFilter) {
      actDropdownValue = '---';
    } else if (actGroup) {
      actDropdownValue = actGroup;
    } else {
      var _options$indexOfSelGr;
      if ((_options$indexOfSelGr = options[indexOfSelGroup]) !== null && _options$indexOfSelGr !== void 0 && _options$indexOfSelGr.label) {
        actDropdownValue = {
          label: /*#__PURE__*/jsx("div", {
            className: dropOption,
            children: /*#__PURE__*/jsx("div", {
              className: text,
              dangerouslySetInnerHTML: {
                __html: options[indexOfSelGroup].value
              }
            })
          }),
          value: options[indexOfSelGroup].value
        };
        indexOfActGroup = indexOfSelGroup;
      } else if (indexOfSelGroup >= 0) {
        var _options$indexOfSelGr2;
        var _item = (_options$indexOfSelGr2 = options[indexOfSelGroup]) === null || _options$indexOfSelGr2 === void 0 || (_options$indexOfSelGr2 = _options$indexOfSelGr2.items) === null || _options$indexOfSelGr2 === void 0 ? void 0 : _options$indexOfSelGr2[indexOfSelGroupItem];
        actDropdownValue = {
          label: /*#__PURE__*/jsx("div", {
            className: dropOption,
            children: /*#__PURE__*/jsx("div", {
              className: text,
              dangerouslySetInnerHTML: {
                __html: _item.value
              }
            })
          }),
          value: _item.value
        };
        var index = indexOfSelGroupItem;
        for (var _i = 0; _i < options.length && _i < indexOfSelGroup; _i++) {
          index = index + options[_i].items.length;
        }
        indexOfActGroup = index;
      }
    }
  }
  var onGroupSel = function onGroupSel(val) {
    var _item$deleteCurFilter;
    (_item$deleteCurFilter = item.deleteCurFilter) === null || _item$deleteCurFilter === void 0 || _item$deleteCurFilter.call(item);
    setActGroup(val.value);
  };
  var allItems = [];
  var _iterator = _createForOfIteratorHelper(filteredGroups),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var _row = _step.value;
      var _iterator2 = _createForOfIteratorHelper(_row.items),
        _step2;
      try {
        for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
          var e = _step2.value;
          allItems.push(e);
        }
      } catch (err) {
        _iterator2.e(err);
      } finally {
        _iterator2.f();
      }
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
  var uiItems = item.currentItemsFilter ? allItems : (_filteredGroups$index = filteredGroups[indexOfActGroup]) === null || _filteredGroups$index === void 0 ? void 0 : _filteredGroups$index.items;
  return /*#__PURE__*/jsxs(Fragment, {
    children: [showSearchBar && /*#__PURE__*/jsx(Searchbar, {
      doShowFilter: true,
      style: {
        margin: '6px 2px 6px 0px',
        width: 'calc(100% - 10px)',
        maxWidth: 'unset',
        border: '1px solid lightgrey'
      },
      isInScroller: false,
      item: item
    }), /*#__PURE__*/jsxs("div", {
      className: container,
      children: [/*#__PURE__*/jsxs("div", {
        className: row,
        children: [/*#__PURE__*/jsx("div", {
          className: colum,
          children: /*#__PURE__*/jsx(RALFilter, {
            id: id,
            dropdownRootClass: dropdownRoot
          })
        }), ((_filteredGroups$index2 = filteredGroups[indexOfActGroup]) === null || _filteredGroups$index2 === void 0 ? void 0 : _filteredGroups$index2.contentItemId) && /*#__PURE__*/jsx(InfoIcon, {
          item: filteredGroups[indexOfActGroup],
          style: {
            zIndex: 100,
            top: 'unset',
            left: 'unset',
            position: 'relative'
          }
        }, filteredGroups[indexOfActGroup].key), /*#__PURE__*/jsx("div", {
          className: classnames(colum, wrapper),
          children: /*#__PURE__*/jsx(Dropdown, {
            className: classnames(dropdownRoot),
            controlClassName: dropdownControl,
            options: options,
            value: actDropdownValue,
            onChange: onGroupSel,
            menuClassName: dropdownMenu,
            arrowClosed: /*#__PURE__*/jsx(Arrow, {
              open: false
            }),
            arrowOpen: /*#__PURE__*/jsx(Arrow, {
              open: true
            })
          })
        })]
      }), /*#__PURE__*/jsx("div", {
        className: contentInnerClassName,
        children: uiItems === null || uiItems === void 0 ? void 0 : uiItems.map(function (uiItem, i) {
          return /*#__PURE__*/jsx(PaletteComponent, {
            paletteId: id,
            item: uiItem,
            hidePricePreview: true,
            namePostFix: item.currentItemsFilter && uiItem.groupName ? '<br/>' + uiItem.groupName : undefined
          }, i);
        })
      })]
    })]
  });
};
var GroupDropdown$1 = observer(GroupDropdown);

export { GroupDropdown$1 as default };
