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

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

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

export default Filter;
