import{clone as f,create as M}from"@bufbuild/protobuf";import{SearchQuerySchema as g}from"@orderlyshop/core-client";import{create as l}from"@bufbuild/protobuf";import{SearchQuerySchema as y,TagSchema as C}from"@orderlyshop/core-client";function Q(e={}){return l(y,{Query:e.query??e.keywords,HiddenQuery:e.hiddenQuery,Tags:(e.tags??[]).map(t=>l(C,{Value:t})),OrderBy:e.orderBy??[],StoreId:e.storeId,Featured:e.featured??!1})}function c(e={}){return L(e)?l(y,e):Q(e)}function L(e){return"Query"in e||"HiddenQuery"in e||"Tags"in e||"OrderBy"in e||"StoreId"in e||"Featured"in e}var d="/categories/";function H(e,t={}){x(e);let r=t.pageRoot??d,n=t.urlMode??"path";return e.map(a=>m({definition:a,pageRoot:r,urlMode:n,description:t.description,pathLabels:[a.label],pathSlugs:[a.slug]}))}function B(e){let t=[],r=new Map,n=new Map;return!Array.isArray(e)||e.length===0?(t.push("navigationDefinitions must contain at least one category."),{valid:!1,errors:t}):(v(e,{errors:t,seenIds:r,seenSlugs:n,pathLabels:[],pathSlugs:[]}),{valid:t.length===0,errors:t})}function U(e){let t=B(e);return t.valid?void 0:`Navigation configuration error: ${t.errors.join(" ")}`}function x(e){let t=U(e);if(t)throw new Error(t)}function p(e){return e.flatMap(t=>[t,...p(t.children??[])])}function D(e,t){return p(t).find(r=>r.id===e)}function $(e,t){return p(t).find(r=>r.metadata.slug===e||r.id===e)}function w(e,t){return D(e,t)??$(e,t)}function E(e,t={}){let r=t.pageRoot??d;return t.urlMode==="hash"?`${r}#id=${encodeURIComponent(e)}`:`${r}${e}/`}function k(e=R(),t={}){let r=b(e),n=t.pageRoot??d;return I(r.hash)?"hash":N(r.pathname,n)?"path":"hash"}function z(e,t,r={}){let n=b(e),a=r.pageRoot??d,i=I(n.hash)??N(n.pathname,a);return i?w(i,t)??t[0]:t[0]}function m(e){let t=e.pathSlugs.join("/"),r=O(e.definition,e.parent,e.pathLabels),n=S(r,e.pathLabels),a={slug:e.pathSlugs[e.pathSlugs.length-1],parentId:e.parentId,pathSegments:e.pathSlugs,query:f(g,r),searchText:n,description:e.definition.description??e.description?.({definition:e.definition,parent:e.parent,pathLabels:e.pathLabels})??`Selected products in ${e.pathLabels.join(" ").toLowerCase()}.`,heroImage:e.definition.heroImage??e.parent?.heroImage??"",heroLayout:e.definition.heroLayout??e.parent?.heroLayout??"image",heroCoverCount:A(e.definition.heroCoverCount??e.parent?.heroCoverCount),pagePath:e.pageRoot};return{id:t,label:e.definition.label,href:E(t,{pageRoot:e.pageRoot,urlMode:e.urlMode}),query:f(g,r),metadata:a,children:(e.definition.children??[]).map(i=>m({definition:i,parent:e.definition,pageRoot:e.pageRoot,urlMode:e.urlMode,description:e.description,parentId:t,pathLabels:[...e.pathLabels,i.label],pathSlugs:[...e.pathSlugs,i.slug]}))}}function A(e){return Number.isFinite(e)&&e!==void 0?Math.max(0,Math.floor(e)):0}function v(e,t){e.forEach((r,n)=>{let a=r?.label?.trim(),i=[...t.pathLabels,a||`item ${n+1}`].join(" > "),o=r?.slug?.trim();a||t.errors.push(`Category ${i} is missing label.`),o?/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(o)||t.errors.push(`Category ${i} has invalid slug "${o}". Use lowercase letters, numbers, and hyphens only.`):t.errors.push(`Category ${i} is missing slug.`);let h=o?[...t.pathSlugs,o]:t.pathSlugs,u=h.join("/");if(u){let s=t.seenIds.get(u);s?t.errors.push(`Category ${i} duplicates category id "${u}" already used by ${s}.`):t.seenIds.set(u,i)}if(o){let s=t.seenSlugs.get(o);s?t.errors.push(`Category ${i} duplicates slug "${o}" already used by ${s}. Slugs must be globally unique.`):t.seenSlugs.set(o,i)}r.children?.length&&v(r.children,{...t,pathLabels:[...t.pathLabels,a||`item ${n+1}`],pathSlugs:h})})}function O(e,t,r){if(e.query)return c(e.query);let n=t?.query?c(t.query):void 0,a=n?f(g,n):M(g),i=n?S(n,r.slice(0,-1)):t?.label;return a.Query=[e.label,i].filter(Boolean).join(" "),a}function S(e,t){return e.Query||e.HiddenQuery||e.Tags.map(r=>r.Value).filter(Boolean).join(" ")||t.join(" ")}function N(e,t){let r=e.replace(/\/index\.html$/,"/");if(!r.startsWith(t)||r===t)return;let n=r.slice(t.length).replace(/^\/+|\/+$/g,"");return n?n.split("/").map(decodeURIComponent).join("/"):void 0}function I(e){let t=e.startsWith("#")?e.slice(1):e;if(!t)return;let n=new URLSearchParams(t.startsWith("?")?t.slice(1):t).get("id");if(n)return n.trim()||void 0;if(!t.includes("="))try{return decodeURIComponent(t).trim()||void 0}catch{return t.trim()||void 0}}function b(e){return typeof e=="string"?new URL(e,R()):new URL(e.href)}function R(){return typeof location>"u"?new URL(d,"https://example.test"):new URL(location.href)}export{x as assertValidNavigationDefinitions,z as categoryFromUrl,E as categoryPagePath,H as createCategoryNavigation,d as defaultCategoryPageRoot,D as findCategoryById,w as findCategoryByIdOrSlug,$ as findCategoryBySlug,p as flattenCategoryNavigation,U as navigationConfigurationError,k as resolveCategoryUrlMode,B as validateNavigationDefinitions};
//# sourceMappingURL=taxonomy.js.map