"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
0 && (module.exports = {
    handleClientError: null,
    handleGlobalErrors: null,
    useErrorHandler: null
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    handleClientError: function() {
        return handleClientError;
    },
    handleGlobalErrors: function() {
        return handleGlobalErrors;
    },
    useErrorHandler: function() {
        return useErrorHandler;
    }
});
const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
const _react = require("react");
const _ishydrationerror = require("../../../is-hydration-error");
const _attachhydrationerrorstate = require("./attach-hydration-error-state");
const _isnextroutererror = require("../../../is-next-router-error");
const _hydrationerrorinfo = require("./hydration-error-info");
const _console = require("../../../../lib/console");
const _iserror = /*#__PURE__*/ _interop_require_default._(require("../../../../../lib/is-error"));
const _consoleerror = require("./console-error");
const errorQueue = [];
const errorHandlers = [];
const rejectionQueue = [];
const rejectionHandlers = [];
function handleClientError(originError, consoleErrorArgs) {
    let error;
    if (!originError || !(0, _iserror.default)(originError)) {
        // If it's not an error, format the args into an error
        const formattedErrorMessage = (0, _console.formatConsoleArgs)(consoleErrorArgs);
        error = new _consoleerror.ConsoleError(formattedErrorMessage);
    } else {
        error = originError;
    }
    (0, _hydrationerrorinfo.storeHydrationErrorStateFromConsoleArgs)(...consoleErrorArgs);
    (0, _attachhydrationerrorstate.attachHydrationErrorState)(error);
    // TODO: change all to push error into errorQueue,
    // currently there's a async api error is always erroring while hydration error showing up.
    // Move hydration error to the front of the queue to unblock.
    if ((0, _ishydrationerror.isHydrationError)(error)) {
        errorQueue.unshift(error);
    } else {
        errorQueue.push(error);
    }
    for (const handler of errorHandlers){
        handler(error);
    }
}
function useErrorHandler(handleOnUnhandledError, handleOnUnhandledRejection) {
    (0, _react.useEffect)(()=>{
        // Handle queued errors.
        errorQueue.forEach(handleOnUnhandledError);
        rejectionQueue.forEach(handleOnUnhandledRejection);
        // Listen to new errors.
        errorHandlers.push(handleOnUnhandledError);
        rejectionHandlers.push(handleOnUnhandledRejection);
        return ()=>{
            // Remove listeners.
            errorHandlers.splice(errorHandlers.indexOf(handleOnUnhandledError), 1);
            rejectionHandlers.splice(rejectionHandlers.indexOf(handleOnUnhandledRejection), 1);
        };
    }, [
        handleOnUnhandledError,
        handleOnUnhandledRejection
    ]);
}
function handleGlobalErrors() {
    if (typeof window !== 'undefined') {
        try {
            // Increase the number of stack frames on the client
            Error.stackTraceLimit = 50;
        } catch (e) {}
        window.addEventListener('error', (event)=>{
            if ((0, _isnextroutererror.isNextRouterError)(event.error)) {
                event.preventDefault();
                return false;
            }
            handleClientError(event.error, []);
        });
        window.addEventListener('unhandledrejection', (ev)=>{
            const reason = ev == null ? void 0 : ev.reason;
            if ((0, _isnextroutererror.isNextRouterError)(reason)) {
                ev.preventDefault();
                return;
            }
            if (!reason || !(reason instanceof Error) || typeof reason.stack !== 'string') {
                // A non-error was thrown, we don't have anything to show. :-(
                return;
            }
            const e = reason;
            rejectionQueue.push(e);
            for (const handler of rejectionHandlers){
                handler(e);
            }
        });
    }
}

if ((typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) && typeof exports.default.__esModule === 'undefined') {
  Object.defineProperty(exports.default, '__esModule', { value: true });
  Object.assign(exports.default, exports);
  module.exports = exports.default;
}

//# sourceMappingURL=use-error-handler.js.map