"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    FormDialog: ()=>FormDialog,
    default: ()=>form_dialog_rslib_entry_
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
const schema_namespaceObject = require("@tachybase/schema");
const external_antd_namespaceObject = require("antd");
const external_react_dom_namespaceObject = require("react-dom");
const index_js_namespaceObject = require("../__builtins__/index.js");
const theme_index_js_namespaceObject = require("../../../style/theme/index.js");
const isModalTitle = (props)=>(0, schema_namespaceObject.isNum)(props) || (0, schema_namespaceObject.isStr)(props) || (0, schema_namespaceObject.isBool)(props) || /*#__PURE__*/ external_react_default().isValidElement(props);
const getModelProps = (props)=>{
    if (isModalTitle(props)) return {
        title: props
    };
    return props;
};
function FormDialog(title, id, renderer, theme) {
    if ((0, schema_namespaceObject.isFn)(id) || /*#__PURE__*/ external_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, index_js_namespaceObject.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, schema_namespaceObject.observer)(()=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_react_namespaceObject.Fragment, {
            children: (0, schema_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(theme_index_js_namespaceObject.GlobalThemeProvider, {
            theme: theme,
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.Observer, {
                children: ()=>{
                    const { token } = (0, index_js_namespaceObject.useToken)();
                    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.FormProvider, {
                            form: form,
                            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(DialogContent, {})
                        })
                    });
                }
            })
        });
    };
    document.body.appendChild(env.host);
    const formDialog = {
        forOpen: (middleware)=>{
            if ((0, schema_namespaceObject.isFn)(middleware)) env.openMiddlewares.push(middleware);
            return formDialog;
        },
        forConfirm: (middleware)=>{
            if ((0, schema_namespaceObject.isFn)(middleware)) env.confirmMiddlewares.push(middleware);
            return formDialog;
        },
        forCancel: (middleware)=>{
            if ((0, schema_namespaceObject.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, index_js_namespaceObject.loading)(modal.loadingText, ()=>(0, schema_namespaceObject.applyMiddleware)(props, env.openMiddlewares));
                    env.form = env.form || (0, schema_namespaceObject.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, schema_namespaceObject.applyMiddleware)(env.form, env.confirmMiddlewares);
                            resolve((0, schema_namespaceObject.toJS)(null === (_env_form = env.form) || void 0 === _env_form ? void 0 : _env_form.values));
                            formDialog.close();
                        }).catch(()=>{});
                    }, async ()=>{
                        await (0, index_js_namespaceObject.loading)(modal.loadingText, ()=>(0, schema_namespaceObject.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, external_react_namespaceObject.useRef)(null);
    const [footer, setFooter] = (0, external_react_namespaceObject.useState)();
    const footerRef = (0, external_react_namespaceObject.useRef)();
    const prefixCls = (0, index_js_namespaceObject.usePrefixCls)('modal');
    (0, external_react_namespaceObject.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, jsx_runtime_namespaceObject.jsx)("div", {
        ref: ref,
        style: {
            display: 'none'
        },
        children: footer && /*#__PURE__*/ (0, external_react_dom_namespaceObject.createPortal)(props.children, footer)
    });
};
FormDialog.Footer = DialogFooter;
FormDialog.Portal = (0, index_js_namespaceObject.createPortalProvider)('form-dialog');
const form_dialog_rslib_entry_ = FormDialog;
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
