import{u as z,f as E,J as D,r as l,d as v,j as e,S as P,b as d,K as I,N as W,O,c as T,w as F,I as N}from"./index-Cn0iwDxV.js";const U={LIVESTOCK:"livestock",EQUIPMENT:"equipment",LAND:"land",FEED_FORAGE:"feed",JOB:"jobs"};function _(s,c){if(s==null)return"POA";const a=s>=1e3?`£${(s/1e3).toFixed(s%1e3===0?0:1)}k`:`£${s}`;return c&&c!=="fixed"?`${a}/${c}`:a}function b(s){const c=Date.now()-new Date(s).getTime(),a=Math.floor(c/864e5);return a===0?"Today":a===1?"Yesterday":a<7?`${a} days ago`:a<30?`${Math.floor(a/7)} weeks ago`:`${Math.floor(a/30)} months ago`}function M({categorySlugProp:s,countySlugProp:c}={}){const a=z(),x=s??a.categorySlug,h=c??a.countySlug,n=h?E(h):void 0,o=x?D(x):void 0,[j,m]=l.useState([]),[w,p]=l.useState(0),[g,S]=l.useState(1),[k,y]=l.useState(1),[L,$]=l.useState(!0),[u,A]=l.useState("newest"),B=l.useCallback(async()=>{if(!(!n||!o)){$(!0);try{if(o.slug==="jobs"){const t=new URLSearchParams({page:String(g),limit:"12",county:n.name}),r=await v.get(`/jobs?${t}`);m(r.data.map(i=>({id:i.id,title:i.title,price:i.wageSalary??"See listing",location:i.locationText??"",county:i.county??"",category:"jobs",datePosted:b(i.createdAt),href:`/jobs/${i.id}`,ctaLabel:"View Job"}))),p(r.pagination.total),y(r.pagination.pages)}else if(o.slug==="contractors"){const t=new URLSearchParams({page:String(g),limit:"12",county:n.name}),r=await v.get(`/contractors?${t}`);m(r.data.map(i=>({id:i.id,title:i.businessName??i.name??"Contractor",price:"",location:i.locationText??"",county:i.county??"",category:"equipment",datePosted:b(i.createdAt),href:`/contractors/${i.id}`,ctaLabel:"View Contractor"}))),p(r.pagination?.total??0),y(r.pagination?.pages??1)}else{const t=new URLSearchParams({page:String(g),limit:"12",sort:u,category:o.apiCategory,county:n.name}),r=await v.get(`/listings?${t}`);m(r.data.map(i=>({id:i.id,title:i.title,price:_(i.price,i.priceUnit),location:i.locationText??"",county:i.county??"",category:U[i.category]??"livestock",datePosted:b(i.createdAt),isFeatured:i.isFeatured,image:i.images?.[0]?.url,user:i.user?{avatarUrl:i.user.avatarUrl,tier:i.user.tier}:void 0}))),p(r.pagination.total),y(r.pagination.pages)}}catch(t){console.error("Failed to fetch listings:",t),m([]),p(0)}finally{$(!1)}}},[n,o,g,u]);if(l.useEffect(()=>{B()},[B]),l.useEffect(()=>{S(1)},[x,h,u]),!n||!o)return e.jsxs("div",{style:{padding:"80px 24px",textAlign:"center"},children:[e.jsx(P,{title:"Page Not Found",noIndex:!0}),e.jsx("h1",{style:{fontSize:"1.5rem",marginBottom:8},children:"Page not found"}),e.jsx("p",{style:{color:"var(--ink-light)"},children:e.jsx(d,{to:"/browse",style:{color:"var(--green-700)"},children:"Browse all listings"})})]});const C=I(n.slug),R=W.filter(t=>t.slug!==o.slug),f=O(n.slug);return e.jsxs("main",{style:{maxWidth:1200,margin:"0 auto",padding:"32px 24px"},children:[e.jsx(P,{title:o.pageTitle(n.name),description:o.metaDescription(n.name),url:`/${o.slug}/${n.slug}`,jsonLd:{"@context":"https://schema.org","@type":"BreadcrumbList",itemListElement:[{"@type":"ListItem",position:1,name:"Home",item:"https://www.farmexchange.co.uk/"},{"@type":"ListItem",position:2,name:o.label,item:`https://www.farmexchange.co.uk/${o.slug}`},{"@type":"ListItem",position:3,name:n.name}]}}),e.jsxs("nav",{style:{display:"flex",alignItems:"center",gap:6,fontSize:"0.85rem",color:"var(--ink-light)",marginBottom:24},children:[e.jsx(d,{to:"/",style:{color:"var(--ink-light)",textDecoration:"none"},children:"Home"}),e.jsx(T,{size:14}),e.jsx(d,{to:`/${o.slug}`,style:{color:"var(--ink-light)",textDecoration:"none"},children:o.label}),e.jsx(T,{size:14}),e.jsx("span",{style:{color:"var(--ink)"},children:n.name})]}),e.jsx("h1",{style:{fontSize:"2rem",fontWeight:700,marginBottom:12,color:"var(--ink)"},children:o.pageTitle(n.name)}),e.jsx("p",{style:{fontSize:"1.05rem",color:"var(--ink-light)",marginBottom:f?16:32,maxWidth:720,lineHeight:1.6},children:o.introText(n.name)}),f&&e.jsx("p",{style:{fontSize:"0.93rem",color:"var(--ink-muted)",marginBottom:32,maxWidth:720,lineHeight:1.7},children:f}),e.jsxs("div",{style:{display:"flex",justifyContent:"space-between",alignItems:"center",marginBottom:24,flexWrap:"wrap",gap:12},children:[e.jsx("p",{style:{color:"var(--ink-light)",margin:0},children:L?"Loading…":`${w} listing${w!==1?"s":""}`}),o.slug!=="jobs"&&o.slug!=="contractors"&&e.jsxs("select",{value:u,onChange:t=>A(t.target.value),style:{padding:"8px 12px",borderRadius:6,border:"1px solid var(--border)",background:"white",fontSize:"0.9rem"},children:[e.jsx("option",{value:"newest",children:"Newest First"}),e.jsx("option",{value:"price_asc",children:"Price: Low to High"}),e.jsx("option",{value:"price_desc",children:"Price: High to Low"})]})]}),L?e.jsx("div",{style:{padding:64,textAlign:"center",color:"var(--ink-light)"},children:"Loading…"}):j.length===0?e.jsxs("div",{style:{padding:"64px 24px",textAlign:"center",background:"var(--cream-50)",borderRadius:12,marginBottom:48},children:[e.jsxs("p",{style:{fontSize:"1.1rem",fontWeight:600,marginBottom:8},children:["No ",o.label.toLowerCase()," listed in ",n.name," yet"]}),e.jsx("p",{style:{color:"var(--ink-light)",marginBottom:20},children:"Be the first to list — it's free."}),e.jsx(d,{to:"/post-listing",style:{display:"inline-block",background:"var(--green-700)",color:"white",padding:"12px 24px",borderRadius:8,textDecoration:"none",fontWeight:600},children:"Post a Listing"})]}):e.jsx("div",{style:{display:"grid",gridTemplateColumns:"repeat(auto-fill, minmax(280px, 1fr))",gap:24,marginBottom:48},children:j.map(t=>e.jsx(F,{listing:t},t.id))}),k>1&&e.jsx("div",{style:{display:"flex",justifyContent:"center",gap:8,marginBottom:48},children:Array.from({length:k},(t,r)=>r+1).map(t=>e.jsx("button",{onClick:()=>{S(t),window.scrollTo(0,0)},style:{padding:"8px 14px",borderRadius:6,border:"1px solid var(--border)",background:t===g?"var(--green-700)":"white",color:t===g?"white":"var(--ink)",cursor:"pointer",fontWeight:t===g?600:400},children:t},t))}),e.jsxs("div",{style:{borderTop:"1px solid var(--border)",paddingTop:32},children:[e.jsxs("div",{style:{marginBottom:32},children:[e.jsxs("h2",{style:{fontSize:"1.2rem",fontWeight:600,marginBottom:16},children:["Also in ",n.name]}),e.jsx("div",{style:{display:"flex",flexWrap:"wrap",gap:10},children:R.map(t=>e.jsx(d,{to:`/${t.slug}/${n.slug}`,style:{padding:"8px 16px",borderRadius:8,border:"1px solid var(--border)",textDecoration:"none",color:"var(--ink)",fontSize:"0.9rem",background:"white"},children:t.label},t.slug))})]}),C.length>0&&e.jsxs("div",{style:{marginBottom:32},children:[e.jsxs("h2",{style:{fontSize:"1.2rem",fontWeight:600,marginBottom:16},children:[o.label," in nearby counties"]}),e.jsx("div",{style:{display:"flex",flexWrap:"wrap",gap:10},children:C.map(t=>e.jsx(d,{to:`/${o.slug}/${t.slug}`,style:{padding:"8px 16px",borderRadius:8,border:"1px solid var(--border)",textDecoration:"none",color:"var(--ink)",fontSize:"0.9rem",background:"white"},children:t.name},t.slug))})]}),e.jsx("div",{children:e.jsxs(d,{to:`/${o.slug}`,style:{display:"inline-flex",alignItems:"center",gap:6,color:"var(--green-700)",textDecoration:"none",fontWeight:600},children:["Browse all ",o.label.toLowerCase()," ",e.jsx(N,{size:16})]})})]})]})}export{M as default};