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