"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __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);
    };
})();
var __webpack_exports__ = {};
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
require("react");
const schema_namespaceObject = require("@tachybase/schema");
const client_namespaceObject = require("@tachybase/test/client");
const index_js_namespaceObject = require("../../api-client/index.js");
const external_testUtils_index_js_namespaceObject = require("../../testUtils/index.js");
const external_user_index_js_namespaceObject = require("../../user/index.js");
const useVariables_js_namespaceObject = require("../hooks/useVariables.js");
var useVariables_js_default = /*#__PURE__*/ __webpack_require__.n(useVariables_js_namespaceObject);
const external_VariablesProvider_js_namespaceObject = require("../VariablesProvider.js");
var external_VariablesProvider_js_default = /*#__PURE__*/ __webpack_require__.n(external_VariablesProvider_js_namespaceObject);
vi.mock('../../collection-manager', async ()=>({
        useCollectionManager_deprecated: ()=>({
                getCollectionJoinField: (path)=>{
                    if ('users.belongsToField' === path) return {
                        type: 'belongsTo',
                        target: 'test'
                    };
                    if ('some.belongsToField' === path) return {
                        type: 'belongsTo',
                        target: 'someBelongsToField'
                    };
                    if ('some.belongsToField.belongsToField' === path) return {
                        type: 'belongsTo',
                        target: 'someBelongsToField'
                    };
                    if ('users.hasManyField' === path) return {
                        type: 'hasMany',
                        target: 'test'
                    };
                    if ('users.hasManyField.hasManyField' === path) return {
                        type: 'hasMany',
                        target: 'test'
                    };
                    if ('local.belongsToField' === path) return {
                        type: 'belongsTo',
                        target: 'test'
                    };
                }
            }),
        CollectionManagerPane: null
    }));
const { apiClient, mockRequest } = (0, external_testUtils_index_js_namespaceObject.mockAPIClient)();
apiClient.auth.role = 'root';
mockRequest.onGet('/auth:check').reply(()=>[
        200,
        {
            data: {
                id: 0,
                nickname: 'from request'
            }
        }
    ]);
mockRequest.onGet('/roles:check').reply(()=>[
        200,
        {
            data: {
                role: 'root'
            }
        }
    ]);
mockRequest.onGet('/users/0/belongsToField:get').reply(()=>[
        200,
        {
            data: {
                id: 0,
                name: '$user.belongsToField'
            }
        }
    ]);
mockRequest.onGet('/users/0/hasManyField:list?pageSize=9999').reply(()=>[
        200,
        {
            data: [
                {
                    id: 0,
                    name: '$user.hasManyField'
                }
            ]
        }
    ]);
mockRequest.onGet('/test/0/hasManyField:list?pageSize=9999').reply(()=>[
        200,
        {
            data: [
                {
                    id: 0,
                    name: '$user.hasManyField.hasManyField'
                }
            ]
        }
    ]);
mockRequest.onGet('/someBelongsToField/0/belongsToField:get').reply(()=>[
        200,
        {
            data: {
                id: 0,
                name: '$some.belongsToField.belongsToField'
            }
        }
    ]);
