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";
const Json = /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].forwardRef((param, ref)=>{
    let { value, onChange, space = 2, ...props } = param;
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const [text, setText] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)('');
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        try {
            null != value ? setText(JSON.stringify(value, null, space)) : setText(void 0);
        } catch (ex) {}
    }, [
        space,
        value
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.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 };
