import { useCallback, useMemo } from 'react';
import { useSessionStorage } from './useSessionStorage.mjs';

const serialize = (value)=>Array.from(value);
const usePersistentFilter = ({ viewId, fieldId, defaultValue, permittedValues })=>{
    const deserialize = useCallback((value)=>{
        if (!value) {
            return new Set(defaultValue);
        }
        return new Set(value.filter((v)=>permittedValues.has(v)));
    }, [
        defaultValue,
        permittedValues
    ]);
    const [serializedValue, setSerializedValue] = useSessionStorage(`${viewId}_filter_${fieldId}`, serialize(defaultValue));
    const value = useMemo(()=>deserialize(serializedValue), [
        deserialize,
        serializedValue
    ]);
    const setValue = useCallback((newValue)=>setSerializedValue(serialize(newValue)), [
        setSerializedValue
    ]);
    return [
        value,
        setValue
    ];
};

export { usePersistentFilter };
//# sourceMappingURL=usePersistentFilter.mjs.map
