import { jsx } from "react/jsx-runtime";
import { useMemo } from "react";
import { createForm } from "@tachybase/schema";
import { useRequest } from "../../api-client/index.mjs";
import { useSchemaComponentContext } from "../hooks/index.mjs";
import { FormProvider } from "./FormProvider.mjs";
import { SchemaComponent } from "./SchemaComponent.mjs";
const defaultTransform = (s)=>s;
const RequestSchemaComponent = (props)=>{
    const { noForm, onlyRenderProperties, hidden, scope, uid, components, onSuccess, schemaTransform = defaultTransform } = props;
    const { reset } = useSchemaComponentContext();
    const conf = {
        url: "/uiSchemas:".concat(onlyRenderProperties ? 'getProperties' : 'getJsonSchema', "/").concat(uid)
    };
    const form = useMemo(()=>createForm(), [
        uid
    ]);
    const { data, loading } = useRequest(conf, {
        refreshDeps: [
            uid
        ],
        onSuccess (data) {
            onSuccess && onSuccess(data);
            reset && reset();
        }
    });
    if (loading || hidden || !data) return;
    return noForm ? /*#__PURE__*/ jsx(SchemaComponent, {
        components: components,
        scope: scope,
        schema: schemaTransform((null == data ? void 0 : data.data) || {})
    }) : /*#__PURE__*/ jsx(FormProvider, {
        form: form,
        children: /*#__PURE__*/ jsx(SchemaComponent, {
            components: components,
            scope: scope,
            schema: schemaTransform((null == data ? void 0 : data.data) || {})
        })
    });
};
const RemoteSchemaComponent = (props)=>props.uid ? /*#__PURE__*/ jsx(RequestSchemaComponent, {
        ...props
    }) : null;
export { RemoteSchemaComponent };
