import React from 'react';
import { FiltersMap } from '@wix/bex-core';
import { SectionRange } from './GridSectionsState';
import { observer } from 'mobx-react-lite';
import { GridSectionsSectionState } from './GridSectionsSectionState';
import { CollectionSectionHeader } from '../CollectionSectionHeader';

export interface GridSectionHeaderProps<T, F extends FiltersMap> {
  sectionDescriptor: SectionRange;
  state: GridSectionsSectionState<T, F>;
}

function _GridSectionHeader<T, F extends FiltersMap>(
  props: GridSectionHeaderProps<T, F>,
) {
  const { sectionDescriptor, state } = props;

  const {
    gridBase: { layoutType },
  } = state;

  const { section, range } = sectionDescriptor;

  const sectionWithCount = {
    ...section,
    count: range.length,
    hasMore: state.isLast && !state.gridSections.collection.fetchedLastPage,
  };

  return (
    <CollectionSectionHeader
      dataHook="grid-section-header"
      section={sectionWithCount}
      skin={layoutType === 'list' ? 'light' : 'neutral'}
      biComponentType="GridSections"
    />
  );
}

export const GridSectionHeader = observer(_GridSectionHeader);
