var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { jsx as _jsx } from "react/jsx-runtime";
import { useCallback, useContext, memo } from 'react';
import usePrompt from './usePrompt';
import { useBlocker } from './useBlocker';
import ConfirmContextProvider, { ConfirmContext } from './ConfirmContext';
const ReactRouterPrompt = ({ when, children, }) => {
    const { onConfirm, resetConfirmation, isActive, proceed, cancel } = usePrompt();
    const { resolve } = useContext(ConfirmContext) || {};
    const blocker = useCallback(
    // @ts-ignore
    (tx) => __awaiter(void 0, void 0, void 0, function* () {
        if (yield onConfirm()) {
            resetConfirmation();
            tx.retry();
        }
    }), [resetConfirmation, onConfirm]);
    useBlocker(blocker, when && !resolve);
    return (_jsx("div", { children: children({
            isActive,
            onConfirm: proceed,
            onCancel: cancel,
        }) }));
};
const Main = (props) => {
    return (_jsx(ConfirmContextProvider, { children: _jsx(ReactRouterPrompt, Object.assign({}, props)) }));
};
export default memo(Main);