import { omit } from 'lodash';
import { createContext, useContext, useEffect, useState } from 'react';
const emptyConnectionInterfaceConfig = {
    getConnectionInterfaces: async () => [],
    isConnectionInterfaceNameAvailable: async () => true,
};
const configContext = createContext(undefined);
const generatedConnectionContext = createContext(undefined);
export const ConnectionInterfaceConfigProvider = configContext.Provider;
export const GeneratedConnectionsProvider = generatedConnectionContext.Provider;
export function useConnectionInterfaceConfig() {
    const value = useContext(configContext) ?? emptyConnectionInterfaceConfig;
    return value;
}
function useGeneratedConnectionsObservable() {
    const observable = useContext(generatedConnectionContext);
    if (!observable)
        throw new Error('Can\'t use generated connections without a "GlobalContext" in the React tree.');
    return observable;
}
export function useGeneratedConnectionsManager() {
    const observable = useGeneratedConnectionsObservable();
    return {
        set(name, type, value) {
            observable.set({ ...observable.get(), [name]: { type, value } });
        },
        unset(name) {
            observable.set(omit(observable.get(), name));
        },
        getByType(type) {
            return Object.values(observable.get()).filter(i => i.type === type).map(i => i.value);
        },
        observable,
    };
}
export function useGeneratedConnections(type) {
    const { getByType, observable } = useGeneratedConnectionsManager();
    const [generated, setGenerated] = useState(getByType(type));
    useEffect(() => observable.onChange(() => setGenerated(getByType(type))), [type]);
    return generated;
}
//# sourceMappingURL=ConnectionInterface.js.map