import{r as a,j as r}from"./vendor-react-BwKDLVNL.js";import{d as y}from"./vendor-dayjs-5tvLiZdS.js";import{c as A}from"./calculator-SzSSWnew.js";import{u as _,l as C}from"./city-autocomplete-Bvqen6Sn.js";import{t as w,v as M,w as D,y as R,D as Y,G as E,cF as N}from"./index-4PWHNWf5.js";import{S as U}from"./skeleton-B4A3ODHp.js";import{u as k}from"./vendor-i18n-C6wqqoou.js";const B="Shanghai",z="Москва",G="FILO20",P=6;function Q(t){const e=t==null?void 0:t[0];return e!=null&&e.portFrom?e.portFrom.split(" ")[0]||e.portFrom:""}function V(t){var e;return t!=null&&t.length?((e=t[t.length-1])==null?void 0:e.portTo)??"":""}function Z(t,e,o){return{id:t.id,routeLabel:C(Q(t.segments),e,o),destination:C(V(t.segments),e,o),totalCost:t.totalCost??0}}function tt({cityFrom:t=B,cityTo:e=z,size:o=G,onDetailsClick:n,onOrderClick:l,limit:c=P,title:u,className:f}){var O;const{t:i,i18n:S}=k(),p=((O=S.language)==null?void 0:O.split("-")[0])??"ru",{data:T=[]}=_({language:p}),g=u??i("bestOffers.title","Лучшие предложения по стоимости"),[h,x]=a.useState([]),[m,b]=a.useState(!0),[L,j]=a.useState(!1);return a.useEffect(()=>{let s=!1;return b(!0),j(!1),A.calculateQuote({cityFrom:t,cityTo:e,time:y().format("DD.MM.YYYY"),size:o,pricingMode:"CONTAINER"}).then(d=>{if(s)return;const I=[...d].sort((v,F)=>(v.totalCost??0)-(F.totalCost??0));x(I.slice(0,c))}).catch(()=>{s||(x([]),j(!0))}).finally(()=>{s||b(!1)}),()=>{s=!0}},[t,e,o,c]),!m&&h.length===0?null:r.jsx(w,{py:"lg",bg:"surface",className:f,children:r.jsxs(M,{size:"base",children:[r.jsxs(D,{gap:6,children:[r.jsxs(D,{gap:2,className:"text-center max-w-2xl mx-auto",children:[r.jsx(R,{variant:"caption",children:i("bestOffers.popularRoute","Популярный маршрут")}),r.jsx(Y,{size:"sm",as:"h2",children:g})]}),m?r.jsx(E,{cols:3,gap:4,"aria-label":i("bestOffers.loadingAriaLabel","Загрузка предложений"),children:Array.from({length:3}).map((s,d)=>r.jsx(U,{className:"h-40 w-full"},d))}):r.jsx(E,{cols:3,gap:4,children:h.map(s=>r.jsx(N,{option:Z(s,T,p),onDetailsClick:()=>n(s),onOrderClick:l?()=>l(s):void 0},s.id))})]}),L?r.jsx("span",{className:"sr-only",role:"status",children:i("bestOffers.loadError","Не удалось загрузить предложения")}):null]})})}function et({children:t,minHeight:e=320}){const o=a.useRef(null),[n,l]=a.useState(!1);return a.useEffect(()=>{if(n)return;const c=o.current;if(!c)return;if(typeof IntersectionObserver>"u"){l(!0);return}const u=new IntersectionObserver(f=>{f.some(i=>i.isIntersecting)&&(l(!0),u.disconnect())},{rootMargin:"300px"});return u.observe(c),()=>u.disconnect()},[n]),r.jsx("div",{ref:o,style:n?void 0:{minHeight:e},children:n?t:null})}export{tt as B,et as D};