"use strict";

exports.__esModule = true;
exports.TabsDropdown = void 0;
var _react = _interopRequireWildcard(require("react"));
var _designSystem = require("@wix/design-system");
var _mobxReactLite = require("mobx-react-lite");
var _ToolbarCollectionContext = require("../ToolbarCollectionContext");
var _TabsFilterState = require("./TabsFilterState");
var _TabTotal = require("../TabTotal");
var _types = require("./types");
var _SingleSelectFilterBase = require("../SingleSelectFilterBase");
var _hooks = require("../../hooks");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/TabsFilter/TabsDropdown.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 _TabsDropdown(props) {
  const {
    data: dataProp,
    items,
    filter,
    selectedId,
    popoverProps
  } = props;
  const {
    itemKey
  } = filter;
  const table = (0, _ToolbarCollectionContext.useToolbarCollectionContext)();
  const container = (0, _react.useRef)();
  const [state] = (0, _react.useState)(() => new _TabsFilterState.TabsFilterState({
    table
  }));
  (0, _react.useEffect)(() => state.init({
    container: container.current,
    itemsCount: items.length
  }), []);
  const {
    isLoading,
    isIdle
  } = table.initTask.status;
  const hideCounterBadge = isLoading || isIdle;
  const options = _react.default.useMemo(() => items.map(item => (0, _designSystem.listItemSelectBuilder)({
    id: item.id,
    title: item.name,
    label: item.name,
    suffix: hideCounterBadge ? undefined : /*#__PURE__*/_react.default.createElement(_TabTotal.TabTotalDropdownOption, {
      value: dataProp.find(data => itemKey(data) === item.id),
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 61,
        columnNumber: 13
      }
    })
  })), [items, hideCounterBadge]);
  const {
    dropdownSelect,
    init
  } = (0, _hooks.useDropdownSelectFilterState)({
    filter
  });
  (0, _react.useEffect)(() => init(), []);
  return /*#__PURE__*/_react.default.createElement(_designSystem.Box, {
    minWidth: 140,
    maxWidth: 200,
    paddingLeft: "SP4",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 77,
      columnNumber: 5
    }
  }, /*#__PURE__*/_react.default.createElement(_SingleSelectFilterBase.SingleSelectFilterBase, {
    dataHook: "tabs-dropdown",
    filter: filter,
    dropdownSelect: dropdownSelect,
    options: options,
    disabled: !options.length,
    selectedId: selectedId,
    onSelect: option => {
      const tab = items.find(item => item.id === option.id);
      if (tab) {
        const values = tab.id === _types.ALL_TAB_ITEM_ID ? [] : [tab.value];
        filter.refresh(values);
      }
    },
    suffix: hideCounterBadge ? undefined : /*#__PURE__*/_react.default.createElement(_designSystem.Input.Affix, {
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 99,
        columnNumber: 13
      }
    }, /*#__PURE__*/_react.default.createElement(_TabTotal.TabTotalDropdownOption, {
      key: selectedId,
      value: dataProp.find(value => itemKey(value) === selectedId),
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 100,
        columnNumber: 15
      }
    })),
    popoverProps: popoverProps,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 78,
      columnNumber: 7
    }
  }));
}
const TabsDropdown = exports.TabsDropdown = (0, _mobxReactLite.observer)(_TabsDropdown);
//# sourceMappingURL=TabsDropdown.js.map