import{j as a}from"./jsx-runtime.D_zvdyIk.js";import{r as d}from"./index.2yJIXLcc.js";import{B as S}from"./index.BlACm4Ui.js";import"./iconBase.Blz05Vck.js";const v=({sizeOption:n})=>{const[l,o]=d.useState(!1),[w,m]=d.useState("Select One"),h=d.useRef(null),g=(r,c)=>{const i=new URLSearchParams(window.location.search);i.set(r.toLowerCase(),c);const e=`${window.location.pathname}?${i.toString()}`;window.history.replaceState({},"",e)},f=r=>{m(r),g(n.name,r),o(!1)};return d.useEffect(()=>{(()=>{const e=new URLSearchParams(window.location.search).get(n.name.toLowerCase());e&&n.values.includes(e)&&m(e)})();const c=i=>{h.current&&!h.current.contains(i.target)&&o(!1)};return document.addEventListener("mousedown",c),()=>document.removeEventListener("mousedown",c)},[n]),a.jsxs("div",{className:"w-72 relative",ref:h,children:[a.jsxs("button",{className:"w-full py-2 pl-3 pr-10 text-left bg-theme-light rounded-md cursor-pointer sm:text-sm focus:outline-none",onClick:()=>o(!l),children:[a.jsx("span",{className:"block truncate text-light",children:w}),a.jsx("span",{className:"absolute inset-y-0 right-0 flex items-center pr-2",children:a.jsx("svg",{className:`h-5 w-5 text-light transform transition-transform ${l?"rotate-180":""}`,fill:"currentColor",viewBox:"0 0 20 20","aria-hidden":"true",children:a.jsx("path",{fillRule:"evenodd",d:"M5.23 7.21a.75.75 0 011.06.02L10 11.168l3.71-3.938a.75.75 0 111.08 1.04l-4.25 4.5a.75.75 0 01-1.08 0l-4.25-4.5a.75.75 0 01.02-1.06z",clipRule:"evenodd"})})})]}),l&&a.jsx("ul",{className:"absolute z-20 mt-1 max-h-60 w-full bg-white shadow-lg rounded-md overflow-auto ring-1 ring-black/5 focus:outline-none",children:n?.values?.map(r=>a.jsx("li",{className:"py-2 px-4 cursor-pointer hover:bg-light hover:text-white text-light",onClick:()=>f(r),children:r},r))})]})},b=n=>{const l={};return n.forEach(o=>{o.altText in l||(l[o.altText]=o.url)}),l};function z({options:n,variants:l,images:o}){const[w,m]=d.useState({color:void 0,size:void 0}),h=b(o);d.useEffect(()=>{const e=new URLSearchParams(window.location.search),t=e.get("color"),s=e.get("size");m({color:t,size:s})},[]);const g=(e,t)=>{const s=new URLSearchParams(window.location.search);s.set(e,t),window.history.replaceState({},"",`${window.location.pathname}?${s.toString()}`)},f=(e,t)=>{m(s=>({...s,[e]:t})),g(e,t)},r=l.map(e=>({id:e.id,availableForSale:e.availableForSale,...e.selectedOptions.reduce((t,s)=>({...t,[s.name.toLowerCase()]:s.value}),{})})),c=n.filter(e=>!(e.name==="Title"&&e.values.includes("Default Title"))),i=n.find(e=>e.name==="Size");return a.jsxs("div",{children:[c.map(e=>a.jsxs("div",{children:[a.jsx("h5",{className:"mb-2 max-md:text-base",children:e.name==="Size"?"":e.name}),a.jsx("div",{className:"flex flex-wrap gap-3",children:e.values.map(t=>{const s=e.name.toLowerCase(),u=r.find(p=>p[s]===t&&p.availableForSale),x=w[s]===t;return e.name==="Size"?null:a.jsx("div",{children:a.jsx("button",{"aria-disabled":!u,disabled:!u,onClick:()=>f(s,t),title:`${e.name} ${t}${u?"":" (Out of Stock)"}`,className:`flex min-w-[48px] items-center justify-center rounded-md border text-sm ${x&&e.name!=="Color"?"cursor-default ring-2 ring-dark dark:ring-darkmode-dark":""} ${!x&&u&&e.name!=="Color"?"ring-1 ring-transparent transition duration-300 ease-in-out hover:scale-110 hover:ring-dark hover:dark:ring-darkmode-dark":""} ${u?"":"relative z-10 cursor-not-allowed overflow-hidden bg-neutral-100 text-neutral-500 dark:bg-neutral-900 dark:text-neutral-400"}`,children:e.name==="Color"?a.jsxs("div",{className:`relative rounded-md overflow-hidden ${x?"outline outline-1 outline-dark dark:outline-darkmode-dark":""}`,children:[a.jsx("img",{src:h[t],alt:t,width:50,height:50,className:`${x?"opacity-80":""}`}),x&&a.jsx("span",{className:"text-inherit h-full opacity-100 absolute top-2 right-2",children:a.jsx(S,{size:35})})]}):t})},t)})})]},e.id)),i&&a.jsxs("div",{className:"mb-8 mt-8",children:[a.jsx("h5",{className:"mb-2 max-md:text-base",children:i.name}),a.jsx(v,{sizeOption:i,selectedSize:w.size,onSizeChange:e=>f("size",e)})]})]})}export{z as VariantSelector};
