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