import{o as e}from"./rolldown-runtime-iuO8RvI4.js";import{Mi as r,ki as s}from"./vendor-frame-CwiylG4Y.js";import{ph as t}from"./shared-1-3-6x7RKF.js";import{Ao as o}from"./shared-6-Cf_fMhRZ.js";import{rn as a}from"./vendor-all-2-BJdkbLSj.js";import{R as n}from"./shared-frame-DbQbH4Yr.js";import{x as l}from"./shared-3-D-Ux4aGn.js";import{in as i,wn as c}from"./shared-8-C1HP2d2B.js";import{t as u}from"./cb9398eea-DNrEzJ-2.js";var d=e(r());function f(e){return"string"==typeof e?e:void 0}function m(e){if(!e||"object"!=typeof e)return null;const r=e,s=f(r.name);return s?{name:s,price:f(r.price),blurb:f(r.blurb),url:f(r.url)}:null}var p=s(),g=(0,d.memo)(function({data:e,disableAnalytics:r,onPageChange:s,onLearnMore:t,onImageClick:o,onDomainClick:l,onSourcesInfoOpen:i}){const c=a(),{products:f}=e,[m,g]=(0,d.useState)(0),h=(0,d.useRef)(null),[b,y]=(0,d.useState)(!1);(0,d.useLayoutEffect)(()=>{const e=h.current;if(!e)return;const r=()=>y(e.clientWidth<480);r();const s=new ResizeObserver(r);return s.observe(e),()=>s.disconnect()},[]);const j=b?1:2,x=(0,d.useRef)(j);(0,d.useLayoutEffect)(()=>{const e=x.current;e!==j&&(x.current=j,g(r=>Math.floor(r*e/j)))},[j]);const k=Math.max(1,Math.ceil(f.length/j)),v=Math.min(m,k-1),M=(0,d.useMemo)(()=>{const e=[];for(let r=0;r<f.length;r+=j)e.push(f.slice(r,r+j));return e},[f,j]),C=(0,d.useCallback)(e=>{g(e),e!==v&&s?.(e)},[v,s]),A=(0,d.useCallback)(()=>C((v-1+k)%k),[C,v,k]),w=(0,d.useCallback)(()=>C((v+1)%k),[C,v,k]),N=(0,d.useMemo)(()=>Array.from({length:k},(e,r)=>()=>C(r)),[k,C]);return(0,p.jsxs)("div",{ref:h,className:"flex w-full flex-col gap-md font-ui",children:[(0,p.jsx)("div",{className:"grid",children:M.map((e,s)=>(0,p.jsx)("div",{className:`col-start-1 row-start-1 grid gap-md ${1===e.length?"grid-cols-1":"grid-cols-2"} ${s===v?"":"invisible"}`,children:e.map((e,o)=>(0,p.jsx)(u,{product:e,widgetType:"product_carousel",disableAnalytics:r,onLearnMore:t,onDomainClick:l,onSourcesInfoOpen:i},`${e.name}-${s}-${o}`))},s))}),k>1&&(0,p.jsxs)("div",{className:"grid grid-cols-[1fr_auto_1fr] items-center",children:[(0,p.jsx)(n,{variant:"secondary",iconOnly:!0,icon:"ArrowLeft",tooltip:!1,"aria-label":c.formatMessage({defaultMessage:"Previous products",id:"Lcucny1zF+"}),onClick:A,className:"justify-self-start"}),(0,p.jsx)("div",{className:"flex items-center gap-xs",children:Array.from({length:k},(e,r)=>(0,p.jsx)("button",{type:"button","aria-label":c.formatMessage({defaultMessage:"Go to page {n}",id:"+M9cAJ3Dhe"},{n:r+1}),"aria-current":r===v,onClick:N[r],className:"size-1.5 rounded-full transition-colors "+(r===v?"bg-fill-primary":"bg-alpha-4")},r))}),(0,p.jsx)(n,{variant:"secondary",iconOnly:!0,icon:"ArrowRight",tooltip:!1,"aria-label":c.formatMessage({defaultMessage:"Next products",id:"ko9Dv680rB"}),onClick:w,className:"justify-self-end"})]})]})}),h=(0,d.memo)(function(){return(0,p.jsxs)("div",{className:"flex flex-col gap-3 bg-surface-2 p-lg font-ui",style:{borderRadius:c},children:[(0,p.jsx)(i,{width:"70%",height:22}),(0,p.jsx)(i,{width:"40%",height:14}),(0,p.jsx)(i,{width:"100%",height:48})]})}),b=(0,d.memo)(function({input:e,isStreaming:r,partialJson:s,conversationUuid:a,disableAnalytics:n}){const{shownRef:i,trackClick:c,trackRender:u}=o("product_carousel",a,n),b=l(),y=(0,d.useMemo)(()=>{const r=function(e){let r;if("string"==typeof e)try{r=t(e)}catch{return null}else{if(!e||"object"!=typeof e)return null;r=e}if(!r||"object"!=typeof r)return null;const s=r,o=[],a=[];if(Array.isArray(s.products))for(let t=0;t<s.products.length&&o.length<6;t++){const e=m(s.products[t]);e&&(o.push(e),a.push(t))}return 0===o.length?null:{products:o,sourceIndices:a,summary:f(s.summary)}}(s??e);if(!r)return null;const o=e&&"object"==typeof e?e.products:void 0,a=r.products.map((e,s)=>{const t=o?.[r.sourceIndices[s]];return Array.isArray(t?.images)?{...e,images:t.images}:e});return{...r,products:a}},[s,e]);(0,d.useEffect)(()=>{r||u(y?"rendered":"blank")},[r,y,u]);const j=(0,d.useCallback)(()=>c("learn_more"),[c]),x=(0,d.useCallback)(()=>c("page_change"),[c]),k=(0,d.useCallback)(()=>c("sources_info_open"),[c]),v=(0,d.useCallback)(e=>b(e.telemetry),[b]);return y?(0,p.jsx)("div",{ref:i,children:(0,p.jsx)(g,{data:y,disableAnalytics:n,onLearnMore:j,onPageChange:x,onDomainClick:v,onSourcesInfoOpen:k})}):r?(0,p.jsx)(h,{}):null});export{b as default};