import { useCallback, useState } from "react";
export function useMediaSelector() {
    const [mediaSelectorVisible, setMediaSelectorVisible] = useState(false);
    const [mediaSelectorMode, setMediaSelectorMode] = useState("images");
    const [selectedMediaIdPath, setSelectedMediaIdPath] = useState("");
    const [initialSearchTerm, setInitialSearchTerm] = useState(undefined);
    const [mediaResolver, setMediaResolver] = useState(undefined);
    const selectMedia = useCallback(({ selectedIdPath, mode, initialSearchTerm, }) => {
        setSelectedMediaIdPath(selectedIdPath);
        setInitialSearchTerm(initialSearchTerm);
        setMediaSelectorVisible(true);
        if (mode)
            setMediaSelectorMode(mode);
        return new Promise((resolve) => {
            setMediaResolver(() => resolve);
        });
    }, []);
    const onMediaSelect = useCallback((mediaUrl) => {
        mediaResolver?.(mediaUrl);
        setMediaSelectorVisible(false);
        setMediaResolver(undefined);
        setInitialSearchTerm(undefined);
    }, [mediaResolver]);
    const handleHide = useCallback(() => {
        setMediaSelectorVisible(false);
        setInitialSearchTerm(undefined);
        // Resolve the promise with null when dialog is closed without selection
        if (mediaResolver) {
            mediaResolver(null);
            setMediaResolver(undefined);
        }
    }, [mediaResolver]);
    return {
        mediaSelectorVisible,
        mediaSelectorMode,
        selectedMediaIdPath,
        initialSearchTerm,
        setMediaSelectorVisible,
        selectMedia,
        onMediaSelect,
        handleHide,
    };
}
//# sourceMappingURL=useMediaSelector.js.map