import React from 'react';
const Context = React.createContext({ antom: undefined, error: undefined });
export const AntomProvider = (props) => {
    const { customError = true } = props;
    const [antomInstance, setAntomInstance] = React.useState();
    const [loadError, setLoadError] = React.useState();
    const [loading, setLoading] = React.useState(true);
    React.useEffect(() => {
        props.loadPromise
            .then((instance) => {
            setAntomInstance(instance);
        })
            .catch((e) => {
            setLoadError(e);
            // eslint-disable-next-line @typescript-eslint/ban-ts-comment
            // @ts-ignore
            window.sdkDebugger?.log('ERROR', 'SYSTEM', 'Antom script load failed', {
                message: 'Antom script load failed',
                stack: e.stack,
                source: 'SYSTEM',
            });
        })
            .finally(() => {
            setLoading(false);
        });
    }, []);
    return (React.createElement(Context.Provider, { value: { antom: antomInstance, error: loadError } }, !loading && (customError ? true : !loadError) ? props.children : null));
};
/**
 * @description 获取antom实例用于调用antom的方法，请在AntomProvider包裹的组件中使用
 */
export function useAntom() {
    return React.useContext(Context).antom;
}
/**
 * @returns 加载antom的问题，如果存在的话，只需要在顶层处理一次
 */
export function useLoadAntomError() {
    return React.useContext(Context).error;
}
