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__core_index_mjs__ from "../../core/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__ from "../../hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__context_mjs__ from "./context.mjs";
const DynamicComponent = (props)=>{
    const { dynamicComponent, disabled } = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(__WEBPACK_EXTERNAL_MODULE__context_mjs__.FilterContext);
    const component = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useComponent)(dynamicComponent);
    const form = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.createForm)({
            values: {
                value: props.value
            },
            disabled,
            effects () {
                (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.onFieldValueChange)('value', (field)=>{
                    var _props_onChange;
                    null == props || null === (_props_onChange = props.onChange) || void 0 === _props_onChange || _props_onChange.call(props, field.value);
                });
            }
        }), [
        JSON.stringify(props.value),
        props.schema
    ]);
    const renderSchemaComponent = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(()=>{
        var _props_schema;
        return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.FieldContext.Provider, {
            value: null,
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__core_index_mjs__.SchemaComponent, {
                schema: {
                    'x-component': 'Input',
                    ...props.schema,
                    'x-component-props': (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.merge)((null == props ? void 0 : null === (_props_schema = props.schema) || void 0 === _props_schema ? void 0 : _props_schema['x-component-props']) || {}, {
                        style: {
                            minWidth: 150
                        }
                    }),
                    name: 'value',
                    'x-read-pretty': false,
                    'x-validator': void 0,
                    'x-decorator': void 0
                }
            })
        });
    }, [
        props.schema
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.FormContext.Provider, {
        value: form,
        children: component ? /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].createElement(component, {
            value: props.value,
            collectionField: props.collectionField,
            onChange: null == props ? void 0 : props.onChange,
            renderSchemaComponent
        }) : renderSchemaComponent()
    });
};
export { DynamicComponent };
