"use strict";
var __webpack_exports__ = {};
const client_namespaceObject = require("@tachybase/client");
var collections_namespaceObject = JSON.parse('[{"key":"h7b9i8khc3q","name":"users","inherit":false,"hidden":false,"description":null,"category":[],"namespace":"users.users","duplicator":{"dumpable":"optional","with":"rolesUsers"},"sortable":"sort","model":"UserModel","createdBy":true,"updatedBy":true,"logging":true,"from":"db2cm","title":"{{t(\\"Users\\")}}","rawTitle":"{{t(\\"Users\\")}}","fields":[{"uiSchema":{"type":"number","title":"{{t(\\"ID\\")}}","x-component":"InputNumber","x-read-pretty":true,"rawTitle":"{{t(\\"ID\\")}}"},"key":"ffp1f2sula0","name":"id","type":"bigInt","interface":"id","description":null,"collectionName":"users","parentKey":null,"reverseKey":null,"autoIncrement":true,"primaryKey":true,"allowNull":false},{"uiSchema":{"type":"string","title":"{{t(\\"Nickname\\")}}","x-component":"Input","rawTitle":"{{t(\\"Nickname\\")}}"},"key":"vrv7yjue90g","name":"nickname","type":"string","interface":"input","description":null,"collectionName":"users","parentKey":null,"reverseKey":null},{"uiSchema":{"type":"string","title":"{{t(\\"Username\\")}}","x-component":"Input","x-validator":{"username":true},"required":true,"rawTitle":"{{t(\\"Username\\")}}"},"key":"2ccs6evyrub","name":"username","type":"string","interface":"input","description":null,"collectionName":"users","parentKey":null,"reverseKey":null,"unique":true},{"uiSchema":{"type":"string","title":"{{t(\\"Email\\")}}","x-component":"Input","x-validator":"email","required":true,"rawTitle":"{{t(\\"Email\\")}}"},"key":"rrskwjl5wt1","name":"email","type":"string","interface":"email","description":null,"collectionName":"users","parentKey":null,"reverseKey":null,"unique":true},{"key":"t09bauwm0wb","name":"roles","type":"belongsToMany","interface":"m2m","description":null,"collectionName":"users","parentKey":null,"reverseKey":null,"target":"roles","foreignKey":"userId","otherKey":"roleName","onDelete":"CASCADE","sourceKey":"id","targetKey":"name","through":"rolesUsers","uiSchema":{"type":"array","title":"{{t(\\"Roles\\")}}","x-component":"AssociationField","x-component-props":{"multiple":true,"fieldNames":{"label":"title","value":"name"}}}},{"key":"1pz0art9mt7","name":"f_n2fu6hvprct","type":"string","interface":"select","description":null,"collectionName":"t_vwpds9fs4xs","parentKey":null,"reverseKey":null,"uiSchema":{"enum":[{"value":"test1","label":"test1"},{"value":"test2","label":"test2"}],"type":"string","x-component":"Select","title":"test"}}]},{"key":"pqnenvqrzxr","name":"roles","inherit":false,"hidden":false,"description":null,"category":[],"namespace":"acl.acl","duplicator":{"dumpable":"required","with":"uiSchemas"},"autoGenId":false,"model":"RoleModel","filterTargetKey":"name","sortable":true,"from":"db2cm","title":"{{t(\\"Roles\\")}}","rawTitle":"{{t(\\"Roles\\")}}","fields":[{"uiSchema":{"type":"string","title":"{{t(\\"Role UID\\")}}","x-component":"Input","rawTitle":"{{t(\\"Role UID\\")}}"},"key":"jbz9m80bxmp","name":"name","type":"uid","interface":"input","description":null,"collectionName":"roles","parentKey":null,"reverseKey":null,"prefix":"r_","primaryKey":true},{"uiSchema":{"type":"string","title":"{{t(\\"Role name\\")}}","x-component":"Input","rawTitle":"{{t(\\"Role name\\")}}"},"key":"faywtz4sf3u","name":"title","type":"string","interface":"input","description":null,"collectionName":"roles","parentKey":null,"reverseKey":null,"unique":true,"translation":true},{"key":"1enkovm9sye","name":"description","type":"string","interface":null,"description":null,"collectionName":"roles","parentKey":null,"reverseKey":null}]}]');
function getCollection(collection) {
    const app = new client_namespaceObject.Application({
        dataSourceManager: {
            collections: [
                collection
            ]
        }
    });
    return app.getCollectionManager().getCollection(collection.name);
}
describe('Collection', ()=>{
    describe('getPrimaryKey()', ()=>{
        test('Return `targetKey` if targetKey property exists', ()=>{
            const collection = getCollection({
                name: 'test',
                targetKey: 'a'
            });
            expect(collection.getPrimaryKey()).toBe('a');
        });
        test('If targetKey does not exist, return the name of the field with `primaryKey` set to true in the fields', ()=>{
            const collection = getCollection({
                name: 'test',
                fields: [
                    {
                        name: 'a',
                        primaryKey: true
                    },
                    {
                        name: 'b'
                    }
                ]
            });
            expect(collection.getPrimaryKey()).toBe('a');
        });
        test('cache the result', ()=>{
            const collection = getCollection({
                name: 'test',
                fields: [
                    {
                        name: 'a',
                        primaryKey: true
                    }
                ]
            });
            const spy = vitest.spyOn(collection, 'getFields');
            collection.getPrimaryKey();
            collection.getPrimaryKey();
            expect(spy).toHaveBeenCalledTimes(1);
        });
    });
    describe('titleField', ()=>{
        test('return `titleField` if it exists', ()=>{
            const collection = getCollection({
                name: 'test',
                titleField: 'a',
                fields: [
                    {
                        name: 'a'
                    }
                ]
            });
            expect(collection.titleField).toBe('a');
        });
        test('if `titleField` does not exist in fields, return `primaryKey`', ()=>{
            const collection = getCollection({
                name: 'test',
                titleField: 'a',
                targetKey: 'b'
            });
            expect(collection.titleField).toBe('b');
        });
        test('if `titleField` does not exist, return `primaryKey`', ()=>{
            const collection = getCollection({
                name: 'test',
                targetKey: 'a'
            });
            expect(collection.titleField).toBe('a');
        });
    });
    describe('options', ()=>{
        test('getOptions()', ()=>{
            const collection = getCollection({
                name: 'test',
                titleField: 'a',
                targetKey: 'b'
            });
            expect(collection.getOptions()).toMatchObject({
                name: 'test',
                titleField: 'a',
                targetKey: 'b',
                dataSource: client_namespaceObject.DEFAULT_DATA_SOURCE_KEY
            });
        });
        test('getOption()', ()=>{
            const collection = getCollection({
                name: 'test',
                key: 'a',
                model: 'b'
            });
            expect(collection.getOption('name')).toBe('test');
            expect(collection.getOption('key')).toBe('a');
            expect(collection.getOption('model')).toBe('b');
            expect(collection.getOption('dataSource')).toBe(client_namespaceObject.DEFAULT_DATA_SOURCE_KEY);
        });
    });
    test('dataSource', ()=>{
        const app = new client_namespaceObject.Application({
            dataSourceManager: {
                dataSources: [
                    {
                        key: 'a',
                        displayName: 'a',
                        collections: [
                            {
                                name: 'user'
                            }
                        ]
                    }
                ]
            }
        });
        const user = app.getCollectionManager('a').getCollection('user');
        expect(user.dataSource).toBe('a');
    });
    describe('getFields()', ()=>{
        test('return fields', ()=>{
            const collection = getCollection({
                name: 'test',
                fields: [
                    {
                        name: 'a'
                    },
                    {
                        name: 'b'
                    }
                ]
            });
            expect(collection.getFields()).toMatchObject([
                {
                    name: 'a'
                },
                {
                    name: 'b'
                }
            ]);
        });
        test('support predicate', ()=>{
            const collection = getCollection({
                name: 'test',
                fields: [
                    {
                        name: 'a',
                        primaryKey: false
                    },
                    {
                        name: 'b',
                        primaryKey: true
                    }
                ]
            });
            const res1 = collection.getFields({
                name: 'a'
            });
            expect(res1.length).toBe(1);
            expect(res1[0].name).toBe('a');
            const res2 = collection.getFields('primaryKey');
            expect(res2.length).toBe(1);
            expect(res2[0].name).toBe('b');
            const res3 = collection.getFields({
                primaryKey: true
            });
            expect(res3.length).toBe(1);
            expect(res3[0].name).toBe('b');
            const res4 = collection.getFields((field)=>'a' === field.name);
            expect(res4.length).toBe(1);
            expect(res4[0].name).toBe('a');
        });
    });
    describe('getField()', ()=>{
        test('return field', ()=>{
            const collection = getCollection({
                name: 'test',
                fields: [
                    {
                        name: 'a'
                    },
                    {
                        name: 'b'
                    }
                ]
            });
            expect(collection.getField('a')).toMatchObject({
                name: 'a'
            });
            expect(collection.getField('test.a')).toMatchObject({
                name: 'a'
            });
        });
        test('support dot notation', ()=>{
            const app = new client_namespaceObject.Application({
                dataSourceManager: {
                    collections: collections_namespaceObject
                }
            });
            const users = app.getCollectionManager().getCollection('users');
            expect(users.getField('roles.name')).toMatchObject({
                name: 'name',
                collectionName: 'roles'
            });
        });
        test('return undefined if field does not exist', ()=>{
            const app = new client_namespaceObject.Application({
                dataSourceManager: {
                    collections: collections_namespaceObject
                }
            });
            const users = app.getCollectionManager().getCollection('users');
            expect(users.getField('no-exist')).toBeUndefined();
            expect(users.getField('no-exist.c')).toBeUndefined();
            expect(users.getField('id.no-exist')).toBeUndefined();
            expect(users.getField('roles.no-exist')).toBeUndefined();
        });
    });
    describe('hasField()', ()=>{
        test('return true if field exists', ()=>{
            const collection = getCollection({
                name: 'test',
                fields: [
                    {
                        name: 'a'
                    },
                    {
                        name: 'b'
                    }
                ]
            });
            expect(collection.hasField('a')).toBe(true);
        });
        test('return false if field does not exist', ()=>{
            const collection = getCollection({
                name: 'test',
                fields: [
                    {
                        name: 'a'
                    },
                    {
                        name: 'b'
                    }
                ]
            });
            expect(collection.hasField('c')).toBe(false);
        });
    });
    test('properties', ()=>{
        const app = new client_namespaceObject.Application({
            dataSourceManager: {
                collections: collections_namespaceObject
            }
        });
        const users = app.getCollectionManager().getCollection('users');
        const usersOptions = collections_namespaceObject.find((c)=>'users' === c.name);
        expect(users.app).toBe(app);
        expect(users.template).toBe(usersOptions.template);
        expect(users.sourceKey).toBe(usersOptions.sourceKey);
        expect(users.name).toBe(usersOptions.name);
        expect(users.key).toBe(usersOptions.key);
        expect(users.title).toBe(usersOptions.title);
        expect(users.inherit).toBe(usersOptions.inherit);
        expect(users.hidden).toBe(usersOptions.hidden);
        expect(users.description).toBe(usersOptions.description);
        expect(users.duplicator).toBe(usersOptions.duplicator);
        expect(users.category).toBe(usersOptions.category);
        expect(users.targetKey).toBe(usersOptions.targetKey);
        expect(users.model).toBe(usersOptions.model);
        expect(users.createdBy).toBe(usersOptions.createdBy);
        expect(users.updatedBy).toBe(usersOptions.updatedBy);
        expect(users.logging).toBe(usersOptions.logging);
        expect(users.from).toBe(usersOptions.from);
        expect(users.rawTitle).toBe(usersOptions.rawTitle);
        expect(users.inherits).toMatchObject([]);
        expect(users.sources).toMatchObject([]);
        expect(users.fields).toMatchObject(usersOptions.fields);
        expect(users.tableName).toBe(usersOptions.tableName);
        expect(users.viewName).toBe(usersOptions.viewName);
        expect(users.writableView).toBe(usersOptions.writableView);
        expect(users.filterTargetKey).toBe(usersOptions.filterTargetKey);
        expect(users.sortable).toBe(usersOptions.sortable);
        expect(users.autoGenId).toBe(usersOptions.autoGenId);
        expect(users.magicAttribute).toBe(usersOptions.magicAttribute);
        expect(users.tree).toBe(usersOptions.tree);
        expect(users.isThrough).toBe(usersOptions.isThrough);
        expect(users.autoCreate).toBe(usersOptions.autoCreate);
        expect(users.resource).toBe(usersOptions.resource);
    });
});
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
});
