import { jsx } from "react/jsx-runtime";
import { createContext, useCallback, useContext, useMemo } from "react";
import { useField, useFieldSchema } from "@tachybase/schema";
import { RemoteSchemaComponent, useDesignable, useSchemaComponentContext } from "../index.mjs";
import { useSchemaTemplateManager } from "./useSchemaTemplateManager.mjs";
const BlockTemplateContext = /*#__PURE__*/ createContext({});
BlockTemplateContext.displayName = 'BlockTemplateContext';
const useBlockTemplateContext = ()=>useContext(BlockTemplateContext);
const BlockTemplate = (props)=>{
    const { templateId } = props;
    const { getTemplateById } = useSchemaTemplateManager();
    const field = useField();
    const fieldSchema = useFieldSchema();
    const { dn } = useDesignable();
    const template = useMemo(()=>getTemplateById(templateId), [
        templateId
    ]);
    const { refresh } = useSchemaComponentContext();
    const onSuccess = useCallback((data)=>{
        var _data_data, _data_data1;
        fieldSchema['x-linkage-rules'] = (null == data ? void 0 : null == (_data_data = data.data) ? void 0 : _data_data['x-linkage-rules']) || [];
        fieldSchema.setProperties(null == data ? void 0 : null == (_data_data1 = data.data) ? void 0 : _data_data1.properties);
        refresh();
    }, [
        template
    ]);
    return template ? /*#__PURE__*/ jsx(BlockTemplateContext.Provider, {
        value: {
            dn,
            field,
            fieldSchema,
            template
        },
        children: /*#__PURE__*/ jsx(RemoteSchemaComponent, {
            noForm: true,
            uid: null == template ? void 0 : template.uid,
            onSuccess: onSuccess
        })
    }) : null;
};
export { BlockTemplate, useBlockTemplateContext };
