import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__ from "../api-client/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__ from "../collection-manager/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__ from "../data-source/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__SchemaTemplateManagerProvider_mjs__ from "./SchemaTemplateManagerProvider.mjs";
const useSchemaTemplateManager = ()=>{
    const { getInheritCollections } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    const { refresh, templates = [] } = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(__WEBPACK_EXTERNAL_MODULE__SchemaTemplateManagerProvider_mjs__.SchemaTemplateManagerContext);
    const api = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
    return {
        templates,
        refresh,
        async getTemplateSchemaByMode (options) {
            const { mode, template } = options;
            if ('copy' === mode) {
                const { data } = await api.request({
                    url: "/uiSchemas:getJsonSchema/".concat(template.uid, "?includeAsyncNode=true")
                });
                const s = (null == data ? void 0 : data.data) || {};
                (0, __WEBPACK_EXTERNAL_MODULE__SchemaTemplateManagerProvider_mjs__.regenerateUid)(s);
                return (0, __WEBPACK_EXTERNAL_MODULE_lodash__.cloneDeep)(s);
            }
            if ('reference' === mode) return {
                type: 'void',
                'x-component': 'BlockTemplate',
                'x-component-props': {
                    templateId: template.key
                }
            };
        },
        async copyTemplateSchema (template) {
            const { data } = await api.request({
                url: "/uiSchemas:getJsonSchema/".concat(template.uid, "?includeAsyncNode=true")
            });
            const s = (null == data ? void 0 : data.data) || {};
            (0, __WEBPACK_EXTERNAL_MODULE__SchemaTemplateManagerProvider_mjs__.regenerateUid)(s);
            return (0, __WEBPACK_EXTERNAL_MODULE_lodash__.cloneDeep)(s);
        },
        async saveAsTemplate (values) {
            const { uid: schemaId } = values;
            const key = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)();
            await api.resource('uiSchemas').saveAsTemplate({
                filterByTk: schemaId,
                values: {
                    key,
                    ...values
                }
            });
            await refresh();
            return {
                key
            };
        },
        getTemplateBySchema: (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((schema)=>{
            if (!schema) return;
            const templateKey = schema['x-template-key'];
            if (templateKey) return null == templates ? void 0 : templates.find((template)=>template.key === templateKey);
            const schemaId = schema['x-uid'];
            if (schemaId) return null == templates ? void 0 : templates.find((template)=>template.uid === schemaId);
        }, [
            templates
        ]),
        getTemplateBySchemaId (schemaId) {
            if (!schemaId) return null;
            return null == templates ? void 0 : templates.find((template)=>template.uid === schemaId);
        },
        getTemplateById (key) {
            return null == templates ? void 0 : templates.find((template)=>template.key === key);
        },
        getTemplatesByCollection (dataSource, collectionName) {
            var _templates_filter;
            const parentCollections = getInheritCollections(collectionName, dataSource);
            const totalCollections = parentCollections.concat([
                collectionName
            ]);
            const items = null == templates ? void 0 : null === (_templates_filter = templates.filter) || void 0 === _templates_filter ? void 0 : _templates_filter.call(templates, (template)=>(template.dataSourceKey || __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__.DEFAULT_DATA_SOURCE_KEY) === dataSource && totalCollections.includes(template.collectionName));
            return items || [];
        },
        getTemplatesByComponentName (componentName) {
            var _templates_filter;
            const items = null == templates ? void 0 : null === (_templates_filter = templates.filter) || void 0 === _templates_filter ? void 0 : _templates_filter.call(templates, (template)=>template.componentName === componentName);
            return items || [];
        }
    };
};
export { useSchemaTemplateManager };
