import {
  useContextSection,
  useFilterDB,
  useFilterField,
  useRangeDate,
  useTableSearchToolEvents,
} from '@hooks';
import {FC} from 'react';
import {context, contextState, 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(
    {
      ...contextState,
    },
    true,
  );
  const {isSearching} = useContextSection(searchContext, state => state[0]);
  const [visible, {onCloseModal, onSearch, onShowModal, onReset}] =
    useTableSearchToolEvents(
      context,
      {...fields, startTime: start, endTime: end},
      {
        resetCallback() {
          onDatesChange([null, null]);
          resetState();
        },
      },
    );
  const [filterList, {set}] = useFilterDB();

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

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

export default Filter;
