"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importStar(require("react"));
const react_dom_1 = tslib_1.__importDefault(require("react-dom"));
const core_1 = require("@material-ui/core");
const WxDialog_1 = tslib_1.__importDefault(require("../WxDialog"));
const theme_1 = require("../theme");
const constants_1 = require("../constants");
/**
 * 弹出确认提示框
 * @param options.title 标题
 * @param options.message 展示的通知文本或者元素
 * @param options.onSucess 点击确定按钮的回调，支持Promise
 * @param options.onCancel 点击取消按钮的回调
 */
function wxConfirm(options) {
    const root = document.querySelector('#root');
    const wxconfirm = document.createElement('div');
    root.appendChild(wxconfirm);
    const el = (react_1.default.createElement(WxConfirm, { options: options, onClose: () => {
            root.removeChild(wxconfirm);
        } }));
    react_dom_1.default.render(el, wxconfirm);
}
exports.default = wxConfirm;
function WxConfirm({ onClose, options }) {
    var _a;
    const theme = ((_a = localStorage.getItem(constants_1.THEME_KEY)) === null || _a === void 0 ? void 0 : _a.replace(/"/g, '')) || constants_1.THEME.LIGHT;
    const [loading, setLoading] = react_1.useState(false);
    const [open, setOpen] = react_1.useState(true);
    const myTheme = theme_1.createTheme(theme);
    const onConfirm = react_1.useCallback(async () => {
        const type = options.onConfirm();
        if (type instanceof Promise) {
            setLoading(true);
            const ret = await type;
            setLoading(false);
            !!ret && setOpen(false);
        }
        else {
            setOpen(false);
        }
    }, []);
    return (react_1.default.createElement(core_1.ThemeProvider, { theme: myTheme },
        react_1.default.createElement(WxDialog_1.default, { title: options.title, open: open, onClose: () => !loading && setOpen(false), onExited: onClose, actions: react_1.default.createElement(react_1.default.Fragment, null,
                react_1.default.createElement(core_1.Box, { color: myTheme.palette.text.hint },
                    react_1.default.createElement(core_1.Button, { disabled: loading, onClick: () => {
                            var _a;
                            (_a = options.onCancel) === null || _a === void 0 ? void 0 : _a.call(options);
                            setOpen(false);
                        }, color: "inherit" }, "\u53D6\u6D88")),
                react_1.default.createElement(core_1.Box, { color: myTheme.palette.primary.main },
                    react_1.default.createElement(core_1.Button, { onClick: onConfirm, disabled: loading, color: "inherit" }, loading ? react_1.default.createElement(core_1.CircularProgress, { color: "primary", size: 18 }) : '确定'))) }, typeof options.message === 'string' ? (react_1.default.createElement(core_1.DialogContentText, null, options.message)) : (options.message))));
}
