import _async_to_generator from "@swc/helpers/src/_async_to_generator.mjs";
import React from 'react';
import { CacheStates } from '../../../../shared/lib/app-router-context';
import { createInitialRouterState } from '../create-initial-router-state';
import { ACTION_SERVER_PATCH, ACTION_NAVIGATE } from '../router-reducer-types';
import { navigateReducer } from './navigate-reducer';
import { serverPatchReducer } from './server-patch-reducer';
jest.mock('../fetch-server-response', ()=>{
    const flightData = [
        [
            'children',
            'linking',
            'children',
            'about',
            [
                'about',
                {
                    children: [
                        '',
                        {}
                    ]
                }, 
            ],
            /*#__PURE__*/ React.createElement("h1", null, "About Page!"),
            /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement("title", null, "About page!")), 
        ], 
    ];
    return {
        fetchServerResponse: (url)=>{
            if (url.pathname === '/linking/about') {
                return Promise.resolve([
                    flightData,
                    undefined
                ]);
            }
            throw new Error('unknown url in mock');
        }
    };
});
const flightDataForPatch = [
    [
        'children',
        'linking',
        'children',
        'somewhere-else',
        [
            'somewhere-else',
            {
                children: [
                    '',
                    {}
                ]
            }, 
        ],
        /*#__PURE__*/ React.createElement("h1", null, "Somewhere Page!"),
        /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement("title", null, "Somewhere page!")), 
    ], 
];
const getInitialRouterStateTree = ()=>[
        '',
        {
            children: [
                'linking',
                {
                    children: [
                        'about',
                        {
                            children: [
                                '',
                                {}
                            ]
                        }
                    ]
                }, 
            ]
        },
        undefined,
        undefined,
        true, 
    ];
