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__api_client_index_mjs__ from "../api-client/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__application_Plugin_mjs__ from "../application/Plugin.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__BlockTemplate_mjs__ from "./BlockTemplate.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__useSchemaTemplateManager_mjs__ from "./useSchemaTemplateManager.mjs";
const SchemaTemplateManagerContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)({});
SchemaTemplateManagerContext.displayName = 'SchemaTemplateManagerContext';
const SchemaTemplateManagerProvider = (props)=>{
    const { templates, refresh } = props;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SchemaTemplateManagerContext.Provider, {
        value: {
            templates,
            refresh
        },
        children: props.children
    });
};
const regenerateUid = (s)=>{
    s['name'] = s['x-uid'] = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)();
    Object.keys(s.properties || {}).forEach((key)=>{
        regenerateUid(s.properties[key]);
    });
};
const useSchemaTemplate = ()=>{
    const { getTemplateBySchema } = (0, __WEBPACK_EXTERNAL_MODULE__useSchemaTemplateManager_mjs__.useSchemaTemplateManager)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const schemaId = fieldSchema['x-uid'];
    const templateKey = fieldSchema['x-template-key'];
    const result = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>getTemplateBySchema(fieldSchema), [
        schemaId,
        templateKey
    ]);
    return result;
};
const InternalRemoteSchemaTemplateManagerProvider = (props)=>{
    var _service_data;
    const api = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
    const options = {
        resource: 'uiSchemaTemplates',
        action: 'list',
        params: {
            paginate: false
        }
    };
    const service = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useRequest)(options);
    if (service.loading && !service.data) return;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SchemaTemplateManagerProvider, {
        refresh: async ()=>{
            const { data } = await api.request(options);
            service.mutate(data);
        },
        templates: null == service ? void 0 : null === (_service_data = service.data) || void 0 === _service_data ? void 0 : _service_data.data,
        children: props.children
    });
};
const RemoteSchemaTemplateManagerProvider = (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(InternalRemoteSchemaTemplateManagerProvider, {
        children: props.children
    });
class RemoteSchemaTemplateManagerPlugin extends __WEBPACK_EXTERNAL_MODULE__application_Plugin_mjs__.Plugin {
    async load() {
        this.addComponents();
        this.app.addProviders([
            RemoteSchemaTemplateManagerProvider
        ]);
    }
    addComponents() {
        this.app.addComponents({
            BlockTemplate: __WEBPACK_EXTERNAL_MODULE__BlockTemplate_mjs__.BlockTemplate
        });
    }
}
export { RemoteSchemaTemplateManagerPlugin, RemoteSchemaTemplateManagerProvider, SchemaTemplateManagerContext, SchemaTemplateManagerProvider, regenerateUid, useSchemaTemplate };
