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

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

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

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

export default Filter;
