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

const Filter: FC = function () {
  const [fields, {onChange, resetState}] = useFilterField(
    {
      ...contextState,
    },
    true,
  );
  const {isSearching} = useContextSection(searchContext, state => state[0]);
  const [visible, {onSearch, onReset, onShowModal, onCloseModal}] =
    useTableSearchToolEvents(context, fields, {
      resetCallback: resetState,
    });
  const [filterList, {set}] = useFilterDB();

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

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

export default Filter;
