import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_collection_Collection_mjs__ from "../../data-source/collection/Collection.mjs";
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 InheritanceCollectionMixin extends __WEBPACK_EXTERNAL_MODULE__data_source_collection_Collection_mjs__.Collection {
    getParentCollectionsName() {
        if (this.parentCollectionsName) return this.parentCollectionsName.slice();
        const parents = [];
        const getParentCollectionsInner = (collectionName)=>{
            const collection = this.collectionManager.getCollection(collectionName);
            if (collection) {
                const { inherits } = collection;
                if (inherits) for(let index = 0; index < inherits.length; index++){
                    const collectionKey = inherits[index];
                    parents.push(collectionKey);
                    getParentCollectionsInner(collectionKey);
                }
            }
            return (0, __WEBPACK_EXTERNAL_MODULE_lodash__.uniq)(parents);
        };
        this.parentCollectionsName = getParentCollectionsInner(this.name);
        return this.parentCollectionsName;
    }
    getParentCollections() {
        if (this.parentCollections) return this.parentCollections.slice();
        this.parentCollections = this.getParentCollectionsName().map((collectionName)=>this.collectionManager.getCollection(collectionName));
        return this.parentCollections;
    }
    getChildrenCollectionsName() {
        let isSupportView = arguments.length > 0 && void 0 !== arguments[0] && arguments[0];
        const cacheKey = isSupportView ? 'supportView' : 'notSupportView';
        if (this.childrenCollectionsName[cacheKey]) return this.childrenCollectionsName[cacheKey].slice();
        const children = [];
        const collections = this.collectionManager.getCollections();
        const getChildrenCollectionsInner = (collectionName)=>{
            const inheritCollections = collections.filter((v)=>{
                var _v_inherits;
                return null === (_v_inherits = v.inherits) || void 0 === _v_inherits ? void 0 : _v_inherits.includes(collectionName);
            });
            inheritCollections.forEach((v)=>{
                const collectionKey = v.name;
                children.push(collectionKey);
                return getChildrenCollectionsInner(collectionKey);
            });
            if (isSupportView) {
                const sourceCollections = collections.filter((v)=>{
                    var _v_sources;
                    return (null === (_v_sources = v.sources) || void 0 === _v_sources ? void 0 : _v_sources.length) === 1 && (null == v ? void 0 : v.sources[0]) === collectionName;
                });
                sourceCollections.forEach((v)=>{
                    const collectionKey = v.name;
                    children.push(v.name);
                    return getChildrenCollectionsInner(collectionKey);
                });
            }
            return (0, __WEBPACK_EXTERNAL_MODULE_lodash__.uniq)(children);
        };
        this.childrenCollectionsName[cacheKey] = getChildrenCollectionsInner(this.name);
        return this.childrenCollectionsName[cacheKey];
    }
    getChildrenCollections() {
        let isSupportView = arguments.length > 0 && void 0 !== arguments[0] && arguments[0];
        const cacheKey = isSupportView ? 'supportView' : 'notSupportView';
        if (this.childrenCollections[cacheKey]) return this.childrenCollections[cacheKey].slice();
        this.childrenCollections[cacheKey] = this.getChildrenCollectionsName(isSupportView).map((collectionName)=>this.collectionManager.getCollection(collectionName));
        return this.childrenCollections[cacheKey];
    }
    getInheritedFields() {
        if (this.inheritsFields) return this.inheritsFields.slice();
        const parentCollections = this.getParentCollectionsName();
        this.inheritsFields = parentCollections.map((collectionName)=>{
            var _this_collectionManager_getCollection;
            return null === (_this_collectionManager_getCollection = this.collectionManager.getCollection(collectionName)) || void 0 === _this_collectionManager_getCollection ? void 0 : _this_collectionManager_getCollection.getFields();
        }).flat().filter(Boolean);
        return this.inheritsFields;
    }
    getFieldsMap() {
        if (this.fieldsMap) return this.fieldsMap;
        this.fieldsMap = this.getAllFields().reduce((memo, field)=>{
            memo[field.name] = field;
            return memo;
        }, {});
        return this.fieldsMap;
    }
    getCurrentFields(predicate) {
        return super.getFields(predicate);
    }
    getParentCollectionFields(parentCollectionName) {
        if (!this.parentCollectionFields) this.parentCollectionFields = {};
        if (this.parentCollectionFields[parentCollectionName]) return this.parentCollectionFields[parentCollectionName];
        const currentFields = this.getCurrentFields();
        const parentCollections = this.getParentCollectionsName();
        const parentCollection = this.collectionManager.getCollection(parentCollectionName);
        const parentFields = parentCollection.getCurrentFields();
        const index = parentCollections.indexOf(parentCollectionName);
        let filterFields = currentFields;
        if (index > 0) {
            parentCollections.splice(index);
            parentCollections.forEach((collectionName)=>{
                const collection = this.collectionManager.getCollection(collectionName);
                filterFields = filterFields.concat(collection.getCurrentFields());
            });
        }
        this.parentCollectionFields[parentCollectionName] = parentFields.filter((v)=>!filterFields.find((k)=>k.name === v.name));
        return this.parentCollectionFields[parentCollectionName];
    }
    getAllCollectionsInheritChain() {
        if (this.allCollectionsInheritChain) return this.allCollectionsInheritChain.slice();
        const collectionsInheritChain = [
            this.name
        ];
        const getInheritChain = (name)=>{
            const collection = this.collectionManager.getCollection(name);
            if (collection) {
                const { inherits } = collection;
                const children = collection.getChildrenCollectionsName();
                if (inherits) for(let index = 0; index < inherits.length; index++){
                    const collectionKey = inherits[index];
                    if (!collectionsInheritChain.includes(collectionKey)) {
                        collectionsInheritChain.push(collectionKey);
                        getInheritChain(collectionKey);
                    }
                }
                if (children) for(let index = 0; index < children.length; index++){
                    const collection = this.collectionManager.getCollection(children[index]);
                    const collectionKey = collection.name;
                    if (!collectionsInheritChain.includes(collectionKey)) {
                        collectionsInheritChain.push(collectionKey);
                        getInheritChain(collectionKey);
                    }
                }
            }
            return collectionsInheritChain;
        };
        this.allCollectionsInheritChain = getInheritChain(this.name);
        return this.allCollectionsInheritChain || [];
    }
    getInheritCollectionsChain() {
        if (this.inheritCollectionsChain) return this.inheritCollectionsChain.slice();
        const collectionsInheritChain = [
            this.name
        ];
        const getInheritChain = (name)=>{
            const collection = this.collectionManager.getCollection(name);
            if (collection) {
                const { inherits } = collection;
                if (inherits) for(let index = 0; index < inherits.length; index++){
                    const collectionKey = inherits[index];
                    if (!collectionsInheritChain.includes(collectionKey)) {
                        collectionsInheritChain.push(collectionKey);
                        getInheritChain(collectionKey);
                    }
                }
            }
            return collectionsInheritChain;
        };
        this.inheritCollectionsChain = getInheritChain(this.name);
        return this.inheritCollectionsChain;
    }
    getAllFields(predicate) {
        if (this.allFields) return this.allFields.slice();
        const currentFields = this.getCurrentFields();
        const inheritedFields = this.getInheritedFields();
        const allFields = (0, __WEBPACK_EXTERNAL_MODULE_lodash__.unionBy)((null == currentFields ? void 0 : currentFields.concat(inheritedFields)) || [], 'name').filter((v)=>!v.isForeignKey);
        this.allFields = allFields;
        return predicate ? (0, __WEBPACK_EXTERNAL_MODULE_lodash__.filter)(allFields, predicate) : allFields;
    }
    getForeignKeyFields() {
        if (this.foreignKeyFields) return this.foreignKeyFields.slice();
        const currentFields = this.getCurrentFields();
        const inheritedFields = this.getInheritedFields();
        const allFields = (0, __WEBPACK_EXTERNAL_MODULE_lodash__.unionBy)((null == currentFields ? void 0 : currentFields.concat(inheritedFields)) || [], 'name').filter((v)=>v.isForeignKey);
        return allFields;
    }
    constructor(...args){
        super(...args), _define_property(this, "parentCollectionsName", void 0), _define_property(this, "parentCollections", void 0), _define_property(this, "childrenCollections", {}), _define_property(this, "childrenCollectionsName", {}), _define_property(this, "inheritsFields", void 0), _define_property(this, "currentFields", void 0), _define_property(this, "allFields", void 0), _define_property(this, "parentCollectionFields", {}), _define_property(this, "allCollectionsInheritChain", void 0), _define_property(this, "inheritCollectionsChain", void 0), _define_property(this, "foreignKeyFields", void 0);
    }
}
export { InheritanceCollectionMixin };
