import{describe as d,it as u,expect as t,vi as e,beforeEach as m}from"vitest";import{getRouteSlugToCatalogSlugMap as r}from"./get-route-slug-to-catalog-slug-map.js";import{resolveItems as s}from"../../plugins/nav-utils.js";import{collectPropValueDeep as n}from"../../../utils/tree/collect-prop-value-deep.js";e.mock("~/server/plugins/nav-utils.js"),e.mock("~/shared/utils/tree/collect-prop-value-deep.js"),d("getRouteSlugToCatalogSlugMap",()=>{let g,c;m(()=>{e.clearAllMocks(),g={contentDir:"/content",outdir:"/out",serverOutDir:"/server-out",hasSitemap:!1,createSharedData:e.fn(),addRouteSharedData:e.fn(),getRouteByFsPath:e.fn(),getRouteBySlug:e.fn(),getConfig:e.fn(),getGlobalConfig:e.fn(),getAllRoutes:e.fn(),getAllRoutesForLocale:e.fn(),getAllApiRoutes:e.fn(),setGlobalData:e.fn(),getGlobalData:e.fn(),setGlobalConfig:e.fn(),parseMarkdoc:e.fn(),setSearchFacets:e.fn(),getSearchFacets:e.fn(),setSearchEngine:e.fn(),addRedirect:e.fn(),addBrowserPlugin:e.fn(),addSsrComponents:e.fn(),addApiRoute:e.fn(),loadOpenApiDefinitions:e.fn(),getRouteSharedDataByFsPath:e.fn()},c={fs:{},cache:{},getConfig:e.fn(),isPathIgnored:e.fn(),withPathPrefix:e.fn(),logger:{}}}),u("should return empty map when catalogs object is empty",async()=>{const a=await r(g,c,{});t(a).toBeInstanceOf(Map),t(a.size).toBe(0),t(s).not.toHaveBeenCalled()}),u("should return empty map when catalog has no resolved items",async()=>{const o={catalog1:{slug:"catalog1",items:[]}};e.mocked(s).mockResolvedValue(void 0);const a=await r(g,c,o);t(a).toBeInstanceOf(Map),t(a.size).toBe(0),t(s).toHaveBeenCalledTimes(1),t(n).not.toHaveBeenCalled()}),u("should map route slugs to catalog slug for single catalog with items",async()=>{const o={catalog1:{slug:"catalog1",items:[]}},a=[{routeSlug:"/page1",label:"Page 1"},{routeSlug:"/page2",label:"Page 2"}];e.mocked(s).mockResolvedValue(a),e.mocked(n).mockReturnValue(["/page1","/page2"]);const l=await r(g,c,o);t(l.size).toBe(2),t(l.get("/page1")).toBe("catalog1"),t(l.get("/page2")).toBe("catalog1"),t(s).toHaveBeenCalledWith(o.catalog1.items,g.contentDir,g,c,{navFile:""}),t(n).toHaveBeenCalledWith(a,"routeSlug")}),u("should map route slugs to catalog slug for multiple catalogs",async()=>{const o={catalog1:{slug:"catalog1",items:[]},catalog2:{slug:"catalog2",items:[]}},a=[{routeSlug:"/page1",label:"Page 1"}],l=[{routeSlug:"/page2",label:"Page 2"},{routeSlug:"/page3",label:"Page 3"}];e.mocked(s).mockResolvedValueOnce(a).mockResolvedValueOnce(l),e.mocked(n).mockReturnValueOnce(["/page1"]).mockReturnValueOnce(["/page2","/page3"]);const i=await r(g,c,o);t(i.size).toBe(3),t(i.get("/page1")).toBe("catalog1"),t(i.get("/page2")).toBe("catalog2"),t(i.get("/page3")).toBe("catalog2"),t(s).toHaveBeenCalledTimes(2),t(n).toHaveBeenCalledTimes(2)}),u("should handle catalog with empty resolved items array",async()=>{const o={catalog1:{slug:"catalog1",items:[]}};e.mocked(s).mockResolvedValue([]),e.mocked(n).mockReturnValue([]);const a=await r(g,c,o);t(a.size).toBe(0),t(s).toHaveBeenCalledTimes(1),t(n).toHaveBeenCalledWith([],"routeSlug")}),u("should handle nested items with route slugs",async()=>{const o={catalog1:{slug:"catalog1",items:[]}},a=[{routeSlug:"/parent",label:"Parent",items:[{routeSlug:"/parent/child1",label:"Child 1"},{routeSlug:"/parent/child2",label:"Child 2"}]}];e.mocked(s).mockResolvedValue(a),e.mocked(n).mockReturnValue(["/parent","/parent/child1","/parent/child2"]);const l=await r(g,c,o);t(l.size).toBe(3),t(l.get("/parent")).toBe("catalog1"),t(l.get("/parent/child1")).toBe("catalog1"),t(l.get("/parent/child2")).toBe("catalog1")}),u("should handle items without routeSlug property",async()=>{const o={catalog1:{slug:"catalog1",items:[]}},a=[{label:"Page without route slug"},{routeSlug:"/page-with-slug",label:"Page with slug"}];e.mocked(s).mockResolvedValue(a),e.mocked(n).mockReturnValue(["/page-with-slug"]);const l=await r(g,c,o);t(l.size).toBe(1),t(l.get("/page-with-slug")).toBe("catalog1")}),u("should overwrite route slug mapping when same route slug appears in multiple catalogs",async()=>{const o={catalog1:{slug:"catalog1",items:[]},catalog2:{slug:"catalog2",items:[]}},a=[{routeSlug:"/shared-page",label:"Shared Page"}],l=[{routeSlug:"/shared-page",label:"Shared Page"}];e.mocked(s).mockResolvedValueOnce(a).mockResolvedValueOnce(l),e.mocked(n).mockReturnValueOnce(["/shared-page"]).mockReturnValueOnce(["/shared-page"]);const i=await r(g,c,o);t(i.size).toBe(1),t(i.get("/shared-page")).toBe("catalog2")})});
