'use client'
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var React = require('react');
var utils = require('@dynamic-labs/utils');
require('@dynamic-labs/iconic');
require('@dynamic-labs/wallet-connector-core');
require('react/jsx-runtime');
require('../../../context/ViewContext/ViewContext.cjs');
require('../../../shared/logger.cjs');
require('@dynamic-labs/wallet-book');
require('../../constants/colors.cjs');
require('../../constants/values.cjs');
require('@dynamic-labs/sdk-api-core');
var usePreventPageScroll = require('../../../shared/utils/hooks/usePreventPageScroll/usePreventPageScroll.cjs');
require('../../../shared/consts/index.cjs');

const useConfirmationPortal = ({ onResolve, onReject, defaultErrorMessage, }) => {
    usePreventPageScroll.usePreventPageScroll(true);
    const [show, setShow] = React.useState(true);
    const errorRef = React.useRef(null);
    const successResponseRef = React.useRef(null);
    const closeModal = React.useCallback(() => {
        setShow(false);
    }, [setShow]);
    const handleOnModalUnmount = React.useCallback(() => {
        if (successResponseRef.current) {
            return onResolve(successResponseRef.current);
        }
        const error = errorRef.current ||
            new utils.DynamicError(defaultErrorMessage || 'Something went wrong', 'defaulterrormessage_something_went_wrong');
        return onReject(error);
    }, [onResolve, onReject, defaultErrorMessage]);
    const onSuccess = (response, closePortal = true) => {
        errorRef.current = null;
        successResponseRef.current = response;
        if (closePortal) {
            closeModal();
        }
    };
    const onError = (error, closePortal = false) => {
        successResponseRef.current = null;
        errorRef.current = error;
        if (closePortal) {
            closeModal();
        }
    };
    return {
        closeModal,
        handleOnModalUnmount,
        onError,
        onSuccess,
        show,
    };
};

exports.useConfirmationPortal = useConfirmationPortal;
