import { FiltersMap, withoutIrrelevant } from '@wix/bex-core';
import { cairoNestedTableCollapseAndExpand } from '@wix/bex-core/bi';
import { NestedTableNodeState } from './NestedTableNodeState';

const withoutDefaults = withoutIrrelevant<
  | 'currentTab'
  | 'currentView'
  | 'currentFilters'
  | 'currentSortOrder'
  | 'listSize'
  | 'filteredListSize'
  | 'initialItems'

  // bulk actions
  | 'countItems'
  | 'isSelectAll'
  | 'isFromSearch'
  | 'searchQuery'
  | 'currentStep'
>();

export class NestedTableNodeStateBIReporter<
  C extends string,
  T,
  F extends FiltersMap,
> {
  readonly state: NestedTableNodeState<C, T, F>;

  constructor(state: NestedTableNodeState<C, T, F>) {
    this.state = state;
  }

  reportExpandCollapse = () => {
    const { state } = this;
    const {
      parent,
      parentNode,
      node: { _expanded },
    } = state;
    const {
      nestedTable: { bi, toolbar },
    } = state;

    if (parent == null || parentNode == null) {
      return;
    }

    bi.reportBi(
      withoutDefaults(cairoNestedTableCollapseAndExpand)({
        ...toolbar._commonDynamicBiParams(),
        clickType: _expanded ? 'Expand' : 'Collapse',
        itemId: parent.id,
        itemIndex: state._rowNum,
        currentLevel: parentNode.childrenDepth,
        numLevels: state.nestedTable.levels.length,
      }),
    );
  };
}
