import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import React from "react";
import { Dialog } from "./dialog";
import { destroyList } from "./types";
import { render as reactRender, unmount } from "../../utils/render";
function ConfirmDialog(props) {
    return /*#__PURE__*/ React.createElement(Dialog, props, props.content);
}
// 如果是消息提示型弹出框，那么只有确认按钮
export var normalizeConfig = function(config) {
    config.hideCancelButton = config.isNotice;
    return config;
};
var confirm = function(config, renderFunc) {
    var div = document.createElement('div');
    document.body.appendChild(div);
    var dialogConfig = _object_spread_props(_object_spread({}, config), {
        visible: false
    });
    var render = function(props, callback) {
        reactRender(/*#__PURE__*/ React.createElement(ConfirmDialog, _object_spread_props(_object_spread({}, props), {
            onCancel: function() {
                return onCancel();
            }
        })), div);
        callback && callback();
    };
    var renderFunction = renderFunc || render;
    var onConfirm = function() {
        var _onConfirm = config.onConfirm || config.onConfirm;
        var ret = _onConfirm === null || _onConfirm === void 0 ? void 0 : _onConfirm();
        if (ret && ret.then) {
            renderFunction(dialogConfig);
            return ret.then(function() {
                onCancel(true);
            }, function(e) {
                console.error(e);
                renderFunction(dialogConfig);
            });
        }
        if (!ret) {
            onCancel(true);
        }
    };
    // 如果是promise，那么处理loading和加载完成关闭
    dialogConfig.onConfirm = onConfirm;
    dialogConfig = normalizeConfig(dialogConfig);
    dialogConfig.visible = true;
    renderFunction(dialogConfig);
    var destroy = function() {
        unmount(div);
        if (div === null || div === void 0 ? void 0 : div.parentNode) {
            div.parentNode.removeChild(div);
        }
        for(var i = 0; i < destroyList.length; i++){
            var fn = destroyList[i];
            if (fn === close) {
                destroyList.splice(i, 1);
                break;
            }
        }
    };
    var onCancel = function(confirm) {
        !confirm && config.onCancel && config.onCancel();
        dialogConfig.visible = false;
        dialogConfig.onClose = function() {
            config.onClose && config.onClose();
        };
        renderFunction(dialogConfig, function() {
            destroy();
        });
    };
    var update = function(newConfig) {
        dialogConfig = _object_spread({}, dialogConfig, newConfig);
        dialogConfig = normalizeConfig(dialogConfig);
        renderFunction(dialogConfig);
    };
    var close = function() {
        dialogConfig.visible = false;
        dialogConfig.onClose = function() {
            config.onClose && config.onClose();
            destroy();
        };
        renderFunction(dialogConfig);
    };
    destroyList.push(close);
    return {
        close: close,
        update: update
    };
};
export default confirm;
