"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__, {
    CollectionManager: ()=>CollectionManager
});
const external_Collection_js_namespaceObject = require("./Collection.js");
const external_utils_js_namespaceObject = require("./utils.js");
function _define_property(obj, key, value) {
    if (key in obj) Object.defineProperty(obj, key, {
        value: value,
        enumerable: true,
        configurable: true,
        writable: true
    });
    else obj[key] = value;
    return obj;
}
class CollectionManager {
    get dataSourceManager() {
        return this.dataSource.dataSourceManager;
    }
    get app() {
        return this.dataSourceManager.app;
    }
    getCollectionInstance(collection) {
        const collectionTemplateInstance = this.dataSource.dataSourceManager.collectionTemplateManager.getCollectionTemplate(collection.template);
        const Cls = (null == collectionTemplateInstance ? void 0 : collectionTemplateInstance.Collection) || external_Collection_js_namespaceObject.Collection;
        const transform = null == collectionTemplateInstance ? void 0 : collectionTemplateInstance.transform;
        const transformedCollection = transform ? transform((0, external_utils_js_namespaceObject.collectionTransform)(collection, this.app), this.app) : (0, external_utils_js_namespaceObject.collectionTransform)(collection, this.dataSource.dataSourceManager.app);
        const options = {
            ...transformedCollection,
            dataSource: this.dataSource.key
        };
        const instance = (0, external_utils_js_namespaceObject.applyMixins)(Cls, this.dataSourceManager.collectionMixins, options, this);
        return instance;
    }
    getInstancesMap() {
        let collections = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : [];
        const instances = collections.map((collection)=>this.getCollectionInstance(collection));
        return instances.reduce((acc, collectionInstance)=>{
            acc[collectionInstance.name] = collectionInstance;
            return acc;
        }, {});
    }
    addCollections() {
        let collections = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : [];
        const newInstancesMap = this.getInstancesMap(collections);
        this.collectionInstancesMap = {
            ...this.collectionInstancesMap,
            ...newInstancesMap
        };
        this.collectionInstancesArr = Object.values(this.collectionInstancesMap);
    }
    setCollections(collections) {
        this.collectionInstancesMap = this.getInstancesMap(collections);
        this.collectionInstancesArr = Object.values(this.collectionInstancesMap);
    }
    reAddCollections() {
        let collectionInstances = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : this.collectionInstancesArr;
        const collectionOptions = collectionInstances.map((collection)=>collection.getOptions());
        this.addCollections(collectionOptions);
    }
    getCollection(path) {
        if (!path) return;
        if ('object' == typeof path) return this.getCollectionInstance(path);
        if (String(path).split('.').length > 1) {
            const associationField = this.getCollectionField(path);
            if (!associationField) return;
            return this.getCollection(associationField.target);
        }
        return this.collectionInstancesMap[path];
    }
    getCollections(predicate) {
        if (predicate) return this.collectionInstancesArr.filter(predicate);
        return this.collectionInstancesArr;
    }
    getCollectionName(path) {
        const res = this.getCollection(path);
        return null == res ? void 0 : res.name;
    }
    getCollectionField(path) {
        if (!path) return;
        if ('object' == typeof path) return path;
        if (String(path).split('.').length < 2) {
            console.error('[@tachybase/client]: CollectionManager.getField() path "'.concat(path, '" is invalid'));
            return;
        }
        const [collectionName, ...fieldNames] = String(path).split('.');
        const collection = this.getCollection(collectionName);
        if (!collection) return;
        return collection.getField(fieldNames.join('.'));
    }
    getCollectionFields(collectionName, predicate) {
        var _this_getCollection;
        return (null === (_this_getCollection = this.getCollection(collectionName)) || void 0 === _this_getCollection ? void 0 : _this_getCollection.getFields(predicate)) || [];
    }
    getSourceKeyByAssocation(associationName) {
        if (!associationName) return;
        const field = this.getCollectionField(associationName);
        if (!field) return;
        if (field.sourceKey) return field.sourceKey;
        const sourceCollection = this.getCollection(associationName.split('.')[0]);
        if (!sourceCollection) return;
        return sourceCollection.filterTargetKey || sourceCollection.getPrimaryKey() || 'id';
    }
    clone() {
        let collections = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : [];
        const collectionManager = new CollectionManager([], this.dataSource);
        collectionManager.collectionInstancesArr = this.collectionInstancesArr;
        collectionManager.collectionInstancesMap = this.collectionInstancesMap;
        collectionManager.addCollections(collections);
        return collectionManager;
    }
    constructor(collections, dataSource){
        _define_property(this, "dataSource", void 0);
        _define_property(this, "collectionInstancesMap", void 0);
        _define_property(this, "collectionInstancesArr", void 0);
        this.dataSource = dataSource;
        this.collectionInstancesMap = {};
        this.collectionInstancesArr = [];
        this.addCollections(collections);
    }
}
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
});
