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