import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
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 Collection {
    get fields() {
        return this.options.fields || [];
    }
    get app() {
        return this.collectionManager.app;
    }
    get dataSource() {
        return this.options.dataSource;
    }
    get sourceKey() {
        return this.options.sourceKey;
    }
    get name() {
        return this.options.name;
    }
    get key() {
        return this.options.key;
    }
    get title() {
        return this.options.title;
    }
    get inherit() {
        return this.options.inherit;
    }
    get hidden() {
        return this.options.hidden;
    }
    get description() {
        return this.options.description;
    }
    get duplicator() {
        return this.options.duplicator;
    }
    get category() {
        return this.options.category;
    }
    get targetKey() {
        return this.options.targetKey;
    }
    get model() {
        return this.options.model;
    }
    get createdBy() {
        return this.options.createdBy;
    }
    get updatedBy() {
        return this.options.updatedBy;
    }
    get logging() {
        return this.options.logging;
    }
    get from() {
        return this.options.from;
    }
    get rawTitle() {
        return this.options.rawTitle;
    }
    getPrimaryKey() {
        if (this.primaryKey) return this.primaryKey;
        if (this.options.targetKey) return this.options.targetKey;
        const field = this.getFields({
            primaryKey: true
        })[0];
        this.primaryKey = null == field ? void 0 : field.name;
        return this.primaryKey;
    }
    get inherits() {
        return this.options.inherits || [];
    }
    get titleField() {
        return this.hasField(this.options.titleField) ? this.options.titleField : this.getPrimaryKey();
    }
    get sources() {
        return this.options.sources || [];
    }
    get template() {
        return this.options.template;
    }
    get tableName() {
        return this.options.tableName;
    }
    get viewName() {
        return this.options.viewName;
    }
    get writableView() {
        return this.options.writableView;
    }
    get filterTargetKey() {
        return this.options.filterTargetKey;
    }
    get sortable() {
        return this.options.sortable;
    }
    get autoGenId() {
        return this.options.autoGenId;
    }
    get magicAttribute() {
        return this.options.magicAttribute;
    }
    get tree() {
        return this.options.tree;
    }
    get isThrough() {
        return this.options.isThrough;
    }
    get autoCreate() {
        return this.options.autoCreate;
    }
    get resource() {
        return this.options.resource;
    }
    getOptions() {
        return this.options;
    }
    getOption(key) {
        return this.options[key];
    }
    getFields(predicate) {
        return predicate ? (0, __WEBPACK_EXTERNAL_MODULE_lodash__.filter)(this.fields, predicate) : this.fields;
    }
    getFieldsMap() {
        if (!this.fieldsMap) this.fieldsMap = this.getFields().reduce((memo, field)=>{
            memo[field.name] = field;
            return memo;
        }, {});
        return this.fieldsMap;
    }
    getFieldByAssociationName(name) {
        const fieldsMap = this.getFieldsMap();
        const [fieldName, ...others] = String(name).split('.');
        const field = fieldsMap[fieldName];
        if (!field) return;
        const collectionName = null == field ? void 0 : field.target;
        if (!collectionName) return;
        const collection = this.collectionManager.getCollection(collectionName);
        if (!collection) return;
        return collection.getField(others.join('.'));
    }
    getField(name) {
        if (!name) return;
        const fieldsMap = this.getFieldsMap();
        if (String(name).split('.').length > 1) {
            const associationRes = this.getFieldByAssociationName(name);
            if (associationRes) return associationRes;
            if ('string' == typeof name && name.startsWith("".concat(this.name, "."))) {
                name = name.replace("".concat(this.name, "."), '');
                return this.getField(name);
            }
            return;
        }
        return fieldsMap[name];
    }
    hasField(name) {
        return !!this.getField(name);
    }
    constructor(options, collectionManager){
        _define_property(this, "options", void 0);
        _define_property(this, "collectionManager", void 0);
        _define_property(this, "fieldsMap", void 0);
        _define_property(this, "primaryKey", void 0);
        this.options = options;
        this.collectionManager = collectionManager;
    }
}
export { Collection };
