"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get BaseDialog () {
        return BaseDialog;
    },
    get close () {
        return close;
    },
    get open () {
        return open;
    }
});
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 _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 _components = require("@tarojs/components");
var _iconsreacttaro = require("@nutui/icons-react-taro");
var _reacttransitiongroup = require("react-transition-group");
var _index = /*#__PURE__*/ _interop_require_default._(require("../button/index"));
var _content = require("./content");
var _configprovider = require("../configprovider/configprovider");
var _index1 = /*#__PURE__*/ _interop_require_default._(require("../overlay/index"));
var _overlay = require("../overlay/overlay");
var _usecustomevent = require("../../hooks/taro/use-custom-event");
var _uselockscoll = require("../../hooks/taro/use-lock-scoll");
var _mergeprops = require("../../utils/merge-props");
var _platform = require("../../utils/taro/platform");
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _overlay.defaultOverlayProps, _content.defaultContentProps), {
    title: '',
    content: '',
    header: '',
    footer: '',
    cancelBadge: '',
    confirmBadge: '',
    confirmText: '',
    cancelText: '',
    hideConfirmButton: false,
    hideCancelButton: false,
    disableConfirmButton: false,
    footerDirection: 'horizontal',
    closeIconPosition: 'bottom',
    closeIcon: false,
    overlay: true,
    overlayStyle: {},
    overlayClassName: 'nut-dialog-overlay',
    zIndex: 1200,
    beforeCancel: function beforeCancel() {
        return true;
    },
    beforeClose: function beforeClose() {
        return true;
    },
    onCancel: function onCancel() {},
    onClose: function onClose() {},
    onConfirm: function onConfirm() {},
    onOverlayClick: function onOverlayClick() {
        return true;
    }
});
var BaseDialog = function BaseDialog(props) {
    var _useParams = (0, _usecustomevent.useParams)((0, _mergeprops.mergeProps)(defaultProps, props)), _useParams_params = _useParams.params, id = _useParams_params.id, closeOnOverlayClick = _useParams_params.closeOnOverlayClick, confirmText = _useParams_params.confirmText, cancelText = _useParams_params.cancelText, children = _useParams_params.children, className = _useParams_params.className, closeIconPosition = _useParams_params.closeIconPosition, closeIcon = _useParams_params.closeIcon, content = _useParams_params.content, disableConfirmButton = _useParams_params.disableConfirmButton, footer = _useParams_params.footer, footerDirection = _useParams_params.footerDirection, header = _useParams_params.header, cancelBadge = _useParams_params.cancelBadge, confirmBadge = _useParams_params.confirmBadge, hideConfirmButton = _useParams_params.hideConfirmButton, hideCancelButton = _useParams_params.hideCancelButton, lockScroll = _useParams_params.lockScroll, overlay = _useParams_params.overlay, overlayStyle = _useParams_params.overlayStyle, overlayClassName = _useParams_params.overlayClassName, style = _useParams_params.style, title = _useParams_params.title, visible = _useParams_params.visible, zIndex = _useParams_params.zIndex, beforeCancel = _useParams_params.beforeCancel, beforeClose = _useParams_params.beforeClose, onClose = _useParams_params.onClose, onCancel = _useParams_params.onCancel, onConfirm = _useParams_params.onConfirm, onOverlayClick = _useParams_params.onOverlayClick, setParams = _useParams.setParams;
    var classPrefix = 'nut-dialog';
    var locale = (0, _configprovider.useConfig)().locale;
    var _useState = (0, _sliced_to_array._)((0, _react.useState)(false), 2), loading = _useState[0], setLoading = _useState[1];
    (0, _usecustomevent.useCustomEvent)(id, function(param) {
        var status = param.status, options = param.options;
        setParams((0, _object_spread_props._)((0, _object_spread._)({}, options), {
            visible: status
        }));
    });
    var refObject = (0, _uselockscoll.useLockScrollTaro)(!!(visible && lockScroll));
    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;
            onCancel();
        };
        var handleOk = function handleOk(e) {
            return (0, _async_to_generator._)(function() {
                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();
                            return [
                                3,
                                4
                            ];
                        case 3:
                            e1 = _state.sent();
                            setLoading(false);
                            return [
                                3,
                                4
                            ];
                        case 4:
                            return [
                                2
                            ];
                    }
                });
            })();
        };
        var btnClass = hideCancelButton || hideConfirmButton ? "".concat(classPrefix, "-footer-block") : '';
        var renderCancelOfVertical = function renderCancelOfVertical() {
            return !hideCancelButton && /*#__PURE__*/ _react.default.createElement(_components.View, {
                className: "".concat(classPrefix, "-footer-cancel ").concat(btnClass),
                onClick: function onClick(e) {
                    return handleCancel(e);
                }
            }, cancelText || locale.cancel);
        };
        var renderCancel = function renderCancel() {
            return !hideCancelButton && /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, cancelBadge ? /*#__PURE__*/ _react.default.createElement(_components.View, {
                className: "".concat(classPrefix, "-footer-cancel-container")
            }, /*#__PURE__*/ _react.default.createElement(_index.default, {
                type: "default",
                size: "large",
                className: "".concat(classPrefix, "-footer-cancel ").concat(btnClass),
                onClick: function onClick(e) {
                    return handleCancel(e);
                }
            }, cancelText || locale.cancel), cancelBadge ? /*#__PURE__*/ _react.default.createElement(_components.View, {
                className: "".concat(classPrefix, "-footer-cancel-badge")
            }, cancelBadge) : null) : /*#__PURE__*/ _react.default.createElement(_index.default, {
                type: "default",
                size: "large",
                className: "".concat(classPrefix, "-footer-cancel ").concat(btnClass),
                onClick: function onClick(e) {
                    return handleCancel(e);
                }
            }, cancelText || locale.cancel));
        };
        var renderConfirm = function renderConfirm() {
            return !hideConfirmButton && /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, confirmBadge ? /*#__PURE__*/ _react.default.createElement(_components.View, {
                className: "".concat(classPrefix, "-footer-ok-container")
            }, /*#__PURE__*/ _react.default.createElement(_index.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), confirmBadge ? /*#__PURE__*/ _react.default.createElement(_components.View, {
                className: "".concat(classPrefix, "-footer-ok-badge")
            }, confirmBadge) : null) : /*#__PURE__*/ _react.default.createElement(_index.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));
        };
        return footer || /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, footerDirection === 'vertical' ? /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, renderConfirm(), renderCancelOfVertical()) : /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, renderCancel(), renderConfirm()));
    };
    var renderCloseIcon = function renderCloseIcon() {
        if (!closeIcon) return null;
        var handleClose = function handleClose() {
            if (!(beforeClose === null || beforeClose === void 0 ? void 0 : beforeClose())) return;
            onClose();
        };
        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(_iconsreacttaro.Close, null) : /*#__PURE__*/ _react.default.createElement(_iconsreacttaro.Failure, null);
        return /*#__PURE__*/ _react.default.createElement(_components.View, {
            className: closeClasses,
            onClick: handleClose,
            ariaRole: "button",
            ariaLabel: locale.close,
            // @ts-ignore
            tabIndex: 0
        }, /*#__PURE__*/ _react.default.isValidElement(closeIcon) ? closeIcon : systomIcon);
    };
    var onHandleClickOverlay = function onHandleClickOverlay(e) {
        if (closeOnOverlayClick && visible && e.target === e.currentTarget) {
            var closed = onOverlayClick && onOverlayClick(e);
            closed && onCancel();
        }
    };
    var renderContent = function renderContent() {
        var contentZIndex = (0, _platform.harmony)() ? zIndex + 1 : zIndex // 解决harmony层级问题
        ;
        return /*#__PURE__*/ _react.default.createElement(_reacttransitiongroup.CSSTransition, {
            in: visible,
            timeout: 300,
            classNames: "fadeDialog",
            unmountOnExit: true,
            appear: true
        }, /*#__PURE__*/ _react.default.createElement(_content.Content, {
            className: className,
            style: (0, _object_spread._)({
                zIndex: contentZIndex
            }, style),
            title: title,
            header: header,
            close: renderCloseIcon(),
            footer: renderFooter(),
            footerDirection: footerDirection,
            visible: visible
        }, content || children));
    };
    return /*#__PURE__*/ _react.default.createElement(_components.View, {
        style: {
            display: visible ? 'block' : 'none'
        },
        ref: refObject,
        catchMove: lockScroll
    }, overlay && /*#__PURE__*/ _react.default.createElement(_index1.default, {
        zIndex: zIndex,
        visible: visible,
        style: overlayStyle,
        className: overlayClassName,
        closeOnOverlayClick: closeOnOverlayClick,
        lockScroll: lockScroll,
        onClick: onHandleClickOverlay
    }), renderContent());
};
function open(selector, options) {
    // eslint-disable-next-line react-hooks/rules-of-hooks
    var path = (0, _usecustomevent.useCustomEventsPath)(selector);
    _usecustomevent.customEvents.trigger(path, {
        status: true,
        options: options
    });
}
function close(selector) {
    // eslint-disable-next-line react-hooks/rules-of-hooks
    var path = (0, _usecustomevent.useCustomEventsPath)(selector);
    _usecustomevent.customEvents.trigger(path, {
        status: false
    });
}
BaseDialog.displayName = 'NutDialog';
BaseDialog.open = open;
BaseDialog.close = close;
