"use strict";

exports.__esModule = true;
exports.useBuildGroupedOptions = useBuildGroupedOptions;
exports.useBuildOption = useBuildOption;
var _designSystem = require("@wix/design-system");
var _react = require("react");
function useBuildOption({
  filter,
  checkbox,
  renderItem
}) {
  return (0, _react.useCallback)((keyedItem, index) => {
    const {
      key,
      item
    } = keyedItem;
    const option = renderItem == null ? void 0 : renderItem(item, index);
    const title = filter.itemName(item);
    return Object.assign((0, _designSystem.listItemSelectBuilder)({
      id: key,
      checkbox,
      ellipsis: true,
      title: filter.itemName(item),
      label: title,
      ...option
    }), {
      sectionId: option == null ? void 0 : option.sectionId,
      keyedItem
    });
  }, [renderItem, checkbox]);
}
function useBuildGroupedOptions({
  items,
  renderSection
}) {
  return (0, _react.useMemo)(() => {
    if (!renderSection) {
      return items;
    }
    const {
      groups
    } = items.reduce((acc, option) => {
      const sectionId = option.sectionId || '';
      let group;
      if (acc._cache[sectionId]) {
        group = acc._cache[sectionId];
      } else {
        group = [(0, _designSystem.listItemSectionBuilder)(renderSection(sectionId))];
        acc.groups.push(group);
        acc._cache[sectionId] = group;
      }
      group.push(option);
      return acc;
    }, {
      _cache: {},
      groups: []
    });
    return groups.flatMap(e => e);
  }, [items, renderSection]);
}
//# sourceMappingURL=useBuildOption.js.map