import { jsx } from "react/jsx-runtime";
import react, { useEffect, useState } from "react";
import { useField } from "@tachybase/schema";
import { Input } from "antd";
const Json = /*#__PURE__*/ react.forwardRef((param, ref)=>{
    let { value, onChange, space = 2, ...props } = param;
    const field = useField();
    const [text, setText] = useState('');
    useEffect(()=>{
        try {
            null != value ? setText(JSON.stringify(value, null, space)) : setText(void 0);
        } catch (ex) {}
    }, [
        space,
        value
    ]);
    return /*#__PURE__*/ jsx(Input.TextArea, {
        ...props,
        style: {
            fontSize: '80%',
            fontFamily: "Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace",
            ...props.style
        },
        ref: ref,
        value: text,
        onChange: (ev)=>{
            setText(ev.target.value);
            try {
                '' !== ev.target.value.trim() && JSON.parse(ev.target.value);
                if ('' !== ev.target.value.trim()) JSON.parse(ev.target.value);
                field.setFeedback({});
            } catch (err) {
                field.setFeedback({
                    type: 'error',
                    code: 'JSONSyntaxError',
                    messages: [
                        err.message
                    ]
                });
            }
        },
        onBlur: (ev)=>{
            try {
                const v = '' !== ev.target.value.trim() ? JSON.parse(ev.target.value) : null;
                field.setFeedback({});
                null == onChange || onChange(v);
            } catch (err) {
                field.setFeedback({
                    type: 'error',
                    code: 'JSONSyntaxError',
                    messages: [
                        err.message
                    ]
                });
            }
        }
    });
});
Json.displayName = 'Json';
export { Json };
