"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    useSchemaTemplateManager: ()=>useSchemaTemplateManager
});
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_lodash_namespaceObject = require("lodash");
const index_js_namespaceObject = require("../api-client/index.js");
const external_collection_manager_index_js_namespaceObject = require("../collection-manager/index.js");
const external_data_source_index_js_namespaceObject = require("../data-source/index.js");
const external_SchemaTemplateManagerProvider_js_namespaceObject = require("./SchemaTemplateManagerProvider.js");
const useSchemaTemplateManager = ()=>{
    const { getInheritCollections } = (0, external_collection_manager_index_js_namespaceObject.useCollectionManager_deprecated)();
    const { refresh, templates = [] } = (0, external_react_namespaceObject.useContext)(external_SchemaTemplateManagerProvider_js_namespaceObject.SchemaTemplateManagerContext);
    const api = (0, index_js_namespaceObject.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, external_SchemaTemplateManagerProvider_js_namespaceObject.regenerateUid)(s);
                return (0, external_lodash_namespaceObject.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, external_SchemaTemplateManagerProvider_js_namespaceObject.regenerateUid)(s);
            return (0, external_lodash_namespaceObject.cloneDeep)(s);
        },
        async saveAsTemplate (values) {
            const { uid: schemaId } = values;
            const key = (0, schema_namespaceObject.uid)();
            await api.resource('uiSchemas').saveAsTemplate({
                filterByTk: schemaId,
                values: {
                    key,
                    ...values
                }
            });
            await refresh();
            return {
                key
            };
        },
        getTemplateBySchema: (0, external_react_namespaceObject.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 || external_data_source_index_js_namespaceObject.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 || [];
        }
    };
};
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
