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__context_mjs__ from "../context.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__SchemaComponentOptions_mjs__ from "./SchemaComponentOptions.mjs";
function toSchema(schema) {
    if (__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.Schema.isSchemaInstance(schema)) return schema;
    if (null == schema ? void 0 : schema.name) return new __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.Schema({
        type: 'object',
        properties: {
            [schema.name]: schema
        }
    });
    return new __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.Schema(schema);
}
const useMemoizedSchema = (schema)=>(0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>toSchema(schema), []);
const RecursionSchemaComponent = (props)=>{
    const { components, scope, schema, ...others } = props;
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(__WEBPACK_EXTERNAL_MODULE__context_mjs__.SchemaComponentContext);
    const s = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>toSchema(schema), [
        schema
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__context_mjs__.SchemaComponentContext.Provider, {
        value: {
            ...ctx,
            refresh: ()=>{
                var _ctx_refresh, _props_onChange;
                null === (_ctx_refresh = ctx.refresh) || void 0 === _ctx_refresh || _ctx_refresh.call(ctx);
                null === (_props_onChange = props.onChange) || void 0 === _props_onChange || _props_onChange.call(props, s);
            }
        },
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__SchemaComponentOptions_mjs__.SchemaComponentOptions, {
            inherit: true,
            components: components,
            scope: scope,
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.RecursionField, {
                ...others,
                schema: s
            })
        })
    });
};
const MemoizedSchemaComponent = (props)=>{
    const { schema, ...others } = props;
    const s = useMemoizedSchema(schema);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(RecursionSchemaComponent, {
        ...others,
        schema: s
    });
};
const SchemaComponent = (props)=>{
    const { memoized, ...others } = props;
    if (memoized) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(MemoizedSchemaComponent, {
        ...others
    });
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(RecursionSchemaComponent, {
        ...others
    });
};
export { SchemaComponent };
