"use strict";

exports.__esModule = true;
exports._TagsFilter = exports.TagsFilter = void 0;
var _react = _interopRequireWildcard(require("react"));
var _ToolbarCollectionContext = require("../ToolbarCollectionContext");
var _react2 = require("@wix/bex-core/react");
var _MultiSelectCheckboxFilter = require("../MultiSelectCheckboxFilter");
var _useFilterCollection = require("../../hooks/useFilterCollection");
var _mobxReactLite = require("mobx-react-lite");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/TagsFilter/TagsFilter.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); }
const _TagsFilter = ({
  filter
}) => {
  var _toolbarState$tagsCol, _toolbarState$tagsCol4;
  const toolbarState = (0, _ToolbarCollectionContext.useToolbarCollectionContext)();
  const {
    translate: t
  } = (0, _react2.useWixPatternsContainer)();
  const tagsFilterCollection = (0, _useFilterCollection.useFilterCollection)(filter, {
    queryName: [(_toolbarState$tagsCol = toolbarState.tagsCollectionState) == null ? void 0 : _toolbarState$tagsCol.tagsManagementState.fqdnString, 'tags', 'filter'].join('/'),
    fetchData: async ({
      search = ''
    }) => {
      var _toolbarState$tagsCol2;
      const filteredItems = (((_toolbarState$tagsCol2 = toolbarState.tagsCollectionState) == null ? void 0 : _toolbarState$tagsCol2.tagsManagementState.items) || []).filter(tag => {
        var _tag$name;
        return (_tag$name = tag.name) == null ? void 0 : _tag$name.toLowerCase().includes(search.toLowerCase());
      });
      return {
        items: filteredItems,
        total: filteredItems.length
      };
    }
  });
  (0, _react.useEffect)(() => {
    var _toolbarState$tagsCol3;
    if ((_toolbarState$tagsCol3 = toolbarState.tagsCollectionState) != null && _toolbarState$tagsCol3.tagsManagementState.items) {
      tagsFilterCollection.refreshAllPages();
    }
  }, [(_toolbarState$tagsCol4 = toolbarState.tagsCollectionState) == null ? void 0 : _toolbarState$tagsCol4.tagsManagementState.items]);
  if (!toolbarState.tagsCollectionState) {
    return null;
  }
  return /*#__PURE__*/_react.default.createElement(_MultiSelectCheckboxFilter.MultiSelectCheckboxFilter, {
    collection: tagsFilterCollection,
    filter: filter,
    popoverProps: {
      appendTo: 'window'
    },
    placeholder: t('cairo.tags.filterPanel.dropdown.placeholder'),
    renderItem: tag => ({
      title: tag.name,
      id: tag.id
    }),
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 48,
      columnNumber: 5
    }
  });
};
exports._TagsFilter = _TagsFilter;
const TagsFilter = exports.TagsFilter = (0, _mobxReactLite.observer)(_TagsFilter);
//# sourceMappingURL=TagsFilter.js.map