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_classnames__ from "classnames";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__style_theme_index_mjs__ from "../../../style/theme/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_useDesignable_mjs__ from "../../hooks/useDesignable.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__Markdown_Void_Designer_mjs__ from "./Markdown.Void.Designer.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__style_mjs__ from "./style.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__util_mjs__ from "./util.mjs";
const MarkdownEditor = (props)=>{
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const [value, setValue] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(props.defaultValue);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
        className: 'mb-markdown',
        style: {
            position: 'relative'
        },
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Input.TextArea, {
                autoSize: {
                    minRows: 3
                },
                ...props,
                value: value,
                onChange: (e)=>{
                    setValue(e.target.value);
                }
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_antd__.Space, {
                style: {
                    position: 'absolute',
                    bottom: 5,
                    right: 5
                },
                children: [
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Button, {
                        onClick: (e)=>{
                            var _props_onCancel;
                            null === (_props_onCancel = props.onCancel) || void 0 === _props_onCancel || _props_onCancel.call(props, e);
                        },
                        children: t('Cancel')
                    }),
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Button, {
                        type: 'primary',
                        onClick: ()=>{
                            var _props_onSubmit;
                            null === (_props_onSubmit = props.onSubmit) || void 0 === _props_onSubmit || _props_onSubmit.call(props, value);
                        },
                        children: t('Save')
                    })
                ]
            })
        ]
    });
};
const MarkdownVoid = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    const { isDarkTheme } = (0, __WEBPACK_EXTERNAL_MODULE__style_theme_index_mjs__.useGlobalTheme)();
    const { componentCls, hashId } = (0, __WEBPACK_EXTERNAL_MODULE__style_mjs__.useStyles)({
        isDarkTheme
    });
    const { content, className } = props;
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const schema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_useDesignable_mjs__.useDesignable)();
    const { onSave, onCancel } = props;
    const { html, loading } = (0, __WEBPACK_EXTERNAL_MODULE__util_mjs__.useParseMarkdown)(content);
    if (loading) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Spin, {});
    return (null == field ? void 0 : field.editable) ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(MarkdownEditor, {
        ...props,
        className: true,
        defaultValue: content,
        onCancel: ()=>{
            field.editable = false;
            null == onCancel || onCancel();
        },
        onSubmit: async (value)=>{
            field.editable = false;
            var _schema_xcomponentprops;
            null !== (_schema_xcomponentprops = schema['x-component-props']) && void 0 !== _schema_xcomponentprops || (schema['x-component-props'] = {});
            schema['x-component-props']['content'] = value;
            field.componentProps.content = value;
            null == onSave || onSave(schema);
            dn.emit('patch', {
                schema: {
                    'x-uid': schema['x-uid'],
                    'x-component-props': {
                        content: value
                    }
                }
            });
        }
    }) : /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
        className: (0, __WEBPACK_EXTERNAL_MODULE_classnames__["default"])([
            componentCls,
            hashId,
            'tb-markdown tb-markdown-default tb-markdown-table',
            className
        ]),
        style: props.style,
        dangerouslySetInnerHTML: {
            __html: html
        }
    });
}, {
    displayName: 'MarkdownVoid'
});
MarkdownVoid.Designer = __WEBPACK_EXTERNAL_MODULE__Markdown_Void_Designer_mjs__.MarkdownVoidDesigner;
export { MarkdownVoid };
