import { ToolbarFeatureInitializer } from './toolbarFeatureInitializer';
import {
  FilterValueTotalState,
  FilterValueTotalStateParams,
} from './FilterValueTotalState';
import { TabsFilterProps } from '../components/TabsFilter';
import { FiltersMap } from '@wix/bex-core';

export const initTabTotal =
  <T, F extends FiltersMap, V>(
    props: TabsFilterProps<V>,
    params: FilterValueTotalStateParams<T, F, V>,
  ): ToolbarFeatureInitializer =>
  (state) => {
    return {
      init: () => {
        const disposers = props.data.map((value) =>
          new FilterValueTotalState({
            ...params,
            filter: props.filter,
            value,
            collection: state.collection,
          }).init(),
        );

        return () => {
          disposers.forEach((d) => d());
        };
      },
      initTask: null,
    };
  };
