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

const Filter: FC = function () {
  const [fields, {onChange, resetState}] = useFilterField(
    {
      orderCode: '',
      materialCode: '',
      materialName: '',
      wbs: '',
      supplierId: '',
      companyNumber: '',
    },
    true,
  );
  const {isSearching} = useContextSection(searchContext, state => state[0]);
  const [visible, {onSearch, onReset, onCloseModal, onShowModal}] =
    useTableSearchToolEvents(context, fields, {
      resetCallback: resetState,
    });
  const [filterList, set] = useTableFilterState(searchContext);

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

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

export default Filter;
