import { jsx as _jsx } from "react/jsx-runtime";
import { createContext, useContext } from 'react';
const ApiContext = createContext(undefined);
export const ApiProvider = ({ api, children }) => {
    return _jsx(ApiContext.Provider, { value: api, children: children });
};
export const useApi = () => {
    const context = useContext(ApiContext);
    if (context === undefined) {
        throw new Error('useApi must be used within an ApiProvider');
    }
    return context;
};
export const useCart = () => {
    const api = useApi();
    const { extensionPoint } = api.useExtension();
    const useCart = api.useCart;
    if (useCart) {
        return useCart();
    }
    throw new ExtensionPointException({
        extensionPoint,
        hookName: 'useCart',
    });
};
export const useExtension = () => {
    const api = useApi();
    return api.useExtension();
};
export const useSettings = () => {
    const api = useApi();
    return api.useSettings();
};
export const defineExtensions = (extensions) => {
    const _extensions = extensions;
    const hoc = (Component) => (props) => (_jsx(ApiProvider, { ...props, children: _jsx(Component, {}) }));
    return Object.keys(extensions).reduce((components, key) => {
        const Component = _extensions[key];
        if (Component !== undefined) {
            return {
                ...components,
                [key]: hoc(Component),
            };
        }
        return components;
    }, {});
};
class ExtensionPointException extends Error {
    constructor({ extensionPoint, hookName }) {
        const message = `The extension point ${extensionPoint} has no access to the hook ${hookName}`;
        super(message);
        this.name = 'ExtensionPointException';
    }
}
//# sourceMappingURL=api.js.map