import{q as n,n as r}from"./vendor-react-Br0ZiotI.js";import{d as A}from"./vendor-dayjs-BcsA502E.js";import{c as F}from"./calculator-2QuttXTN.js";import{u as _,l as C}from"./city-autocomplete-BuE2e1o0.js";import{b6 as M,a3 as R,bs as D,bz as Y,ab as w,ay as E,D as N}from"./index-B-EDPu3R.js";import{S as U}from"./skeleton-DFMLqn_S.js";import{u as k}from"./vendor-i18n-C54Hc0CQ.js";const z="Shanghai",B="Москва",P="FILO20",q=6;function G(t){const e=t==null?void 0:t[0];return e!=null&&e.portFrom?e.portFrom.split(" ")[0]||e.portFrom:""}function Q(t){var e;return t!=null&&t.length?((e=t[t.length-1])==null?void 0:e.portTo)??"":""}function V(t,e,a){return{id:t.id,routeLabel:C(G(t.segments),e,a),destination:C(Q(t.segments),e,a),totalCost:t.totalCost??0}}function tt({cityFrom:t=z,cityTo:e=B,size:a=P,onDetailsClick:o,onOrderClick:l,limit:c=q,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]=n.useState([]),[b,m]=n.useState(!0),[L,j]=n.useState(!1);return n.useEffect(()=>{let s=!1;return m(!0),j(!1),F.calculateQuote({cityFrom:t,cityTo:e,time:A().format("DD.MM.YYYY"),size:a,pricingMode:"CONTAINER"}).then(d=>{if(s)return;const I=[...d].sort((v,y)=>(v.totalCost??0)-(y.totalCost??0));x(I.slice(0,c))}).catch(()=>{s||(x([]),j(!0))}).finally(()=>{s||m(!1)}),()=>{s=!0}},[t,e,a,c]),!b&&h.length===0?null:r.jsx(M,{py:"lg",bg:"surface",className:f,children:r.jsxs(R,{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(Y,{variant:"caption",children:i("bestOffers.popularRoute","Популярный маршрут")}),r.jsx(w,{size:"sm",as:"h2",children:g})]}),b?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:V(s,T,p),onDetailsClick:()=>o(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 a=n.useRef(null),[o,l]=n.useState(!1);return n.useEffect(()=>{if(o)return;const c=a.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()},[o]),r.jsx("div",{ref:a,style:o?void 0:{minHeight:e},children:o?t:null})}export{tt as B,et as D};