import { useEffect, useMemo } from 'react';
import { useShellStore } from '../stores/shellStore';
import { UdpPagesLazy } from '../udp/pages/UdpPagesLazy';
/**
 * Resolves the widget registry from either the new `widgets` prop or the
 * deprecated `getWidget`/`isValidWidget` props and syncs it to the shell store.
 */
export function useWidgetRegistry({ widgets, getWidget, isValidWidget }) {
    const { setGetWidget, setIsValidWidget } = useShellStore();
    const mergedWidgetMap = useMemo(() => {
        if (!widgets)
            return null;
        return Object.assign(Object.assign({}, UdpPagesLazy), widgets);
    }, [widgets]);
    const resolvedGetWidget = useMemo(() => {
        if (mergedWidgetMap) {
            return (widgetType) => {
                var _a;
                return (_a = mergedWidgetMap[widgetType]) !== null && _a !== void 0 ? _a : null;
            };
        }
        return getWidget;
    }, [mergedWidgetMap, getWidget]);
    const resolvedIsValidWidget = useMemo(() => {
        if (mergedWidgetMap) {
            return (widgetType) => Object.prototype.hasOwnProperty.call(mergedWidgetMap, widgetType);
        }
        return isValidWidget;
    }, [mergedWidgetMap, isValidWidget]);
    useEffect(() => {
        setGetWidget(resolvedGetWidget);
        return () => setGetWidget(null);
    }, [resolvedGetWidget, setGetWidget]);
    useEffect(() => {
        setIsValidWidget(resolvedIsValidWidget);
        return () => setIsValidWidget(null);
    }, [resolvedIsValidWidget, setIsValidWidget]);
}
//# sourceMappingURL=useWidgetRegistry.js.map