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__data_source_collection_CollectionManagerProvider_mjs__ from "../../data-source/collection/CollectionManagerProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_data_source_DataSourceManager_mjs__ from "../../data-source/data-source/DataSourceManager.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_data_source_DataSourceManagerProvider_mjs__ from "../../data-source/data-source/DataSourceManagerProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_data_source_DataSourceProvider_mjs__ from "../../data-source/data-source/DataSourceProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../schema-component/index.mjs";
const useCollectionManager_deprecated = (dataSourceName)=>{
    const dm = (0, __WEBPACK_EXTERNAL_MODULE__data_source_data_source_DataSourceManagerProvider_mjs__.useDataSourceManager)();
    const dataSource = (0, __WEBPACK_EXTERNAL_MODULE__data_source_data_source_DataSourceProvider_mjs__.useDataSource)();
    const cm = (0, __WEBPACK_EXTERNAL_MODULE__data_source_collection_CollectionManagerProvider_mjs__.useCollectionManager)();
    const dataSourceNameValue = dataSourceName || (null == dataSource ? void 0 : dataSource.key) || void 0;
    const [random, setRandom] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)());
    const { refresh } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useSchemaComponentContext)();
    const interfaces = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        var _dm_collectionFieldInterfaceManager;
        return null == dm ? void 0 : null === (_dm_collectionFieldInterfaceManager = dm.collectionFieldInterfaceManager) || void 0 === _dm_collectionFieldInterfaceManager ? void 0 : _dm_collectionFieldInterfaceManager.getFieldInterfaces().reduce((acc, cur)=>{
            acc[cur.name] = cur;
            return acc;
        }, {});
    }, [
        dm,
        random
    ]);
    const templates = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>null == dm ? void 0 : dm.collectionTemplateManager.getCollectionTemplates(), [
        dm,
        random
    ]);
    const getCollections = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(()=>{
        var _dm_getDataSource_collectionManager, _dm_getDataSource;
        return null == dm ? void 0 : null === (_dm_getDataSource = dm.getDataSource(dataSourceNameValue)) || void 0 === _dm_getDataSource ? void 0 : null === (_dm_getDataSource_collectionManager = _dm_getDataSource.collectionManager) || void 0 === _dm_getDataSource_collectionManager ? void 0 : _dm_getDataSource_collectionManager.getCollections().map((item)=>item.getOptions());
    }, [
        dm,
        dataSource
    ]);
    const getCm = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((dataSource)=>{
        var _dm_getDataSource;
        if (cm && !dataSource) return cm;
        return null == dm ? void 0 : null === (_dm_getDataSource = dm.getDataSource(dataSource || dataSourceNameValue)) || void 0 === _dm_getDataSource ? void 0 : _dm_getDataSource.collectionManager;
    }, [
        cm,
        dm,
        dataSourceNameValue
    ]);
    const collections = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        var _dm_getDataSource_collectionManager, _dm_getDataSource;
        return null == dm ? void 0 : null === (_dm_getDataSource = dm.getDataSource(__WEBPACK_EXTERNAL_MODULE__data_source_data_source_DataSourceManager_mjs__.DEFAULT_DATA_SOURCE_KEY)) || void 0 === _dm_getDataSource ? void 0 : null === (_dm_getDataSource_collectionManager = _dm_getDataSource.collectionManager) || void 0 === _dm_getDataSource_collectionManager ? void 0 : _dm_getDataSource_collectionManager.getCollections();
    }, [
        dm,
        random
    ]);
    const service = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(()=>null == dm ? void 0 : dm.getDataSource(__WEBPACK_EXTERNAL_MODULE__data_source_data_source_DataSourceManager_mjs__.DEFAULT_DATA_SOURCE_KEY).reload().then(()=>{
            refresh();
            setRandom((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)());
        }), [
        dm
    ]);
    const refreshCM = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(()=>null == dm ? void 0 : dm.getDataSource(__WEBPACK_EXTERNAL_MODULE__data_source_data_source_DataSourceManager_mjs__.DEFAULT_DATA_SOURCE_KEY).reload().then(()=>{
            refresh();
            setRandom((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)());
        }), [
        dm
    ]);
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    const getInheritedFields = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((name, customDataSource)=>{
        var _getCm_getCollection, _getCm;
        return (null === (_getCm = getCm(customDataSource)) || void 0 === _getCm ? void 0 : null === (_getCm_getCollection = _getCm.getCollection(name)) || void 0 === _getCm_getCollection ? void 0 : _getCm_getCollection.getInheritedFields()) || [];
    }, [
        dm,
        getCm
    ]);
    const getCollectionFields = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((name, customDataSource)=>{
        var _getCm, _collection_getAllFields;
        if (!name) return [];
        const collection = null === (_getCm = getCm(customDataSource)) || void 0 === _getCm ? void 0 : _getCm.getCollection(name);
        return (null == collection ? void 0 : null === (_collection_getAllFields = collection.getAllFields) || void 0 === _collection_getAllFields ? void 0 : _collection_getAllFields.call(collection)) || (null == collection ? void 0 : collection.getFields()) || [];
    }, [
        dm,
        getCm
    ]);
    const getCollectionField = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((name, customDataSource)=>{
        var _getCm;
        if (!name || name.split('.').length < 2) return;
        return null === (_getCm = getCm(customDataSource)) || void 0 === _getCm ? void 0 : _getCm.getCollectionField(name);
    }, [
        dm,
        getCm
    ]);
    const getInheritCollections = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((name, customDataSource)=>{
        var _getCm_getCollection, _getCm;
        if (!name) return [];
        return (null === (_getCm = getCm(customDataSource)) || void 0 === _getCm ? void 0 : null === (_getCm_getCollection = _getCm.getCollection(name)) || void 0 === _getCm_getCollection ? void 0 : _getCm_getCollection.getParentCollectionsName()) || [];
    }, [
        dm,
        getCm
    ]);
    const getChildrenCollections = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(function(name) {
        let isSupportView = arguments.length > 1 && void 0 !== arguments[1] && arguments[1], customDataSource = arguments.length > 2 ? arguments[2] : void 0;
        var _getCm;
        if (!name) return [];
        const collection = null === (_getCm = getCm(customDataSource)) || void 0 === _getCm ? void 0 : _getCm.getCollection(name);
        const res = null == collection ? void 0 : collection.getChildrenCollections(isSupportView);
        return res || [];
    }, [
        dm,
        getCm
    ]);
    const getCurrentCollectionFields = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((name, customDataSource)=>{
        var _getCm_getCollection, _getCm;
        if (!name) return [];
        return (null === (_getCm = getCm(customDataSource)) || void 0 === _getCm ? void 0 : null === (_getCm_getCollection = _getCm.getCollection(name)) || void 0 === _getCm_getCollection ? void 0 : _getCm_getCollection.getCurrentFields()) || [];
    }, [
        dm,
        getCm
    ]);
    const getCollectionFieldsOptions = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(function(collectionName) {
        let type = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : 'string', opts = arguments.length > 2 ? arguments[2] : void 0;
        var _fields_filter;
        const { association = false, cached = {}, collectionNames = [
            collectionName
        ], maxDepth = 1, allowAllTypes = false, exceptInterfaces = [], prefixFieldValue = '', usePrefix = false, dataSource: customDataSourceNameValue } = opts || {};
        if (collectionNames.length - 1 > maxDepth) return;
        if (cached[collectionName]) return __WEBPACK_EXTERNAL_MODULE_lodash__["default"].cloneDeep(cached[collectionName]);
        if ('string' == typeof type) type = [
            type
        ];
        const fields = getCollectionFields(collectionName, customDataSourceNameValue);
        const options = null == fields ? void 0 : null === (_fields_filter = fields.filter((field)=>field.interface && !exceptInterfaces.includes(field.interface) && (allowAllTypes || type.includes(field.type) || !!(association && field.target && field.target !== collectionName && Array.isArray(association)) && association.includes(field.interface)))) || void 0 === _fields_filter ? void 0 : _fields_filter.map((field)=>{
            var _field_uiSchema;
            const result = {
                value: usePrefix && prefixFieldValue ? "".concat(prefixFieldValue, ".").concat(field.name) : field.name,
                label: compile(null == field ? void 0 : null === (_field_uiSchema = field.uiSchema) || void 0 === _field_uiSchema ? void 0 : _field_uiSchema.title) || field.name,
                ...field
            };
            if (association && field.target) {
                var _result_children;
                result.children = collectionNames.includes(field.target) ? [] : getCollectionFieldsOptions(field.target, type, {
                    ...opts,
                    cached,
                    dataSource: customDataSourceNameValue,
                    collectionNames: [
                        ...collectionNames,
                        field.target
                    ],
                    prefixFieldValue: usePrefix ? prefixFieldValue ? "".concat(prefixFieldValue, ".").concat(field.name) : field.name : '',
                    usePrefix
                });
                if (!(null === (_result_children = result.children) || void 0 === _result_children ? void 0 : _result_children.length)) return null;
            }
            return result;
        }).filter(Boolean);
        cached[collectionName] = options;
        return options;
    }, [
        getCollectionFields
    ]);
    const getCollection = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((name, customDataSource)=>{
        var _getCm;
        return null === (_getCm = getCm(customDataSource)) || void 0 === _getCm ? void 0 : _getCm.getCollection(name);
    }, [
        dm,
        getCm
    ]);
    const getAllCollectionsInheritChain = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((collectionName, customDataSource)=>{
        var _getCm_getCollection, _getCm;
        return null === (_getCm = getCm(customDataSource)) || void 0 === _getCm ? void 0 : null === (_getCm_getCollection = _getCm.getCollection(collectionName)) || void 0 === _getCm_getCollection ? void 0 : _getCm_getCollection.getAllCollectionsInheritChain();
    }, [
        dm,
        getCm
    ]);
    const getInheritCollectionsChain = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((collectionName, customDataSource)=>()=>{
            var _getCm_getCollection, _getCm;
            return null === (_getCm = getCm(customDataSource)) || void 0 === _getCm ? void 0 : null === (_getCm_getCollection = _getCm.getCollection(collectionName)) || void 0 === _getCm_getCollection ? void 0 : _getCm_getCollection.getInheritCollectionsChain();
        }, [
        dm,
        getCm
    ]);
    const getInterface = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((name)=>null == dm ? void 0 : dm.collectionFieldInterfaceManager.getFieldInterface(name), [
        dm
    ]);
    const isTitleField = (field)=>{
        var _getInterface;
        return !field.isForeignKey && (null === (_getInterface = getInterface(field.interface)) || void 0 === _getInterface ? void 0 : _getInterface.titleUsable);
    };
    const getParentCollectionFields = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((parentCollection, currentCollection, customDataSource)=>{
        var _getCm_getCollection, _getCm;
        return null === (_getCm = getCm(customDataSource)) || void 0 === _getCm ? void 0 : null === (_getCm_getCollection = _getCm.getCollection(currentCollection)) || void 0 === _getCm_getCollection ? void 0 : _getCm_getCollection.getParentCollectionFields(parentCollection);
    }, [
        dm,
        getCm
    ]);
    const getTemplate = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(function() {
        let name = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : 'general';
        return null == dm ? void 0 : dm.collectionTemplateManager.getCollectionTemplate(name);
    }, [
        dm
    ]);
    return {
        service,
        interfaces,
        collections,
        templates,
        getTemplate,
        getInterface,
        getCollections,
        getParentCollectionFields,
        getInheritCollections,
        getChildrenCollections,
        refreshCM,
        get: getCollection,
        getInheritedFields,
        getCollectionField,
        getCollectionFields,
        getCollectionFieldsOptions,
        getCurrentCollectionFields,
        getCollection,
        getCollectionJoinField: getCollectionField,
        getAllCollectionsInheritChain,
        getInheritCollectionsChain,
        isTitleField
    };
};
export { useCollectionManager_deprecated };
