import { ExportState, FiltersMap, SelectedExportOption } from '@wix/bex-core';
import { Box, RadioGroup, Text } from '@wix/design-system';
import React from 'react';
import { observer } from 'mobx-react-lite';
import { useToolbarCollectionContext } from '../ToolbarCollectionContext';
import { RadioGroupSafe } from './RadioGroupSafe';

export interface ExportStepConfigProps<T, F extends FiltersMap> {
  state: ExportState<T, F>;
  exportToCSVMessage?: string;
}

function _ExportStepConfig<T, F extends FiltersMap>({
  state,
  exportToCSVMessage,
}: ExportStepConfigProps<T, F>) {
  const {
    selectedExportOption,
    disabledRadios,
    totalForExportMap,
    translate: t,
  } = state;

  const table = useToolbarCollectionContext();

  return (
    <Box gap="SP4" direction="vertical" paddingBottom="SP1">
      <RadioGroupSafe
        dataHook="export-group"
        onChange={(val) =>
          state.onExportTypeChange(val as SelectedExportOption)
        }
        value={selectedExportOption}
        disabledRadios={disabledRadios}
      >
        <RadioGroup.Radio value="all">
          {t('cairo.export.allItems')}
        </RadioGroup.Radio>
        <RadioGroup.Radio value="filtered">
          {t('cairo.export.filteredItems')}{' '}
          {disabledRadios.indexOf('filtered') === -1
            ? totalForExportMap.filtered != null &&
              `(${totalForExportMap.filtered})`
            : '(0)'}
        </RadioGroup.Radio>
        {table.selectionProps.value.showSelection && (
          <RadioGroup.Radio value="selected">
            {t('cairo.export.selectedItems')}{' '}
            {disabledRadios.indexOf('selected') === -1
              ? totalForExportMap.selected != null &&
                `(${totalForExportMap.selected})`
              : '(0)'}
          </RadioGroup.Radio>
        )}
      </RadioGroupSafe>

      <Text dataHook="export-csv-message" widows>
        {exportToCSVMessage || t('cairo.export.exportToCSVMessage')}
      </Text>
    </Box>
  );
}

export const ExportStepConfig = observer(_ExportStepConfig);
