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

const Filter: FC = function () {
  const [{dates, start, end}, onDatesChange] = useRangeDate();
  const [fields, {onChange, resetState}] = useFilterField(
    {
      wllbCode: '',
      startTime: '',
      endTime: '',
      userName: '',
      materialName: '',
      storageCode: '',
      departmentCode: '',
      wbs: '',
      askGoodsCode: '',
      realName: '',
      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}
        onChange={onChange}
        onDatesChange={onDatesChange}
        filterData={filterList}
        fields={fields}
        dates={dates}
        onReset={onReset}
        onFilter={onShowModal}
        isSearching={isSearching}
        sourceMap={sourceMap}
        fixedMap={fixedMap}
        testId='semi_out_stream_filter'
      />

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

export default Filter;
