import {FilterSelectorModal, FilterTool} from '@components';
import {
  useContextSection,
  useFilterDB,
  useFilterField,
  useRangeDate,
  useTableSearchToolEvents,
} from '@hooks';
import {FC} from 'react';
import {context, searchContext} from '../context';
import {fixedMap, sourceMap} from './state';

const Filter: FC = function () {
  const [{dates, start, end}, onDatesChange] = useRangeDate();
  const [fields, {onChange, resetState}] = useFilterField(
    {
      startTime: '',
      endTime: '',
      wllbCode: '',
      materialName: '',
      userName: '',
      wbs: '',
      storageCode: '',
      storageLocationName: '',
      supplierId: '',
      realName: '',
      orderCode: '',
      accountSleeve: '',
    },
    true,
  );
  const {isSearching} = useContextSection(searchContext, state => state[0]);
  const [visible, {onSearch, onShowModal, onCloseModal, onReset}] =
    useTableSearchToolEvents(
      context,
      {...fields, startTime: start, endTime: end},
      {
        resetCallback() {
          resetState();
          onDatesChange([null, null]);
        },
      },
    );
  const [filterList, {set}] = useFilterDB();

  return (
    <>
      <FilterTool
        onSearch={onSearch}
        onReset={onReset}
        onFilter={onShowModal}
        filterData={filterList}
        sourceMap={sourceMap}
        fixedMap={fixedMap}
        isSearching={isSearching}
        onChange={onChange}
        onDatesChange={onDatesChange}
        dates={dates}
        fields={fields}
        testId='raw_in_stream_filter'
      />

      <FilterSelectorModal
        visible={visible}
        onClose={onCloseModal}
        onConfirm={set}
        source={filterList}
        filtermap={sourceMap}
      />
    </>
  );
};

export default Filter;
