import { withoutDefaults } from '@wix/bex-core';
import { TabsResponsiveState } from './TabsResponsiveState';
import { EventEmitter } from 'events';
import { addEventListener } from '@wix/bex-core/util';
import { action } from 'mobx';
import { cairoTabSelected } from '@wix/bex-core/bi';
export class TabsFilterState {
    constructor(params) {
        this.events = new EventEmitter();
        this.table = params.table;
        this.responsive = new TabsResponsiveState({
            container: this.table.container,
        });
    }
    init({ container, itemsCount, }) {
        const { collection } = this.table;
        const disposers = [
            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 action(() => {
                reportBi(withoutDefaults(cairoTabSelected)({
                    ...table._commonDynamicBiParams(),
                    newTabIndex: nextTab.index + 1,
                    newTabId: nextTab.id,
                    prevTabIndex: prevTab?.index != null ? prevTab.index + 1 : undefined,
                    prevTabId: prevTab?.id,
                    numTabs: items.length,
                    hasAllTab,
                }));
            });
        };
        events.on('fetch', onFetch);
    }
}
//# sourceMappingURL=TabsFilterState.js.map