"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.TabsFilterState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _bexCore = require("@wix/bex-core");
var _TabsResponsiveState = require("./TabsResponsiveState");
var _events = require("events");
var _util = require("@wix/bex-core/util");
var _mobx = require("mobx");
var _bi = require("@wix/bex-core/bi");
class TabsFilterState {
  constructor(params) {
    (0, _defineProperty2.default)(this, "table", void 0);
    (0, _defineProperty2.default)(this, "responsive", void 0);
    (0, _defineProperty2.default)(this, "events", new _events.EventEmitter());
    this.table = params.table;
    this.responsive = new _TabsResponsiveState.TabsResponsiveState({
      container: this.table.container
    });
  }
  init({
    container,
    itemsCount
  }) {
    const {
      collection
    } = this.table;
    const disposers = [(0, _util.addEventListener)(collection.emitter, 'fetch', () => {
      const ls = this.events.listeners('fetch').map(l => l());
      return () => {
        ls.forEach(l => l());
      };
    }), this.responsive.init({
      container,
      itemsCount
    })];
    return () => {
      disposers.forEach(d => d());
    };
  }
  _reportTabSelectedBiOnNextResult({
    nextTab,
    prevTab,
    items,
    hasAllTab
  }) {
    const {
      table,
      events
    } = this;
    const {
      reportBi
    } = table;
    const onFetch = () => {
      events.off('fetch', onFetch);
      return (0, _mobx.action)(() => {
        reportBi((0, _bexCore.withoutDefaults)(_bi.cairoTabSelected)({
          ...table._commonDynamicBiParams(),
          newTabIndex: nextTab.index + 1,
          newTabId: nextTab.id,
          prevTabIndex: (prevTab == null ? void 0 : prevTab.index) != null ? prevTab.index + 1 : undefined,
          prevTabId: prevTab == null ? void 0 : prevTab.id,
          numTabs: items.length,
          hasAllTab
        }));
      });
    };
    events.on('fetch', onFetch);
  }
}
exports.TabsFilterState = TabsFilterState;
//# sourceMappingURL=TabsFilterState.js.map