import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_react_dom__ from "react-dom";
import * as __WEBPACK_EXTERNAL_MODULE__builtins_index_mjs__ from "../__builtins__/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__style_theme_index_mjs__ from "../../../style/theme/index.mjs";
const isModalTitle = (props)=>(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.isNum)(props) || (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.isStr)(props) || (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.isBool)(props) || /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].isValidElement(props);
const getModelProps = (props)=>{
    if (isModalTitle(props)) return {
        title: props
    };
    return props;
};
function FormDialog(title, id, renderer, theme) {
    if ((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.isFn)(id) || /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].isValidElement(id)) {
        theme = renderer;
        renderer = id;
        id = 'form-dialog';
    }
    const env = {
        host: document.createElement('div'),
        form: null,
        promise: null,
        openMiddlewares: [],
        confirmMiddlewares: [],
        cancelMiddlewares: []
    };
    const root = (0, __WEBPACK_EXTERNAL_MODULE__builtins_index_mjs__.createPortalRoot)(env.host, id);
    const props = getModelProps(title);
    const modal = {
        ...props,
        afterClose: ()=>{
            var _props_afterClose;
            null == props || null === (_props_afterClose = props.afterClose) || void 0 === _props_afterClose || _props_afterClose.call(props);
            root.unmount();
        }
    };
    const DialogContent = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)(()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react__.Fragment, {
            children: (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.isFn)(renderer) ? renderer(env.form) : renderer
        }), {
        displayName: 'DialogContent'
    });
    const renderDialog = function() {
        let open = !(arguments.length > 0) || void 0 === arguments[0] || arguments[0], resolve = arguments.length > 1 ? arguments[1] : void 0, reject = arguments.length > 2 ? arguments[2] : void 0;
        const { form } = env;
        if (!form) return null;
        return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__style_theme_index_mjs__.GlobalThemeProvider, {
            theme: theme,
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.Observer, {
                children: ()=>{
                    const { token } = (0, __WEBPACK_EXTERNAL_MODULE__builtins_index_mjs__.useToken)();
                    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Modal, {
                        zIndex: token.zIndexPopupBase + 1000,
                        ...modal,
                        open: open,
                        confirmLoading: form.submitting,
                        onCancel: (e)=>{
                            var _modal_onCancel;
                            if ((null == modal ? void 0 : null === (_modal_onCancel = modal.onCancel) || void 0 === _modal_onCancel ? void 0 : _modal_onCancel.call(modal, e)) !== false) null == reject || reject();
                        },
                        onOk: async (e)=>{
                            var _modal_onOk;
                            if ((null == modal ? void 0 : null === (_modal_onOk = modal.onOk) || void 0 === _modal_onOk ? void 0 : _modal_onOk.call(modal, e)) !== false) null == resolve || resolve();
                        },
                        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.FormProvider, {
                            form: form,
                            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(DialogContent, {})
                        })
                    });
                }
            })
        });
    };
    document.body.appendChild(env.host);
    const formDialog = {
        forOpen: (middleware)=>{
            if ((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.isFn)(middleware)) env.openMiddlewares.push(middleware);
            return formDialog;
        },
        forConfirm: (middleware)=>{
            if ((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.isFn)(middleware)) env.confirmMiddlewares.push(middleware);
            return formDialog;
        },
        forCancel: (middleware)=>{
            if ((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.isFn)(middleware)) env.cancelMiddlewares.push(middleware);
            return formDialog;
        },
        open: async (props)=>{
            if (env.promise) return env.promise;
            env.promise = new Promise(async (resolve, reject)=>{
                try {
                    props = await (0, __WEBPACK_EXTERNAL_MODULE__builtins_index_mjs__.loading)(modal.loadingText, ()=>(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.applyMiddleware)(props, env.openMiddlewares));
                    env.form = env.form || (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.createForm)(props);
                } catch (e) {
                    reject(e);
                }
                root.render(()=>renderDialog(true, ()=>{
                        var _env_form;
                        null === (_env_form = env.form) || void 0 === _env_form || _env_form.submit(async ()=>{
                            var _env_form;
                            await (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.applyMiddleware)(env.form, env.confirmMiddlewares);
                            resolve((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.toJS)(null === (_env_form = env.form) || void 0 === _env_form ? void 0 : _env_form.values));
                            formDialog.close();
                        }).catch(()=>{});
                    }, async ()=>{
                        await (0, __WEBPACK_EXTERNAL_MODULE__builtins_index_mjs__.loading)(modal.loadingText, ()=>(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.applyMiddleware)(env.form, env.cancelMiddlewares));
                        formDialog.close();
                    }));
            });
            return env.promise;
        },
        close: ()=>{
            if (!env.host) return;
            root.render(()=>renderDialog(false));
        }
    };
    return formDialog;
}
const DialogFooter = (props)=>{
    const ref = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)(null);
    const [footer, setFooter] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)();
    const footerRef = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)();
    const prefixCls = (0, __WEBPACK_EXTERNAL_MODULE__builtins_index_mjs__.usePrefixCls)('modal');
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useLayoutEffect)(()=>{
        var _ref_current;
        const content = null === (_ref_current = ref.current) || void 0 === _ref_current ? void 0 : _ref_current.closest(".".concat(prefixCls, "-content"));
        if (content) {
            if (!footerRef.current) {
                footerRef.current = content.querySelector(".".concat(prefixCls, "-footer"));
                if (!footerRef.current) {
                    footerRef.current = document.createElement('div');
                    footerRef.current.classList.add("".concat(prefixCls, "-footer"));
                    content.appendChild(footerRef.current);
                }
            }
            setFooter(footerRef.current);
        }
    }, [
        prefixCls
    ]);
    footerRef.current = footer;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
        ref: ref,
        style: {
            display: 'none'
        },
        children: footer && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_dom__.createPortal)(props.children, footer)
    });
};
FormDialog.Footer = DialogFooter;
FormDialog.Portal = (0, __WEBPACK_EXTERNAL_MODULE__builtins_index_mjs__.createPortalProvider)('form-dialog');
const form_dialog_rslib_entry_ = FormDialog;
export { FormDialog, form_dialog_rslib_entry_ as default };
