"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Dialog", {
    enumerable: true,
    get: function() {
        return Dialog;
    }
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
var _async_to_generator = require("@swc/helpers/_/_async_to_generator");
var _define_property = require("@swc/helpers/_/_define_property");
var _object_spread = require("@swc/helpers/_/_object_spread");
var _object_spread_props = require("@swc/helpers/_/_object_spread_props");
var _object_without_properties = require("@swc/helpers/_/_object_without_properties");
var _sliced_to_array = require("@swc/helpers/_/_sliced_to_array");
var _ts_generator = require("@swc/helpers/_/_ts_generator");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _iconsreact = require("@nutui/icons-react");
var _button = /*#__PURE__*/ _interop_require_default._(require("../button"));
var _confirm = /*#__PURE__*/ _interop_require_default._(require("./confirm"));
var _dialogwrap = require("./dialogwrap");
var _configprovider = require("../configprovider");
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _dialogwrap.defaultDialogWrapProps), {
    confirmText: '',
    cancelText: '',
    overlay: true,
    closeOnOverlayClick: true,
    hideConfirmButton: false,
    hideCancelButton: false,
    disableConfirmButton: false,
    footerDirection: 'horizontal',
    lockScroll: true,
    closeIconPosition: 'bottom',
    closeIcon: false,
    zIndex: 1200,
    beforeCancel: function beforeCancel() {
        return true;
    },
    beforeClose: function beforeClose() {
        return true;
    }
});
var BaseDialog = function BaseDialog(props, ref) {
    var locale = (0, _configprovider.useConfig)().locale;
    var _ref = (0, _object_spread._)({}, defaultProps, props), visible = _ref.visible, footer = _ref.footer, hideConfirmButton = _ref.hideConfirmButton, hideCancelButton = _ref.hideCancelButton, lockScroll = _ref.lockScroll, disableConfirmButton = _ref.disableConfirmButton, closeOnOverlayClick = _ref.closeOnOverlayClick, confirmText = _ref.confirmText, cancelText = _ref.cancelText, closeIconPosition = _ref.closeIconPosition, closeIcon = _ref.closeIcon, footerDirection = _ref.footerDirection, onClose = _ref.onClose, onCancel = _ref.onCancel, onConfirm = _ref.onConfirm, beforeCancel = _ref.beforeCancel, beforeClose = _ref.beforeClose, restProps = (0, _object_without_properties._)(_ref, [
        "visible",
        "footer",
        "hideConfirmButton",
        "hideCancelButton",
        "lockScroll",
        "disableConfirmButton",
        "closeOnOverlayClick",
        "confirmText",
        "cancelText",
        "closeIconPosition",
        "closeIcon",
        "footerDirection",
        "onClose",
        "onCancel",
        "onConfirm",
        "beforeCancel",
        "beforeClose"
    ]);
    var classPrefix = 'nut-dialog';
    var _useState = (0, _sliced_to_array._)((0, _react.useState)(false), 2), loading = _useState[0], setLoading = _useState[1];
    var renderFooter = function renderFooter() {
        if (footer === null) return '';
        var handleCancel = function handleCancel(e) {
            e.stopPropagation();
            if (!(beforeCancel === null || beforeCancel === void 0 ? void 0 : beforeCancel())) return;
            if (!(beforeClose === null || beforeClose === void 0 ? void 0 : beforeClose())) return;
            onClose === null || onClose === void 0 ? void 0 : onClose();
            onCancel === null || onCancel === void 0 ? void 0 : onCancel();
        };
        var handleOk = /*#__PURE__*/ function() {
            var _ref = (0, _async_to_generator._)(function(e) {
                var e1;
                return (0, _ts_generator._)(this, function(_state) {
                    switch(_state.label){
                        case 0:
                            e.stopPropagation();
                            setLoading(true);
                            _state.label = 1;
                        case 1:
                            _state.trys.push([
                                1,
                                3,
                                ,
                                4
                            ]);
                            return [
                                4,
                                onConfirm === null || onConfirm === void 0 ? void 0 : onConfirm(e)
                            ];
                        case 2:
                            _state.sent();
                            setLoading(false);
                            onClose === null || onClose === void 0 ? void 0 : onClose();
                            return [
                                3,
                                4
                            ];
                        case 3:
                            e1 = _state.sent();
                            setLoading(false);
                            return [
                                3,
                                4
                            ];
                        case 4:
                            return [
                                2
                            ];
                    }
                });
            });
            return function handleOk(e) {
                return _ref.apply(this, arguments);
            };
        }();
        var btnClass = hideCancelButton || hideConfirmButton ? "".concat(classPrefix, "-footer-block") : '';
        return footer || /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, !hideCancelButton && (footerDirection === 'vertical' ? /*#__PURE__*/ _react.default.createElement("div", {
            className: "".concat(classPrefix, "-footer-cancel ").concat(btnClass),
            onClick: function onClick(e) {
                return handleCancel(e);
            }
        }, cancelText || locale.cancel) : /*#__PURE__*/ _react.default.createElement(_button.default, {
            type: "default",
            size: "large",
            className: "".concat(classPrefix, "-footer-cancel ").concat(btnClass),
            onClick: function onClick(e) {
                return handleCancel(e);
            }
        }, cancelText || locale.cancel)), !hideConfirmButton && /*#__PURE__*/ _react.default.createElement(_button.default, {
            type: "primary",
            size: "large",
            className: (0, _classnames.default)("".concat(classPrefix, "-footer-ok ").concat(btnClass), {
                disabled: disableConfirmButton
            }),
            disabled: disableConfirmButton,
            onClick: function onClick(e) {
                return handleOk(e);
            },
            loading: loading
        }, confirmText || locale.confirm));
    };
    var renderCloseIcon = function renderCloseIcon() {
        if (!closeIcon) return null;
        var handleCancel = function handleCancel() {
            if (!(beforeCancel === null || beforeCancel === void 0 ? void 0 : beforeCancel())) return;
            if (!(beforeClose === null || beforeClose === void 0 ? void 0 : beforeClose())) return;
            onClose === null || onClose === void 0 ? void 0 : onClose();
            onCancel === null || onCancel === void 0 ? void 0 : onCancel();
        };
        var _obj;
        var closeClasses = (0, _classnames.default)((_obj = {}, (0, _define_property._)(_obj, "".concat(classPrefix, "-close"), true), (0, _define_property._)(_obj, "".concat(classPrefix, "-close-").concat(closeIconPosition), true), _obj));
        var systomIcon = closeIconPosition !== 'bottom' ? /*#__PURE__*/ _react.default.createElement(_iconsreact.Close, null) : /*#__PURE__*/ _react.default.createElement(_iconsreact.Failure, null);
        return /*#__PURE__*/ _react.default.createElement("div", {
            className: closeClasses,
            onClick: handleCancel
        }, /*#__PURE__*/ _react.default.isValidElement(closeIcon) ? closeIcon : systomIcon);
    };
    return /*#__PURE__*/ _react.default.createElement("div", {
        style: {
            display: visible ? 'block' : 'none'
        }
    }, /*#__PURE__*/ _react.default.createElement(_dialogwrap.DialogWrap, (0, _object_spread_props._)((0, _object_spread._)({}, restProps), {
        visible: visible,
        lockScroll: lockScroll,
        footer: renderFooter(),
        footerDirection: footerDirection,
        close: renderCloseIcon(),
        onClose: onClose,
        onCancel: onCancel
    })));
};
var Dialog = /*#__PURE__*/ (0, _react.forwardRef)(BaseDialog);
Dialog.confirm = function(props) {
    return (0, _confirm.default)(props);
};
[
    'alert'
].forEach(function(type) {
    ;
    Dialog[type] = function(props) {
        return (0, _confirm.default)((0, _object_spread_props._)((0, _object_spread._)({}, props), {
            isNotice: false,
            noticeType: type
        }));
    };
});
Dialog.displayName = 'NutDialog';
