"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    default: function() {
        return _default;
    },
    normalizeConfig: function() {
        return normalizeConfig;
    }
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _object_spread = require("@swc/helpers/_/_object_spread");
var _object_spread_props = require("@swc/helpers/_/_object_spread_props");
var _react = /*#__PURE__*/ _interop_require_default._(require("react"));
var _dialog = require("./dialog");
var _types = require("./types");
var _render = require("../../utils/render");
function ConfirmDialog(props) {
    return /*#__PURE__*/ _react.default.createElement(_dialog.Dialog, props, props.content);
}
var normalizeConfig = function normalizeConfig(config) {
    config.hideCancelButton = config.isNotice;
    return config;
};
var confirm = function confirm(config, renderFunc) {
    var div = document.createElement('div');
    document.body.appendChild(div);
    var dialogConfig = (0, _object_spread_props._)((0, _object_spread._)({}, config), {
        visible: false
    });
    var render = function render(props, callback) {
        (0, _render.render)(/*#__PURE__*/ _react.default.createElement(ConfirmDialog, (0, _object_spread_props._)((0, _object_spread._)({}, props), {
            onCancel: function onCancel1() {
                return onCancel();
            }
        })), div);
        callback && callback();
    };
    var renderFunction = renderFunc || render;
    var onConfirm = function onConfirm() {
        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 destroy() {
        (0, _render.unmount)(div);
        if (div === null || div === void 0 ? void 0 : div.parentNode) {
            div.parentNode.removeChild(div);
        }
        for(var i = 0; i < _types.destroyList.length; i++){
            var fn = _types.destroyList[i];
            if (fn === close) {
                _types.destroyList.splice(i, 1);
                break;
            }
        }
    };
    var onCancel = function onCancel(confirm) {
        !confirm && config.onCancel && config.onCancel();
        dialogConfig.visible = false;
        dialogConfig.onClose = function() {
            config.onClose && config.onClose();
        };
        renderFunction(dialogConfig, function() {
            destroy();
        });
    };
    var update = function update(newConfig) {
        dialogConfig = (0, _object_spread._)({}, dialogConfig, newConfig);
        dialogConfig = normalizeConfig(dialogConfig);
        renderFunction(dialogConfig);
    };
    var close = function close() {
        dialogConfig.visible = false;
        dialogConfig.onClose = function() {
            config.onClose && config.onClose();
            destroy();
        };
        renderFunction(dialogConfig);
    };
    _types.destroyList.push(close);
    return {
        close: close,
        update: update
    };
};
var _default = confirm;
