import { useEffect, useState } from 'react';
import { off, on } from '../util/misc.js';
export function usePermission(descriptor) {
    const [state, setState] = useState('not-requested');
    useEffect(() => {
        const unmount = { current: null };
        setState('requested');
        navigator.permissions
            .query(descriptor)
            .then((status) => {
            const handleChange = () => {
                setState(status.state);
            };
            setState(status.state);
            on(status, 'change', handleChange, { passive: true });
            unmount.current = () => {
                off(status, 'change', handleChange);
            };
        });
        return () => {
            if (unmount.current) {
                unmount.current();
            }
        };
    }, [descriptor.name]);
    return state;
}
