import { jsx as _jsx } from "react/jsx-runtime";
import { useState } from 'react';
import { useLocation, useHistory } from 'react-router-dom';
import { TableSearch as Search } from './Search';
export function SearchWithQueryParams(props) {
    const { queryParams = 'search', onChange, ...rest } = props;
    const { search, pathname } = useLocation();
    const history = useHistory();
    const params = new URLSearchParams(search);
    const initialValue = params.get(queryParams) || '';
    const [value, setValue] = useState(initialValue);
    function handleOnChange(value) {
        const { onChange } = props;
        params.set(queryParams, value);
        history.replace(`${pathname}?${params.toString()}`);
        setValue(value);
        if (typeof onChange === 'function') {
            onChange(value);
        }
    }
    return (_jsx(Search, { onChange: handleOnChange, value: value, onReset: () => {
            handleOnChange('');
        }, ...rest }));
}