const Providers = (param)=>{
    let { children } = param;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_js_namespaceObject.APIClientProvider, {
        apiClient: apiClient,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_user_index_js_namespaceObject.CurrentUserProvider, {
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.SchemaOptionsContext.Provider, {
                value: {},
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.SchemaExpressionScopeContext.Provider, {
                    value: {},
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_VariablesProvider_js_default(), {
                        children: children
                    })
                })
            })
        })
    });
};
describe('useVariables', ()=>{
    it('parsing variables with custom ctx', async ()=>{
        const { result } = (0, client_namespaceObject.renderHook)(()=>useVariables_js_default()(), {
            wrapper: Providers
        });
        await (0, client_namespaceObject.waitFor)(async ()=>{
            expect(result.current.ctxRef.current).toMatchInlineSnapshot('\n        {\n          "$date": {\n            "last30Days": [Function],\n            "last7Days": [Function],\n            "last90Days": [Function],\n            "lastIsoWeek": [Function],\n            "lastMonth": [Function],\n            "lastQuarter": [Function],\n            "lastWeek": [Function],\n            "lastYear": [Function],\n            "next30Days": [Function],\n            "next7Days": [Function],\n            "next90Days": [Function],\n            "nextIsoWeek": [Function],\n            "nextMonth": [Function],\n            "nextQuarter": [Function],\n            "nextWeek": [Function],\n            "nextYear": [Function],\n            "now": [Function],\n            "thisIsoWeek": [Function],\n            "thisMonth": [Function],\n            "thisQuarter": [Function],\n            "thisWeek": [Function],\n            "thisYear": [Function],\n            "today": [Function],\n            "tomorrow": [Function],\n            "yesterday": [Function],\n          },\n          "$nDate": {\n            "last30Days": [Function],\n            "last7Days": [Function],\n            "last90Days": [Function],\n            "lastIsoWeek": [Function],\n            "lastMonth": [Function],\n            "lastQuarter": [Function],\n            "lastWeek": [Function],\n            "lastYear": [Function],\n            "next30Days": [Function],\n            "next7Days": [Function],\n            "next90Days": [Function],\n            "nextIsoWeek": [Function],\n            "nextMonth": [Function],\n            "nextQuarter": [Function],\n            "nextWeek": [Function],\n            "nextYear": [Function],\n            "now": [Function],\n            "thisIsoWeek": [Function],\n            "thisMonth": [Function],\n            "thisQuarter": [Function],\n            "thisWeek": [Function],\n            "thisYear": [Function],\n            "today": [Function],\n            "tomorrow": [Function],\n            "yesterday": [Function],\n          },\n          "$nRole": "root",\n          "$system": {\n            "now": [Function],\n          },\n          "$user": {\n            "id": 0,\n            "nickname": "from request",\n          },\n          "currentTime": [Function],\n          "currentUser": {\n            "id": 0,\n            "nickname": "from request",\n          },\n        }\n      ');
        });
        (0, client_namespaceObject.act)(()=>{
            result.current.setCtx({
                $user: {
                    nickname: 'test'
                }
            });
        });
        await (0, client_namespaceObject.waitFor)(async ()=>{
            expect(await result.current.parseVariable('{{ $user.nickname }}')).toBe('test');
        });
    });
    it('parsing variables with current user data', async ()=>{
        const { result } = (0, client_namespaceObject.renderHook)(()=>useVariables_js_default()(), {
            wrapper: Providers
        });
        await (0, client_namespaceObject.waitFor)(async ()=>{
            expect(await result.current.parseVariable('{{ $user.nickname }}')).toBe('from request');
        });
    });
    it('parsing variables with lazy loading of data', async ()=>{
        const { result } = (0, client_namespaceObject.renderHook)(()=>useVariables_js_default()(), {
            wrapper: Providers
        });
        await (0, client_namespaceObject.waitFor)(async ()=>{
            expect(await result.current.parseVariable('{{ $user.belongsToField }}')).toEqual({
                id: 0,
                name: '$user.belongsToField'
            });
        });
    });
    it('long variable path', async ()=>{
        const { result } = (0, client_namespaceObject.renderHook)(()=>useVariables_js_default()(), {
            wrapper: Providers
        });
        await (0, client_namespaceObject.waitFor)(async ()=>{
            expect(await result.current.parseVariable('{{ $user.belongsToField.name }}')).toBe('$user.belongsToField');
        });
    });
    it('should return array when variable path is a hasMany field', async ()=>{
        const { result } = (0, client_namespaceObject.renderHook)(()=>useVariables_js_default()(), {
            wrapper: Providers
        });
        await (0, client_namespaceObject.waitFor)(async ()=>{
            expect(await result.current.parseVariable('{{ $user.hasManyField }}')).toEqual([
                {
                    id: 0,
                    name: '$user.hasManyField'
                }
            ]);
        });
        await (0, client_namespaceObject.waitFor)(async ()=>{
            expect(await result.current.parseVariable('{{ $user.hasManyField.name }}')).toEqual([
                '$user.hasManyField'
            ]);
        });
    });
    it('$user.hasManyField.hasManyField', async ()=>{
        const { result } = (0, client_namespaceObject.renderHook)(()=>useVariables_js_default()(), {
            wrapper: Providers
        });
        await (0, client_namespaceObject.waitFor)(async ()=>{
            expect(await result.current.parseVariable('{{ $user.hasManyField.hasManyField }}')).toEqual([
                {
                    id: 0,
                    name: '$user.hasManyField.hasManyField'
                }
            ]);
        });
    });
    it('$user.hasManyField.hasManyField.name', async ()=>{
        const { result } = (0, client_namespaceObject.renderHook)(()=>useVariables_js_default()(), {
            wrapper: Providers
        });
        await (0, client_namespaceObject.waitFor)(async ()=>{
            expect(await result.current.parseVariable('{{ $user.hasManyField.hasManyField.name }}')).toEqual([
                '$user.hasManyField.hasManyField'
            ]);
        });
    });
    it('should throw error when variable is not found', async ()=>{
        const { result } = (0, client_namespaceObject.renderHook)(()=>useVariables_js_default()(), {
            wrapper: Providers
        });
        await (0, client_namespaceObject.waitFor)(async ()=>{
            try {
                await result.current.parseVariable('{{ $some.name }}');
                expect(true).toBe(false);
            } catch (error) {
                expect(error.message).toBe('VariablesProvider: $some is not found');
            }
        });
    });
    it('should not error when changing the variable path', async ()=>{
        const { result } = (0, client_namespaceObject.renderHook)(()=>useVariables_js_default()(), {
            wrapper: Providers
        });
        await (0, client_namespaceObject.waitFor)(async ()=>{
            expect(await result.current.parseVariable('{{ $user.hasManyField }}')).toEqual([
                {
                    id: 0,
                    name: '$user.hasManyField'
                }
            ]);
        });
        await (0, client_namespaceObject.waitFor)(async ()=>{
            expect(await result.current.parseVariable('{{ $user.hasManyField.hasManyField }}')).toEqual([
                {
                    id: 0,
                    name: '$user.hasManyField.hasManyField'
                }
            ]);
        });
    });
    it('register variable', async ()=>{
        const { result } = (0, client_namespaceObject.renderHook)(()=>useVariables_js_default()(), {
            wrapper: Providers
        });
        await (0, client_namespaceObject.waitFor)(async ()=>{
            expect(result.current.ctxRef.current).toMatchInlineSnapshot('\n        {\n          "$date": {\n            "last30Days": [Function],\n            "last7Days": [Function],\n            "last90Days": [Function],\n            "lastIsoWeek": [Function],\n            "lastMonth": [Function],\n            "lastQuarter": [Function],\n            "lastWeek": [Function],\n            "lastYear": [Function],\n            "next30Days": [Function],\n            "next7Days": [Function],\n            "next90Days": [Function],\n            "nextIsoWeek": [Function],\n            "nextMonth": [Function],\n            "nextQuarter": [Function],\n            "nextWeek": [Function],\n            "nextYear": [Function],\n            "now": [Function],\n            "thisIsoWeek": [Function],\n            "thisMonth": [Function],\n            "thisQuarter": [Function],\n            "thisWeek": [Function],\n            "thisYear": [Function],\n            "today": [Function],\n            "tomorrow": [Function],\n            "yesterday": [Function],\n          },\n          "$nDate": {\n            "last30Days": [Function],\n            "last7Days": [Function],\n            "last90Days": [Function],\n            "lastIsoWeek": [Function],\n            "lastMonth": [Function],\n            "lastQuarter": [Function],\n            "lastWeek": [Function],\n            "lastYear": [Function],\n            "next30Days": [Function],\n            "next7Days": [Function],\n            "next90Days": [Function],\n            "nextIsoWeek": [Function],\n            "nextMonth": [Function],\n            "nextQuarter": [Function],\n            "nextWeek": [Function],\n            "nextYear": [Function],\n            "now": [Function],\n            "thisIsoWeek": [Function],\n            "thisMonth": [Function],\n            "thisQuarter": [Function],\n            "thisWeek": [Function],\n            "thisYear": [Function],\n            "today": [Function],\n            "tomorrow": [Function],\n            "yesterday": [Function],\n          },\n          "$nRole": "root",\n          "$system": {\n            "now": [Function],\n          },\n          "$user": {\n            "id": 0,\n            "nickname": "from request",\n          },\n          "currentTime": [Function],\n          "currentUser": {\n            "id": 0,\n            "nickname": "from request",\n          },\n        }\n      ');
        });
        (0, client_namespaceObject.act)(()=>{
            result.current.registerVariable({
                name: '$new',
                collectionName: 'new',
                ctx: {
                    name: 'new variable'
                }
            });
        });
        await (0, client_namespaceObject.waitFor)(async ()=>{
            expect(result.current.ctxRef.current).toMatchInlineSnapshot('\n        {\n          "$date": {\n            "last30Days": [Function],\n            "last7Days": [Function],\n            "last90Days": [Function],\n            "lastIsoWeek": [Function],\n            "lastMonth": [Function],\n            "lastQuarter": [Function],\n            "lastWeek": [Function],\n            "lastYear": [Function],\n            "next30Days": [Function],\n            "next7Days": [Function],\n            "next90Days": [Function],\n            "nextIsoWeek": [Function],\n            "nextMonth": [Function],\n            "nextQuarter": [Function],\n            "nextWeek": [Function],\n            "nextYear": [Function],\n            "now": [Function],\n            "thisIsoWeek": [Function],\n            "thisMonth": [Function],\n            "thisQuarter": [Function],\n            "thisWeek": [Function],\n            "thisYear": [Function],\n            "today": [Function],\n            "tomorrow": [Function],\n            "yesterday": [Function],\n          },\n          "$nDate": {\n            "last30Days": [Function],\n            "last7Days": [Function],\n            "last90Days": [Function],\n            "lastIsoWeek": [Function],\n            "lastMonth": [Function],\n            "lastQuarter": [Function],\n            "lastWeek": [Function],\n            "lastYear": [Function],\n            "next30Days": [Function],\n            "next7Days": [Function],\n            "next90Days": [Function],\n            "nextIsoWeek": [Function],\n            "nextMonth": [Function],\n            "nextQuarter": [Function],\n            "nextWeek": [Function],\n            "nextYear": [Function],\n            "now": [Function],\n            "thisIsoWeek": [Function],\n            "thisMonth": [Function],\n            "thisQuarter": [Function],\n            "thisWeek": [Function],\n            "thisYear": [Function],\n            "today": [Function],\n            "tomorrow": [Function],\n            "yesterday": [Function],\n          },\n          "$nRole": "root",\n          "$new": {\n            "name": "new variable",\n          },\n          "$system": {\n            "now": [Function],\n          },\n          "$user": {\n            "id": 0,\n            "nickname": "from request",\n          },\n          "currentTime": [Function],\n          "currentUser": {\n            "id": 0,\n            "nickname": "from request",\n          },\n        }\n      ');
        });
        await (0, client_namespaceObject.waitFor)(async ()=>{
            expect(result.current.getVariable('$new')).toEqual({
                name: '$new',
                collectionName: 'new',
                ctx: {
                    name: 'new variable'
                }
            });
        });
        await (0, client_namespaceObject.waitFor)(async ()=>{
            expect(await result.current.parseVariable('{{ $new.name }}')).toBe('new variable');
        });
    });
    it('$new.noExist', async ()=>{
        const { result } = (0, client_namespaceObject.renderHook)(()=>useVariables_js_default()(), {
            wrapper: Providers
        });
        await (0, client_namespaceObject.waitFor)(async ()=>{
            result.current.registerVariable({
                name: '$new',
                ctx: {
                    name: 'new variable'
                }
            });
        });
        await (0, client_namespaceObject.waitFor)(async ()=>{
            expect(await result.current.parseVariable('{{ $new.noExist }}')).toBe(void 0);
        });
    });
    it('use object as local variable', async ()=>{
        const { result } = (0, client_namespaceObject.renderHook)(()=>useVariables_js_default()(), {
            wrapper: Providers
        });
        await (0, client_namespaceObject.waitFor)(async ()=>{
            expect(result.current.getVariable('$local')).toBe(null);
        });
        expect(await result.current.parseVariable('{{ $local.name }}', {
            name: '$local',
            ctx: {
                name: 'local variable'
            }
        })).toBe('local variable');
        expect(result.current.getVariable('$local')).toBe(null);
    });
    it('use array as local variables', async ()=>{
        const { result } = (0, client_namespaceObject.renderHook)(()=>useVariables_js_default()(), {
            wrapper: Providers
        });
        await (0, client_namespaceObject.waitFor)(async ()=>{
            expect(result.current.getVariable('$local')).toBe(null);
        });
        expect(await result.current.parseVariable('{{ $local.name }}', [
            {
                name: '$local',
                ctx: {
                    name: 'local variable'
                }
            }
        ])).toBe('local variable');
        expect(result.current.getVariable('$local')).toBe(null);
    });
    it('parse multiple variables concurrently using local variables', async ()=>{
        const { result } = (0, client_namespaceObject.renderHook)(()=>useVariables_js_default()(), {
            wrapper: Providers
        });
        const promises = [];
        await (0, client_namespaceObject.waitFor)(()=>{
            for(let i = 0; i < 3; i++)promises.push(result.current.parseVariable('{{ $user.nickname }}', [
                {
                    name: "$local",
                    ctx: {
                        name: "local variable ".concat(i)
                    }
                }
            ]));
        });
        await Promise.all(promises);
        expect(result.current.getVariable('$local')).toBe(null);
    });
    it('no id', async ()=>{
        const { result } = (0, client_namespaceObject.renderHook)(()=>useVariables_js_default()(), {
            wrapper: Providers
        });
        await (0, client_namespaceObject.waitFor)(async ()=>{
            result.current.registerVariable({
                name: '$some',
                ctx: {
                    name: 'new variable',
                    belongsToField: {
                        id: 0
                    }
                },
                collectionName: 'some'
            });
        });
        await (0, client_namespaceObject.waitFor)(async ()=>{
            expect(await result.current.parseVariable('{{ $some.belongsToField.belongsToField }}')).toEqual({
                id: 0,
                name: '$some.belongsToField.belongsToField'
            });
        });
        result.current.registerVariable({
            name: '$some',
            ctx: {
                name: 'new variable',
                belongsToField: null
            },
            collectionName: 'some'
        });
        await (0, client_namespaceObject.waitFor)(async ()=>{
            expect(await result.current.parseVariable('{{ $some.belongsToField.belongsToField }}')).toBe(void 0);
        });
    });
    it('getCollectionField', async ()=>{
        const { result } = (0, client_namespaceObject.renderHook)(()=>useVariables_js_default()(), {
            wrapper: Providers
        });
        await (0, client_namespaceObject.waitFor)(async ()=>{
            expect(await result.current.getCollectionField('{{ $user.belongsToField }}')).toEqual({
                type: 'belongsTo',
                target: 'test'
            });
        });
    });
    it('getCollectionField with no exist variable', async ()=>{
        const { result } = (0, client_namespaceObject.renderHook)(()=>useVariables_js_default()(), {
            wrapper: Providers
        });
        await (0, client_namespaceObject.waitFor)(async ()=>{
            expect(await result.current.getCollectionField('{{ $noExist.belongsToField }}')).toEqual(void 0);
        });
    });
    it('getCollectionField with local variables', async ()=>{
        const { result } = (0, client_namespaceObject.renderHook)(()=>useVariables_js_default()(), {
            wrapper: Providers
        });
        const localVariables = [
            {
                name: '$local',
                ctx: {
                    name: 'local variable'
                },
                collectionName: 'local'
            }
        ];
        await (0, client_namespaceObject.waitFor)(async ()=>{
            expect(await result.current.getCollectionField('{{ $local.belongsToField }}', localVariables)).toMatchInlineSnapshot('\n        {\n          "target": "test",\n          "type": "belongsTo",\n        }\n      ');
        });
    });
});
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
});
