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

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

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

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

export default Filter;
