import {
  useContextSection,
  useFilterDB,
  useFilterField,
  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 [fields, {onChange, resetState}] = useFilterField(
    {
      storageLocationName: '',
      wllbCode: '',
      materialName: '',
      companyCode: '',
      ustomerCode: '',
      wbs: '',
    },
    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
        fields={fields}
        onChange={onChange}
        filterData={filterList}
        onReset={onReset}
        onSearch={onSearch}
        isSearching={isSearching}
        sourceMap={sourceMap}
        fixedMap={fixedMap}
        onFilter={onShowModal}
        testId='dead_product_filter'
      />

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

export default Filter;
