"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports._TagsFilter = exports.TagsFilter = void 0;
var _react = _interopRequireDefault(require("react"));
var _ToolbarCollectionContext = require("../ToolbarCollectionContext");
var _react2 = require("@wix/bex-core/react");
var _MultiSelectCheckboxFilter = require("../MultiSelectCheckboxFilter");
var _types = require("@wix/bex-utils/@wix/ambassador-os-tags-v1-tag/types");
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";
const _TagsFilter = ({
  filter
}) => {
  var _toolbarState$tagsCol;
  const toolbarState = (0, _ToolbarCollectionContext.useToolbarCollectionContext)();
  const {
    translate: t
  } = (0, _react2.useWixPatternsContainer)();
  const tagsFilterCollection = (0, _useFilterCollection.useFilterCollection)(filter, {
    paginationMode: 'cursor',
    queryName: [(_toolbarState$tagsCol = toolbarState.tagsCollectionState) == null ? void 0 : _toolbarState$tagsCol.tagsManagementState.fqdnString, 'tags', 'filter'].join('/'),
    fetchData: async ({
      search = '',
      cursor,
      limit
    }) => {
      var _toolbarState$tagsCol2;
      const tagsManagementState = (_toolbarState$tagsCol2 = toolbarState.tagsCollectionState) == null ? void 0 : _toolbarState$tagsCol2.tagsManagementState;
      if (!tagsManagementState) {
        return {
          items: [],
          cursor: null,
          hasNext: false
        };
      }
      const result = await tagsManagementState.tagsService.queryTags({
        fqdn: tagsManagementState.fqdnString,
        exposure: _types.TagExposure.PRIVATE,
        query: {
          cursorPaging: {
            limit,
            cursor: cursor ?? null
          },
          ...(search ? {
            filter: {
              name: {
                $contains: search
              }
            }
          } : {})
        }
      }, {}, {
        serverErrorOverride: () => ({
          message: t('cairo.tags.collectionPage.popover.loadTags.error.body')
        })
      });
      return {
        items: result.items,
        cursor: result.cursor,
        hasNext: result.hasNext
      };
    }
  });
  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: 64,
      columnNumber: 5
    }
  });
};
exports._TagsFilter = _TagsFilter;
const TagsFilter = exports.TagsFilter = (0, _mobxReactLite.observer)(_TagsFilter);
//# sourceMappingURL=TagsFilter.js.map