"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.useBuildAccordionItems = useBuildAccordionItems;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = require("@wix/bex-core/react");
var _react2 = _interopRequireWildcard(require("react"));
var _designSystem = require("@wix/design-system");
var _CollectionFilter = require("../components/CollectionFilter");
var _FilterAccordionTitle = require("../components/FiltersPanel/FilterAccordionTitle");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/hooks/useBuildAccordionItems.tsx";
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function useBuildAccordionItem(state) {
  return (0, _react2.useCallback)(filterElement => {
    const {
      type: Component,
      props: filterProps
    } = filterElement;
    const {
      filter,
      initiallyOpen,
      popoverProps,
      accordionItemProps
    } = filterProps;
    const clonedElement = /*#__PURE__*/_react2.default.createElement(Component, (0, _extends2.default)({}, filterProps, {
      popoverProps: {
        appendTo: 'window',
        ...popoverProps
      },
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 22,
        columnNumber: 7
      }
    }));
    return {
      title: /*#__PURE__*/_react2.default.createElement(_FilterAccordionTitle.FilterAccordionTitle, {
        state: state.toolbar,
        filter: filter,
        filterProps: filterProps,
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 30,
          columnNumber: 9
        }
      }),
      children: filterProps.filter ? /*#__PURE__*/_react2.default.createElement(_CollectionFilter.CollectionFilter, {
        filter: filterProps.filter,
        state: state,
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 37,
          columnNumber: 9
        }
      }, clonedElement) : clonedElement,
      initiallyOpen,
      ...accordionItemProps
    };
  }, []);
}
function useBuildAccordionItems(state) {
  const {
    translate: t
  } = (0, _react.useWixPatternsContainer)();
  const buildAccordionItem = useBuildAccordionItem(state);
  const {
    panelFilterElements
  } = state;
  const defaultSectionTitle = t('cairo.filtersPanel.sections.default.title');
  const {
    groups,
    defaultGroup
  } = panelFilterElements.reduce((accumulator, item) => {
    var _item$props;
    const sectionTitle = (_item$props = item.props) == null ? void 0 : _item$props.sectionTitle;
    if (!sectionTitle) {
      return {
        ...accumulator,
        defaultGroup: [...accumulator.defaultGroup, buildAccordionItem(item)]
      };
    }
    const groupItems = accumulator.groups[sectionTitle] ? [...accumulator.groups[sectionTitle], buildAccordionItem(item)] : [(0, _designSystem.accordionSectionItemBuilder)({
      title: sectionTitle
    }), buildAccordionItem(item)];
    return {
      ...accumulator,
      groups: {
        ...accumulator.groups,
        [sectionTitle]: groupItems
      }
    };
  }, {
    groups: {},
    defaultGroup: []
  });
  const items = Object.values(groups).flat();
  if (!items.length) {
    return defaultGroup;
  }
  return [...items, ...(defaultGroup.length ? [(0, _designSystem.accordionSectionItemBuilder)({
    title: defaultSectionTitle
  }), ...defaultGroup] : [])];
}
//# sourceMappingURL=useBuildAccordionItems.js.map