/**
 * amis-ui v6.14.0-beta.1
 * Copyright 2018-2025 fex
 */

import { __read, __awaiter, __generator } from 'tslib';
import React__default from 'react';
import FinalModal from './Modal.js';
import Button from './Button.js';
import Drawer from './Drawer.js';
import { localeable, themeable } from 'amis-core';
import Spinner from './Spinner.js';
import PopUp from './PopUp.js';
import { findDOMNode } from 'react-dom';

function ConfirmBox(_a) {
    var _this = this;
    var type = _a.type, size = _a.size, closeOnEsc = _a.closeOnEsc, show = _a.show, onCancel = _a.onCancel, title = _a.title, showTitle = _a.showTitle, headerClassName = _a.headerClassName, __ = _a.translate, children = _a.children, showFooter = _a.showFooter, onConfirm = _a.onConfirm, beforeConfirm = _a.beforeConfirm, popOverContainer = _a.popOverContainer, position = _a.position, resizable = _a.resizable, cx = _a.classnames, className = _a.className, bodyClassName = _a.bodyClassName, footerClassName = _a.footerClassName, mobileUI = _a.mobileUI, disabled = _a.disabled, testIdBuilder = _a.testIdBuilder, onEntered = _a.onEntered, onExited = _a.onExited;
    var _b = __read(React__default.useState(), 2), loading = _b[0], setLoading = _b[1];
    var _c = __read(React__default.useState(), 2), error = _c[0], setError = _c[1];
    var bodyRef = React__default.useRef();
    var bodyDomRef = React__default.useRef();
    var getPopOverContainer = React__default.useCallback(function () {
        var dom = bodyDomRef.current && !bodyDomRef.current.nodeType
            ? findDOMNode(bodyDomRef.current)
            : null;
        return dom === null || dom === void 0 ? void 0 : dom.parentElement;
    }, []);
    var handleConfirm = React__default.useCallback(function () { return __awaiter(_this, void 0, void 0, function () {
        var ret, _a, e_1;
        var _b, _c;
        return __generator(this, function (_d) {
            switch (_d.label) {
                case 0:
                    setError('');
                    setLoading(true);
                    _d.label = 1;
                case 1:
                    _d.trys.push([1, 7, 8, 9]);
                    if (!beforeConfirm) return [3 /*break*/, 3];
                    return [4 /*yield*/, (beforeConfirm === null || beforeConfirm === void 0 ? void 0 : beforeConfirm(bodyRef.current))];
                case 2:
                    _a = _d.sent();
                    return [3 /*break*/, 5];
                case 3: return [4 /*yield*/, ((_c = (_b = bodyRef.current) === null || _b === void 0 ? void 0 : _b.submit) === null || _c === void 0 ? void 0 : _c.call(_b))];
                case 4:
                    _a = _d.sent();
                    _d.label = 5;
                case 5:
                    ret = _a;
                    if (ret === false) {
                        return [2 /*return*/];
                    }
                    else if (typeof ret === 'string') {
                        setError(ret);
                        return [2 /*return*/];
                    }
                    return [4 /*yield*/, (onConfirm === null || onConfirm === void 0 ? void 0 : onConfirm(ret))];
                case 6:
                    _d.sent();
                    return [3 /*break*/, 9];
                case 7:
                    e_1 = _d.sent();
                    setError(e_1.message);
                    return [3 /*break*/, 9];
                case 8:
                    setLoading(false);
                    setTimeout(function () { return setError(''); }, 5000);
                    return [7 /*endfinally*/];
                case 9: return [2 /*return*/];
            }
        });
    }); }, [onConfirm, beforeConfirm]);
    React__default.useEffect(function () {
        show && setError('');
    }, [show]);
    function renderDialog() {
        return mobileUI ? (React__default.createElement(PopUp, { isShow: show, showConfirm: true, onConfirm: handleConfirm, onHide: onCancel, container: popOverContainer, onEntered: onEntered, onExited: onExited }, typeof children === 'function'
            ? children({
                bodyRef: bodyRef,
                loading: loading,
                popOverContainer: getPopOverContainer,
                onConfirm: handleConfirm
            })
            : children)) : (React__default.createElement(FinalModal, { size: size, closeOnEsc: closeOnEsc, show: show, onHide: onCancel, container: popOverContainer, className: className, onEntered: onEntered, onExited: onExited },
            showTitle !== false && title ? (React__default.createElement(FinalModal.Header, { onClose: onCancel, className: headerClassName }, title)) : null,
            React__default.createElement(FinalModal.Body, { ref: bodyDomRef, className: bodyClassName }, typeof children === 'function'
                ? children({
                    bodyRef: bodyRef,
                    loading: loading,
                    onConfirm: handleConfirm,
                    popOverContainer: getPopOverContainer
                })
                : children),
            (showFooter !== null && showFooter !== void 0 ? showFooter : true) ? (React__default.createElement(FinalModal.Footer, { className: footerClassName },
                loading || error ? (React__default.createElement("div", { className: cx('Dialog-info') },
                    React__default.createElement(Spinner, { size: "sm", key: "info", show: loading }),
                    error ? (React__default.createElement("span", { className: cx('Dialog-error') }, error)) : null)) : null,
                React__default.createElement(Button, { disabled: loading, onClick: onCancel, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('cancel') }, __('cancel')),
                React__default.createElement(Button, { disabled: loading || disabled, onClick: handleConfirm, level: "primary", testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('confirm') }, __('confirm')))) : null));
    }
    function renderDrawer() {
        return (React__default.createElement(Drawer, { size: size, closeOnEsc: closeOnEsc, show: show, onHide: onCancel, container: popOverContainer, position: position, resizable: resizable, showCloseButton: false, className: className },
            showTitle !== false && title ? (React__default.createElement("div", { className: cx('Drawer-header', headerClassName) },
                React__default.createElement("div", { className: cx('Drawer-title') }, title))) : null,
            React__default.createElement("div", { ref: bodyDomRef, className: cx('Drawer-body', bodyClassName) }, typeof children === 'function'
                ? children({
                    bodyRef: bodyRef,
                    loading: loading,
                    popOverContainer: getPopOverContainer,
                    onConfirm: handleConfirm
                })
                : children),
            (showFooter !== null && showFooter !== void 0 ? showFooter : true) ? (React__default.createElement("div", { className: cx('Drawer-footer', footerClassName) },
                loading || error ? (React__default.createElement("div", { className: cx('Drawer-info') },
                    React__default.createElement(Spinner, { size: "sm", key: "info", show: loading }),
                    error ? (React__default.createElement("span", { className: cx('Drawer-error') }, error)) : null)) : null,
                React__default.createElement(Button, { onClick: handleConfirm, level: "primary" }, __('confirm')),
                React__default.createElement(Button, { onClick: onCancel }, __('cancel')))) : null));
    }
    return type === 'drawer' ? renderDrawer() : renderDialog();
}
ConfirmBox.defaultProps = {
    type: 'dialog',
    position: 'right'
};
var ConfirmBox$1 = localeable(themeable(ConfirmBox));

export { ConfirmBox, ConfirmBox$1 as default };
