import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import React, { forwardRef, useState } from "react";
import classNames from "classnames";
import { Failure, Close } from "@nutui/icons-react";
import Button from "../button";
import confirm from "./confirm";
import { DialogWrap, defaultDialogWrapProps } from "./dialogwrap";
import { useConfig } from "../configprovider";
var defaultProps = _object_spread_props(_object_spread({}, 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() {
        return true;
    },
    beforeClose: function() {
        return true;
    }
});
var BaseDialog = function(props, ref) {
    var locale = useConfig().locale;
    var _ref = _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 = _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 = _sliced_to_array(useState(false), 2), loading = _useState[0], setLoading = _useState[1];
    var renderFooter = function() {
        if (footer === null) return '';
        var handleCancel = function(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 = _async_to_generator(function(e) {
                var e1;
                return _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.createElement(React.Fragment, null, !hideCancelButton && (footerDirection === 'vertical' ? /*#__PURE__*/ React.createElement("div", {
            className: "".concat(classPrefix, "-footer-cancel ").concat(btnClass),
            onClick: function(e) {
                return handleCancel(e);
            }
        }, cancelText || locale.cancel) : /*#__PURE__*/ React.createElement(Button, {
            type: "default",
            size: "large",
            className: "".concat(classPrefix, "-footer-cancel ").concat(btnClass),
            onClick: function(e) {
                return handleCancel(e);
            }
        }, cancelText || locale.cancel)), !hideConfirmButton && /*#__PURE__*/ React.createElement(Button, {
            type: "primary",
            size: "large",
            className: classNames("".concat(classPrefix, "-footer-ok ").concat(btnClass), {
                disabled: disableConfirmButton
            }),
            disabled: disableConfirmButton,
            onClick: function(e) {
                return handleOk(e);
            },
            loading: loading
        }, confirmText || locale.confirm));
    };
    var renderCloseIcon = function() {
        if (!closeIcon) return null;
        var handleCancel = function() {
            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 = classNames((_obj = {}, _define_property(_obj, "".concat(classPrefix, "-close"), true), _define_property(_obj, "".concat(classPrefix, "-close-").concat(closeIconPosition), true), _obj));
        var systomIcon = closeIconPosition !== 'bottom' ? /*#__PURE__*/ React.createElement(Close, null) : /*#__PURE__*/ React.createElement(Failure, null);
        return /*#__PURE__*/ React.createElement("div", {
            className: closeClasses,
            onClick: handleCancel
        }, /*#__PURE__*/ React.isValidElement(closeIcon) ? closeIcon : systomIcon);
    };
    return /*#__PURE__*/ React.createElement("div", {
        style: {
            display: visible ? 'block' : 'none'
        }
    }, /*#__PURE__*/ React.createElement(DialogWrap, _object_spread_props(_object_spread({}, restProps), {
        visible: visible,
        lockScroll: lockScroll,
        footer: renderFooter(),
        footerDirection: footerDirection,
        close: renderCloseIcon(),
        onClose: onClose,
        onCancel: onCancel
    })));
};
export var Dialog = /*#__PURE__*/ forwardRef(BaseDialog);
Dialog.confirm = function(props) {
    return confirm(props);
};
[
    'alert'
].forEach(function(type) {
    ;
    Dialog[type] = function(props) {
        return confirm(_object_spread_props(_object_spread({}, props), {
            isNotice: false,
            noticeType: type
        }));
    };
});
Dialog.displayName = 'NutDialog';
