import{d as B,c as C,j as e,L as $,m as _,r as D,a as z,u as A,i as T,e as E,ad as H,s as N,V as x,ae as U,g as F,af as P}from"./index-CxGMQRP8.js";import{C as q,b as y,c as V}from"./card-Cno9Ax1Q.js";import{A as J}from"./AccessLevelBadge-DltdAAii.js";import{D as K,a as M,b as Z,c as O,d as G}from"./dialog-B2yNK5Sm.js";import{U as Q}from"./UpgradeButton-ClIjchS9.js";import{A as W,a as S}from"./AnimatedBadge-BhbVKYT_.js";import{g as X,a as L}from"./sesl-badge-utils-ehMeVD2e.js";import{C as Y}from"./clock-BRThcDnu.js";import{B as I}from"./book-check-hFExILuH.js";import{B as ee}from"./bookmark-3T8L64A2.js";const se="https://cdn.storysparkle.org/ui_elements/Hero%20Icons/Reading%20Cart.webp",te=({className:s="",size:i="default",onClick:c})=>{const{getLocalizedPath:d}=B(),{t:m}=C("reading-cart"),r=i==="sm";return e.jsx($,{to:d("/reading-cart"),className:s,onClick:c,children:e.jsxs(_.div,{className:`relative inline-flex flex-col items-center ${r?"pl-8":"pl-12"}`,whileHover:{scale:1.05},whileTap:{scale:.98},transition:{type:"spring",stiffness:400,damping:17},children:[e.jsx("img",{src:se,alt:"Reading Cart",className:`absolute -top-0 left-0 pointer-events-none z-10 rotate-[-15deg] ${r?"w-14 h-auto":"w-20 h-auto"}`}),e.jsx("div",{className:`bg-gradient-to-r from-rose-400 via-red-400 to-orange-400
                      shadow-lg shadow-rose-400/30
                      hover:shadow-xl hover:shadow-rose-400/40 transition-shadow rounded-full
                      ${r?"pl-8 pr-6 py-2.5":"pl-12 pr-10 py-4"}`,children:e.jsx("span",{className:`text-white font-bold uppercase tracking-widest whitespace-nowrap ${r?"text-xs":"text-sm md:text-base"}`,children:m("checkCartButton")})})]})})},ae=({storyId:s,isInCart:i,onCartChange:c,className:d=""})=>{const[m,r]=D.useState(!1),[g,o]=D.useState(!1),{toast:u}=z(),{t:f}=C("reading-cart"),{user:l}=A(),v=T(),{getLocalizedPath:j}=B(),{userType:p}=E(),h=!!l,w=p==="free"||p==="guest",b=async a=>{if(a.preventDefault(),a.stopPropagation(),!h){v(j("/login"));return}r(!0);try{if(i){const{error:n}=await N.from("reading_cart").delete().eq("user_id",l?.id).eq("story_id",s);if(n)throw n;c(s,!1)}else{if(w&&l){const{count:R,error:k}=await N.from("reading_cart").select("id",{count:"exact",head:!0}).eq("user_id",l.id);if(k)throw k;if((R??0)>=3){o(!0);return}}const{error:n}=await N.from("reading_cart").insert({user_id:l?.id,story_id:s});if(n)throw n;c(s,!0)}}catch(n){console.error("Error updating reading cart:",n),u({title:f("toast.error"),description:f("toast.errorDescription"),variant:"destructive"})}finally{r(!1)}},t=`h-5 w-5 transition-colors ${i?"text-red-500 hover:text-red-600":"text-gray-600 hover:text-red-500"} ${m?"animate-pulse":""}`;return e.jsxs(e.Fragment,{children:[e.jsx("button",{onClick:b,disabled:m,className:`p-2 rounded-full bg-white/80 backdrop-blur-sm hover:bg-white/90 transition-all duration-200 hover:scale-110 ${d}`,title:i?"Remove from Reading Cart":"Add to Reading Cart",children:e.jsx(H,{className:t,fill:i?"currentColor":"none"})}),e.jsx(K,{open:g,onOpenChange:o,children:e.jsxs(M,{className:"sm:max-w-xl",onClick:a=>a.stopPropagation(),onPointerDown:a=>a.stopPropagation(),children:[e.jsxs(Z,{children:[e.jsx(O,{children:f("cartLimitTitle")}),e.jsx(G,{children:f("cartLimitReached")})]}),e.jsxs("div",{className:"flex flex-col items-center justify-center gap-4 pt-4",children:[e.jsx(Q,{size:"sm",onClick:()=>o(!1)}),e.jsx(te,{size:"sm",onClick:()=>o(!1)})]})]})})]})};function re(s){return s.toLowerCase().replace(/ & /g,"_").replace(/ /g,"_")}function ie(s,i){switch(i){case"bn":if(s.descriptionBn)return s.descriptionBn;break;case"es":if(s.descriptionEs)return s.descriptionEs;break;case"zh":case"zh-Hans":case"zh-Hant":if(s.descriptionZh)return s.descriptionZh;break;case"ja":if(s.descriptionJa)return s.descriptionJa;break}return s.description}const pe=({story:s,isMarkedAsRead:i=!1,isInCart:c=!1,onCartChange:d,index:m=0,getLocalizedPath:r,hasAccess:g=!0,isAuthenticated:o=!1})=>{const{t:u,i18n:f}=C("stories"),l=T(),v=ie(s,f.language),j=t=>{if(!t)return!1;const a=new Date(t);return Math.floor((new Date().getTime()-a.getTime())/(1e3*3600*24))<=30},p=t=>{const a=Math.floor(t/60),n=t%60;return`${a}:${n.toString().padStart(2,"0")}`},h=r(`/story/${s.id}`),w=()=>g?h:r(o?"/subscription":"/login"),b=t=>{!g&&!o&&(t.preventDefault(),sessionStorage.setItem("pendingAuthRedirect",h),l(r("/login"),{state:{redirectTo:h}}))};return e.jsx(W,{index:m,className:"story-card relative h-full rounded-lg overflow-hidden",children:e.jsx($,{to:w(),onClick:b,className:"cursor-pointer",children:e.jsxs(q,{className:"h-full flex flex-col rounded-lg overflow-hidden bg-white shadow-md transition-transform duration-300 hover:-translate-y-1",children:[e.jsxs("div",{className:"relative aspect-square overflow-hidden",children:[e.jsx("div",{className:"w-full h-full transition-transform group-hover:scale-105",children:s.thumbnailVideoUrl?e.jsx("video",{src:s.thumbnailVideoUrl,className:"w-full h-full object-cover",autoPlay:!0,loop:!0,muted:!0,playsInline:!0,onError:t=>{console.error("Thumbnail video failed to load:",t),t.currentTarget.style.display="none";const a=document.createElement("img");a.src=s.thumbnail,a.alt=s.title,a.className="w-full h-full object-cover",t.currentTarget.parentElement?.appendChild(a)}}):e.jsx("img",{src:s.thumbnail,alt:s.title,className:"w-full h-full object-cover"})}),e.jsx("div",{className:"absolute bottom-0 left-0 right-0 p-2 bg-gradient-to-t from-black/70 to-transparent",children:e.jsxs("div",{className:"flex items-center gap-2 text-white",children:[e.jsx(Y,{className:"h-3 w-3"}),e.jsx("span",{className:"text-xs",children:p(s.duration)})]})}),i&&e.jsx("div",{className:"absolute top-2 left-2 z-10",children:e.jsxs(S,{className:"text-primary-foreground flex items-center gap-1 bg-green-600",variant:"warning",children:[e.jsx(I,{className:"h-3 w-3"}),"Read"]})}),j(s.createdAt)&&e.jsx("div",{className:"absolute top-2 right-2 z-10",children:e.jsxs(S,{className:"text-primary-foreground flex items-center gap-1 bg-nature-yellow",variant:"warning",children:[e.jsx(ee,{className:"h-3 w-3"}),"New!"]})}),d&&e.jsx("div",{className:"absolute bottom-2 right-2 z-20",children:e.jsx(ae,{storyId:s.id,isInCart:c,onCartChange:d})})]}),e.jsxs(y,{className:"p-4 flex-1 flex flex-col",children:[e.jsxs("div",{className:"flex items-center flex-wrap gap-2 mb-2",children:[e.jsxs(x,{className:`text-xs ${U(s.cefrLevel)}`,children:[F(s.cefrLevel)," ",u("age.years")]}),s.seslScore!==void 0&&s.seslScore!==null&&e.jsxs(x,{variant:"outline",className:`text-xs ${X(L(s.seslScore))}`,children:[u("sesl.label")," ",s.seslScore," ",u(`sesl.difficulty.${L(s.seslScore)}`)]}),(()=>{const t=P(s.cefrLevel);return e.jsxs(x,{className:`text-xs ${t.classes}`,children:["CEFR ",t.shortLabel]})})()]}),e.jsx("h3",{className:"text-lg font-semibold mb-2",children:s.title}),e.jsx("p",{className:"text-sm text-nature-green",children:v}),s.accessLevel&&e.jsx("div",{className:"mt-2",children:e.jsx(J,{accessLevel:s.accessLevel})})]}),e.jsx(V,{className:"p-4 pt-0 flex flex-wrap gap-2",children:s.theme.map(t=>e.jsx(x,{variant:"outline",className:"text-xs",children:u(`theme.${re(t)}`)},t))})]})})})};export{pe as S};
//# sourceMappingURL=StoryCard-CejHRa54.js.map