function runPromiseThrowChain(fn) {
    return _runPromiseThrowChain.apply(this, arguments);
}
function _runPromiseThrowChain() {
    _runPromiseThrowChain = _async_to_generator(function*(fn) {
        try {
            return yield fn();
        } catch (err) {
            if (err instanceof Promise) {
                yield err;
                return yield runPromiseThrowChain(fn);
            }
            throw err;
        }
    });
    return _runPromiseThrowChain.apply(this, arguments);
}
describe('serverPatchReducer', ()=>{
    it('should apply server patch', _async_to_generator(function*() {
        const initialTree = getInitialRouterStateTree();
        const initialCanonicalUrl = '/linking';
        const children = /*#__PURE__*/ React.createElement("html", null, /*#__PURE__*/ React.createElement("head", null), /*#__PURE__*/ React.createElement("body", null, "Root layout"));
        const initialParallelRoutes = new Map([
            [
                'children',
                new Map([
                    [
                        'linking',
                        {
                            status: CacheStates.READY,
                            parallelRoutes: new Map([
                                [
                                    'children',
                                    new Map([
                                        [
                                            '',
                                            {
                                                status: CacheStates.READY,
                                                data: null,
                                                subTreeData: /*#__PURE__*/ React.createElement(React.Fragment, null, "Linking page"),
                                                parallelRoutes: new Map()
                                            }, 
                                        ], 
                                    ]), 
                                ], 
                            ]),
                            data: null,
                            subTreeData: /*#__PURE__*/ React.createElement(React.Fragment, null, "Linking layout level")
                        }, 
                    ], 
                ]), 
            ], 
        ]);
        const state = createInitialRouterState({
            initialTree,
            initialCanonicalUrl,
            children,
            initialParallelRoutes,
            isServer: false,
            location: new URL('/linking/about', 'https://localhost')
        });
        const action = {
            type: ACTION_SERVER_PATCH,
            flightData: flightDataForPatch,
            previousTree: [
                '',
                {
                    children: [
                        'linking',
                        {
                            children: [
                                'about',
                                {
                                    children: [
                                        '',
                                        {}
                                    ]
                                }
                            ]
                        }, 
                    ]
                },
                undefined,
                undefined,
                true, 
            ],
            overrideCanonicalUrl: undefined,
            cache: {
                status: CacheStates.LAZY_INITIALIZED,
                data: null,
                subTreeData: null,
                parallelRoutes: new Map()
            },
            mutable: {}
        };
        const newState = yield runPromiseThrowChain(()=>serverPatchReducer(state, action));
        const expectedState = {
            prefetchCache: new Map(),
            pushRef: {
                mpaNavigation: false,
                pendingPush: false
            },
            focusAndScrollRef: {
                apply: false
            },
            canonicalUrl: '/linking/about',
            cache: {
                status: CacheStates.READY,
                data: null,
                subTreeData: /*#__PURE__*/ React.createElement("html", null, /*#__PURE__*/ React.createElement("head", null), /*#__PURE__*/ React.createElement("body", null, "Root layout")),
                parallelRoutes: new Map([
                    [
                        'children',
                        new Map([
                            [
                                'linking',
                                {
                                    status: CacheStates.READY,
                                    parallelRoutes: new Map([
                                        [
                                            'children',
                                            new Map([
                                                [
                                                    '',
                                                    {
                                                        status: CacheStates.READY,
                                                        data: null,
                                                        subTreeData: /*#__PURE__*/ React.createElement(React.Fragment, null, "Linking page"),
                                                        parallelRoutes: new Map()
                                                    }, 
                                                ],
                                                [
                                                    'somewhere-else',
                                                    {
                                                        status: CacheStates.READY,
                                                        data: null,
                                                        subTreeData: /*#__PURE__*/ React.createElement("h1", null, "Somewhere Page!"),
                                                        parallelRoutes: new Map([
                                                            [
                                                                'children',
                                                                new Map([
                                                                    [
                                                                        '',
                                                                        {
                                                                            status: CacheStates.LAZY_INITIALIZED,
                                                                            data: null,
                                                                            head: /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement("title", null, "Somewhere page!")),
                                                                            parallelRoutes: new Map(),
                                                                            subTreeData: null
                                                                        }, 
                                                                    ], 
                                                                ]), 
                                                            ], 
                                                        ])
                                                    }, 
                                                ], 
                                            ]), 
                                        ], 
                                    ]),
                                    data: null,
                                    subTreeData: /*#__PURE__*/ React.createElement(React.Fragment, null, "Linking layout level")
                                }, 
                            ], 
                        ]), 
                    ], 
                ])
            },
            tree: [
                '',
                {
                    children: [
                        'linking',
                        {
                            children: [
                                'somewhere-else',
                                {
                                    children: [
                                        '',
                                        {}
                                    ]
                                }
                            ]
                        }, 
                    ]
                },
                undefined,
                undefined,
                true, 
            ]
        };
        expect(newState).toMatchObject(expectedState);
    }));
    it('should apply server patch (concurrent)', _async_to_generator(function*() {
        const initialTree = getInitialRouterStateTree();
        const initialCanonicalUrl = '/linking';
        const children = /*#__PURE__*/ React.createElement("html", null, /*#__PURE__*/ React.createElement("head", null), /*#__PURE__*/ React.createElement("body", null, "Root layout"));
        const initialParallelRoutes = new Map([
            [
                'children',
                new Map([
                    [
                        'linking',
                        {
                            status: CacheStates.READY,
                            parallelRoutes: new Map([
                                [
                                    'children',
                                    new Map([
                                        [
                                            '',
                                            {
                                                status: CacheStates.READY,
                                                data: null,
                                                subTreeData: /*#__PURE__*/ React.createElement(React.Fragment, null, "Linking page"),
                                                parallelRoutes: new Map()
                                            }, 
                                        ], 
                                    ]), 
                                ], 
                            ]),
                            data: null,
                            subTreeData: /*#__PURE__*/ React.createElement(React.Fragment, null, "Linking layout level")
                        }, 
                    ], 
                ]), 
            ], 
        ]);
        const state = createInitialRouterState({
            initialTree,
            initialCanonicalUrl,
            children,
            initialParallelRoutes,
            isServer: false,
            location: new URL('/linking/about', 'https://localhost')
        });
        const state2 = createInitialRouterState({
            initialTree,
            initialCanonicalUrl,
            children,
            initialParallelRoutes,
            isServer: false,
            location: new URL('/linking/about', 'https://localhost')
        });
        const action = {
            type: ACTION_SERVER_PATCH,
            flightData: flightDataForPatch,
            previousTree: [
                '',
                {
                    children: [
                        'linking',
                        {
                            children: [
                                'somewhere-else',
                                {
                                    children: [
                                        '',
                                        {}
                                    ]
                                }
                            ]
                        }, 
                    ]
                },
                undefined,
                undefined,
                true, 
            ],
            overrideCanonicalUrl: undefined,
            cache: {
                status: CacheStates.LAZY_INITIALIZED,
                data: null,
                subTreeData: null,
                parallelRoutes: new Map()
            },
            mutable: {}
        };
        yield runPromiseThrowChain(()=>serverPatchReducer(state, action));
        const newState = yield runPromiseThrowChain(()=>serverPatchReducer(state2, action));
        const expectedState = {
            prefetchCache: new Map(),
            pushRef: {
                mpaNavigation: false,
                pendingPush: false
            },
            focusAndScrollRef: {
                apply: false
            },
            canonicalUrl: '/linking/about',
            cache: {
                status: CacheStates.READY,
                data: null,
                subTreeData: /*#__PURE__*/ React.createElement("html", null, /*#__PURE__*/ React.createElement("head", null), /*#__PURE__*/ React.createElement("body", null, "Root layout")),
                parallelRoutes: new Map([
                    [
                        'children',
                        new Map([
                            [
                                'linking',
                                {
                                    status: CacheStates.READY,
                                    parallelRoutes: new Map([
                                        [
                                            'children',
                                            new Map([
                                                [
                                                    '',
                                                    {
                                                        status: CacheStates.READY,
                                                        data: null,
                                                        subTreeData: /*#__PURE__*/ React.createElement(React.Fragment, null, "Linking page"),
                                                        parallelRoutes: new Map()
                                                    }, 
                                                ], 
                                            ]), 
                                        ], 
                                    ]),
                                    data: null,
                                    subTreeData: /*#__PURE__*/ React.createElement(React.Fragment, null, "Linking layout level")
                                }, 
                            ], 
                        ]), 
                    ], 
                ])
            },
            tree: [
                '',
                {
                    children: [
                        'linking',
                        {
                            children: [
                                'about',
                                {
                                    children: [
                                        '',
                                        {}
                                    ]
                                }
                            ]
                        }, 
                    ]
                },
                undefined,
                undefined,
                true, 
            ]
        };
        expect(newState).toMatchObject(expectedState);
    }));
    it('should apply server patch without affecting focusAndScrollRef', _async_to_generator(function*() {
        const initialTree = getInitialRouterStateTree();
        const initialCanonicalUrl = '/linking';
        const children = /*#__PURE__*/ React.createElement("html", null, /*#__PURE__*/ React.createElement("head", null), /*#__PURE__*/ React.createElement("body", null, "Root layout"));
        const initialParallelRoutes = new Map([
            [
                'children',
                new Map([
                    [
                        'linking',
                        {
                            status: CacheStates.READY,
                            parallelRoutes: new Map([
                                [
                                    'children',
                                    new Map([
                                        [
                                            '',
                                            {
                                                status: CacheStates.READY,
                                                data: null,
                                                subTreeData: /*#__PURE__*/ React.createElement(React.Fragment, null, "Linking page"),
                                                parallelRoutes: new Map()
                                            }, 
                                        ], 
                                    ]), 
                                ], 
                            ]),
                            data: null,
                            subTreeData: /*#__PURE__*/ React.createElement(React.Fragment, null, "Linking layout level")
                        }, 
                    ], 
                ]), 
            ], 
        ]);
        const navigateAction = {
            type: ACTION_NAVIGATE,
            url: new URL('/linking/about', 'https://localhost'),
            isExternalUrl: false,
            locationSearch: '',
            navigateType: 'push',
            forceOptimisticNavigation: false,
            cache: {
                status: CacheStates.LAZY_INITIALIZED,
                data: null,
                subTreeData: null,
                parallelRoutes: new Map()
            },
            mutable: {}
        };
        const state = createInitialRouterState({
            initialTree,
            initialCanonicalUrl,
            children,
            initialParallelRoutes,
            isServer: false,
            location: new URL(initialCanonicalUrl, 'https://localhost')
        });
        const stateAfterNavigate = yield runPromiseThrowChain(()=>navigateReducer(state, navigateAction));
        const action = {
            type: ACTION_SERVER_PATCH,
            flightData: flightDataForPatch,
            previousTree: [
                '',
                {
                    children: [
                        'linking',
                        {
                            children: [
                                'about',
                                {
                                    children: [
                                        '',
                                        {}
                                    ]
                                }
                            ]
                        }, 
                    ]
                },
                undefined,
                undefined,
                true, 
            ],
            overrideCanonicalUrl: undefined,
            cache: {
                status: CacheStates.LAZY_INITIALIZED,
                data: null,
                subTreeData: null,
                parallelRoutes: new Map()
            },
            mutable: {}
        };
        const newState = yield runPromiseThrowChain(()=>serverPatchReducer(stateAfterNavigate, action));
        const expectedState = {
            prefetchCache: new Map(),
            pushRef: {
                mpaNavigation: false,
                pendingPush: true
            },
            focusAndScrollRef: {
                apply: true
            },
            canonicalUrl: '/linking/about',
            cache: {
                status: CacheStates.READY,
                data: null,
                subTreeData: /*#__PURE__*/ React.createElement("html", null, /*#__PURE__*/ React.createElement("head", null), /*#__PURE__*/ React.createElement("body", null, "Root layout")),
                parallelRoutes: new Map([
                    [
                        'children',
                        new Map([
                            [
                                'linking',
                                {
                                    status: CacheStates.READY,
                                    parallelRoutes: new Map([
                                        [
                                            'children',
                                            new Map([
                                                [
                                                    '',
                                                    {
                                                        status: CacheStates.READY,
                                                        data: null,
                                                        subTreeData: /*#__PURE__*/ React.createElement(React.Fragment, null, "Linking page"),
                                                        parallelRoutes: new Map()
                                                    }, 
                                                ],
                                                [
                                                    'about',
                                                    {
                                                        status: CacheStates.READY,
                                                        data: null,
                                                        subTreeData: /*#__PURE__*/ React.createElement("h1", null, "About Page!"),
                                                        parallelRoutes: new Map([
                                                            [
                                                                'children',
                                                                new Map([
                                                                    [
                                                                        '',
                                                                        {
                                                                            status: CacheStates.LAZY_INITIALIZED,
                                                                            data: null,
                                                                            head: /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement("title", null, "About page!")),
                                                                            parallelRoutes: new Map(),
                                                                            subTreeData: null
                                                                        }, 
                                                                    ], 
                                                                ]), 
                                                            ], 
                                                        ])
                                                    }, 
                                                ],
                                                [
                                                    'somewhere-else',
                                                    {
                                                        status: CacheStates.READY,
                                                        data: null,
                                                        subTreeData: /*#__PURE__*/ React.createElement("h1", null, "Somewhere Page!"),
                                                        parallelRoutes: new Map([
                                                            [
                                                                'children',
                                                                new Map([
                                                                    [
                                                                        '',
                                                                        {
                                                                            status: CacheStates.LAZY_INITIALIZED,
                                                                            data: null,
                                                                            head: /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement("title", null, "Somewhere page!")),
                                                                            parallelRoutes: new Map(),
                                                                            subTreeData: null
                                                                        }, 
                                                                    ], 
                                                                ]), 
                                                            ], 
                                                        ])
                                                    }, 
                                                ], 
                                            ]), 
                                        ], 
                                    ]),
                                    data: null,
                                    subTreeData: /*#__PURE__*/ React.createElement(React.Fragment, null, "Linking layout level")
                                }, 
                            ], 
                        ]), 
                    ], 
                ])
            },
            tree: [
                '',
                {
                    children: [
                        'linking',
                        {
                            children: [
                                'somewhere-else',
                                {
                                    children: [
                                        '',
                                        {}
                                    ]
                                }
                            ]
                        }, 
                    ]
                },
                undefined,
                undefined,
                true, 
            ]
        };
        expect(newState).toMatchObject(expectedState);
    }));
});

//# sourceMappingURL=server-patch-reducer.test.js.map