import{r as e,j as r}from"./vendor-react-Dp9pL_Wn.js";import{_ as o,$ as s,a0 as t,a1 as n}from"./index-CBEPphRm.js";import{Q as a}from"./c6eedc03a-CsEFTfjy.js";import{u as i}from"./vendor-intl-D2wGIp4M.js";import{P as c}from"./cb9398eea-DA1F8CTz.js";import{B as l}from"./vendor-ant-cds-bBedrtIN.js";import"./vendor-infra-DayZK7Ds.js";import"./vendor-zod-0xcuG3aM.js";import"./vendor-router-qG5OCEmI.js";import"./vendor-query-BwvgHhJ1.js";import"./vendor-zustand-BeF8W8xk.js";import"./caf7c7e06-DJYXCm3T.js";import"./vendor-base-ui-DNypCIlh.js";import"./vendor-radix-Ch96tRrt.js";import"./vendor-base-5_f7mmWg.js";import"./vendor-lodash-BvMLws5K.js";import"./vendor-icons-CokWnmlw.js";import"./vendor-motion-EDlUw66k.js";import"./c5669b8e4-DFSq88jr.js";import"./vendor-luxon-CZxT8ieM.js";import"./c5f4e1303-CiyIFgnl.js";import"./cbaad2c48-DaP34LG2.js";import"./vendor-virtual-DRz8lJfQ.js";import"./vendor-growthbook-C66YtMMX.js";import"./vendor-zod-v3-CZy0YPWu.js";import"./c9bc02337-DAknS3Mi.js";function u(e){return"string"==typeof e?e:void 0}function d(e){if(!e||"object"!=typeof e)return null;const r=e,o=u(r.name);return o?{name:o,price:u(r.price),blurb:u(r.blurb),url:u(r.url)}:null}const m=e.memo(function({data:o,disableAnalytics:s,onPageChange:t,onLearnMore:n,onImageClick:a,onDomainClick:u,onSourcesInfoOpen:d}){const m=i(),{products:f}=o,[p,j]=e.useState(0),g=e.useRef(null),[h,b]=e.useState(!1);e.useLayoutEffect(()=>{const e=g.current;if(!e)return;const r=()=>b(e.clientWidth<480);r();const o=new ResizeObserver(r);return o.observe(e),()=>o.disconnect()},[]);const v=h?1:2,y=e.useRef(v);e.useLayoutEffect(()=>{const e=y.current;e!==v&&(y.current=v,j(r=>Math.floor(r*e/v)))},[v]);const x=Math.max(1,Math.ceil(f.length/v)),k=Math.min(p,x-1),C=e.useMemo(()=>{const e=[];for(let r=0;r<f.length;r+=v)e.push(f.slice(r,r+v));return e},[f,v]),M=e.useCallback(e=>{j(e),e!==k&&t?.(e)},[k,t]),A=e.useCallback(()=>M((k-1+x)%x),[M,k,x]),w=e.useCallback(()=>M((k+1)%x),[M,k,x]),N=e.useMemo(()=>Array.from({length:x},(e,r)=>()=>M(r)),[x,M]);return r.jsxs("div",{ref:g,className:"flex w-full flex-col gap-md font-ui",children:[r.jsx("div",{className:"grid",children:C.map((e,o)=>r.jsx("div",{className:`col-start-1 row-start-1 grid gap-md ${1===e.length?"grid-cols-1":"grid-cols-2"} ${o===k?"":"invisible"}`,children:e.map((e,t)=>r.jsx(c,{product:e,widgetType:"product_carousel",disableAnalytics:s,onLearnMore:n,onDomainClick:u,onSourcesInfoOpen:d},`${e.name}-${o}-${t}`))},o))}),x>1&&r.jsxs("div",{className:"grid grid-cols-[1fr_auto_1fr] items-center",children:[r.jsx(l,{variant:"secondary",iconOnly:!0,icon:"ArrowLeft",tooltip:!1,"aria-label":m.formatMessage({defaultMessage:"Previous products",id:"Lcucny1zF+"}),onClick:A,className:"justify-self-start"}),r.jsx("div",{className:"flex items-center gap-xs",children:Array.from({length:x},(e,o)=>r.jsx("button",{type:"button","aria-label":m.formatMessage({defaultMessage:"Go to page {n}",id:"+M9cAJ3Dhe"},{n:o+1}),"aria-current":o===k,onClick:N[o],className:"size-1.5 rounded-full transition-colors "+(o===k?"bg-fill-primary":"bg-alpha-4")},o))}),r.jsx(l,{variant:"secondary",iconOnly:!0,icon:"ArrowRight",tooltip:!1,"aria-label":m.formatMessage({defaultMessage:"Next products",id:"ko9Dv680rB"}),onClick:w,className:"justify-self-end"})]})]})}),f=e.memo(function(){return r.jsxs("div",{className:"flex flex-col gap-3 bg-surface-2 p-lg font-ui",style:{borderRadius:t},children:[r.jsx(n,{width:"70%",height:22}),r.jsx(n,{width:"40%",height:14}),r.jsx(n,{width:"100%",height:48})]})}),p=e.memo(function({input:t,isStreaming:n,partialJson:i,conversationUuid:c,disableAnalytics:l}){const{shownRef:p,trackClick:j,trackRender:g}=o("product_carousel",c,l),h=s(),b=e.useMemo(()=>{const e=function(e){let r;if("string"==typeof e)try{r=a(e)}catch{return null}else{if(!e||"object"!=typeof e)return null;r=e}if(!r||"object"!=typeof r)return null;const o=r,s=[],t=[];if(Array.isArray(o.products))for(let n=0;n<o.products.length&&s.length<6;n++){const e=d(o.products[n]);e&&(s.push(e),t.push(n))}return 0===s.length?null:{products:s,sourceIndices:t,summary:u(o.summary)}}(i??t);if(!e)return null;const r=t&&"object"==typeof t?t.products:void 0,o=e.products.map((o,s)=>{const t=r?.[e.sourceIndices[s]];return Array.isArray(t?.images)?{...o,images:t.images}:o});return{...e,products:o}},[i,t]);e.useEffect(()=>{n||g(b?"rendered":"blank")},[n,b,g]);const v=e.useCallback(()=>j("learn_more"),[j]),y=e.useCallback(()=>j("page_change"),[j]),x=e.useCallback(()=>j("sources_info_open"),[j]),k=e.useCallback(e=>h(e.telemetry),[h]);return b?r.jsx("div",{ref:p,children:r.jsx(m,{data:b,disableAnalytics:l,onLearnMore:v,onPageChange:y,onDomainClick:k,onSourcesInfoOpen:x})}):n?r.jsx(f,{}):null});export{p as default};
