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

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

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

export default Filter;
