import{a as Oe,j as e,g as $e,s as se}from"./index-5JG4S71q.js";import{r as s,R as Z}from"./vendor-react-DP_h-t3b.js";import{u as Fe,S as ze}from"./ScheduleModal-j4AWtqsx.js";import{y as Ee,f as oe,h as Je,X as Xe,aj as He,E as Ye,O as We}from"./vendor-ui-B25LK1Jt.js";function Le({text:a,onChange:re,onAnalyze:R,onImprove:_,onCopy:De,onTextChanged:P,isAnalyzing:u,isImproving:p,canAnalyze:O,canPerformAnalyze:le,hasResults:f,textMatchesAnalyzed:C,profileImageUrl:ne,avatarLoading:Ve=!1,isSignedIn:$=!1,tierLimits:c,profile:l,onUpgradeClick:x,onSignInClick:h,initialImage:y,onToggleVersion:ce,canToggle:ie=!1,showingImproved:F=!0}){const{user:o}=Oe(),{addBookmark:A,removeBookmark:G,isBookmarked:v,canUseBookmarks:N}=Fe(o),[de,K]=s.useState(!1),[z,g]=s.useState(""),[ue,b]=s.useState(!1),[Ze,he]=s.useState(""),[me,E]=s.useState(!1),[pe,fe]=s.useState(""),[xe,U]=s.useState(!1),[ve,J]=s.useState(!1),q=s.useRef(null),X=s.useRef(null),H=s.useRef(),Y=s.useRef(),I=s.useRef(null),[S,W]=s.useState(null),[Ae,D]=s.useState(null),T=a.includes("<span")||a.includes("</span>");Z.useEffect(()=>{W(y||null),y&&D(null)},[y]);const ge=s.useCallback(()=>{if(!a||a.trim()==="")return 2;const t=a.split(`
`).length,r=Math.ceil(a.length/50),n=Math.max(t,r);return Math.max(2,Math.min(n,15))},[a]),Q=s.useCallback(()=>{const t=q.current;t&&(t.style.height="auto",t.style.height=`${Math.max(60,t.scrollHeight)}px`)},[]);Z.useEffect(()=>{T||Q()},[a,T,Q]),Z.useEffect(()=>{if(T&&X.current){const t=X.current;t.style.height="auto",t.style.height=`${Math.max(80,t.scrollHeight)}px`}},[a,T]);const k=t=>t.replace(/<span[^>]*>/g,"").replace(/<\/span>/g,"").trim(),L=t=>{re(t),he(k(t)),f&&t!==a&&(P==null||P())},be=s.useCallback(t=>{if((t.metaKey||t.ctrlKey)&&t.key==="Enter"){t.preventDefault();const r=f&&C&&!u;(r?!p&&f:O&&!u)&&(r?_():R())}},[R,_,O,f,C,p,u]),ke=t=>{t.stopPropagation(),we()},we=s.useCallback(()=>{try{const t=a.replace(/<span[^>]*>/g,"").replace(/<\/span>/g,"");navigator.clipboard.writeText(t),K(!0),setTimeout(()=>K(!1),2e3)}catch{}},[a]),je=t=>{t.stopPropagation(),ye()},ye=s.useCallback(async()=>{var i,w,j,d,ae;if(!a.trim())return;if(!o){h==null||h();return}if(!N){g("Upgrade to Pro Plan to use Bookmarks"),b(!0),setTimeout(()=>b(!1),2e3);return}const t=a.replace(/<span[^>]*>/g,"").replace(/<\/span>/g,""),n=`compose-${k(a)}`;try{if(v(n)){const B=await G(n);g(B?"Bookmark removed":"Failed to remove bookmark")}else{const B=await A(t,"✍️ My post","compose",n,((i=o==null?void 0:o.user_metadata)==null?void 0:i.preferred_username)||((w=o==null?void 0:o.user_metadata)==null?void 0:w.user_name)||((d=(j=o==null?void 0:o.user_metadata)==null?void 0:j.name)==null?void 0:d.split(" ")[0])||"You",new Date().toISOString(),((ae=o==null?void 0:o.user_metadata)==null?void 0:ae.avatar_url)||void 0,void 0,void 0,void 0,y||void 0);g(B?"Bookmarked!":"Failed to bookmark post")}b(!0),setTimeout(()=>b(!1),2e3)}catch{g("Failed to update bookmark"),b(!0),setTimeout(()=>b(!1),2e3)}},[a,o,A,G,v,N,h,y]),ee=()=>{E(!0)},Ne=async t=>{try{const r=a.replace(/<span[^>]*>/g,"").replace(/<\/span>/g,""),{data:{session:n}}=await se.auth.getSession();if(!n)throw new Error("Not authenticated");const i=await fetch("https://pamffbanuwjldtpshraq.supabase.co/functions/v1/post-now",{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${n.access_token}`,apikey:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InBhbWZmYmFudXdqbGR0cHNocmFxIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTU0NTU5MjgsImV4cCI6MjA3MTAzMTkyOH0.z2YaWkmSGwp9RiOwklFhKAAlRM0jvkPiMPbhno_uYIo"},body:JSON.stringify({post_text:r,image_url:S||void 0,platforms:t})});if(!i.ok){const d=await i.json().catch(()=>({}));throw new Error(d.error||"Failed to post")}const w=await i.json();E(!1);const j=t.map(d=>d==="x"?"X":d==="linkedin"?"LinkedIn":d==="threads"?"Threads":d==="bluesky"?"Bluesky":d).join(", ");te(`Successfully posted to ${j}!`)}catch(r){alert(r instanceof Error?r.message:"Failed to post")}},Se=async(t,r)=>{const n=a.replace(/<span[^>]*>/g,"").replace(/<\/span>/g,""),{data:{session:i}}=await se.auth.getSession();if(!i)throw new Error("Not authenticated");const w=await fetch("https://pamffbanuwjldtpshraq.supabase.co/functions/v1/schedule-post",{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${i.access_token}`,apikey:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InBhbWZmYmFudXdqbGR0cHNocmFxIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTU0NTU5MjgsImV4cCI6MjA3MTAzMTkyOH0.z2YaWkmSGwp9RiOwklFhKAAlRM0jvkPiMPbhno_uYIo"},body:JSON.stringify({post_text:n,scheduled_time:t,image_url:S||void 0,platforms:r})});if(!w.ok){const j=await w.json().catch(()=>({}));throw new Error(j.error||"Failed to schedule post")}},te=s.useCallback(t=>{fe(t),U(!0),J(!1),H.current&&clearTimeout(H.current),Y.current&&clearTimeout(Y.current),Y.current=setTimeout(()=>{J(!0)},2700),H.current=setTimeout(()=>{U(!1),J(!1)},3e3)},[]),Te=s.useCallback(t=>{var n;const r=(n=t.target.files)==null?void 0:n[0];if(r&&r.type.startsWith("image/")){const i=new FileReader;i.onloadend=()=>{W(i.result),D(r)},i.readAsDataURL(r)}},[]),Ce=s.useCallback(()=>{W(null),D(null),I.current&&(I.current.value="")},[]),Ie=s.useCallback(()=>{var t;(t=I.current)==null||t.click()},[]),Me=t=>t.replace(/<span[^>]*>/g,"").replace(/<\/span>/g,"").length,m=f&&C&&!u,Be=m?p?"Improving...":(c&&!c.canImprove,"Improve"):u?"Analyzing...":"Analyze",V=m?!p&&f&&C:O&&!u&&!p&&le,Re=()=>{if(!$){h==null||h();return}m?c!=null&&c.canImprove?_():x==null||x():R()},M=Me(a),_e=M>280,Pe=M>=240;return e.jsxs("div",{className:"h-full flex flex-col overflow-x-hidden max-w-full",children:[e.jsxs("div",{className:"flex items-start justify-between mb-3",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("img",{src:$e(ne)||"/730e4976-9074-484d-b01b-0ed75100909a.png",alt:l!=null&&l.display_name?`${l.display_name}'s profile`:"Profile",className:"w-12 h-12 rounded-full flex-shrink-0 object-cover"}),e.jsxs("div",{className:"flex flex-col",children:[e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx("span",{className:"text-sm sm:text-base font-bold text-[var(--text)]",children:(l==null?void 0:l.display_name)||(l==null?void 0:l.username)||"You"}),(l==null?void 0:l.is_blue_verified)&&e.jsx("img",{src:"/64px-Twitter_Verified_Badge.svg.png",alt:"Verified",className:"w-4 h-4 flex-shrink-0"})]}),e.jsxs("span",{className:"text-sm text-gray-500",children:["@",(l==null?void 0:l.username)||"you"]})]})]}),e.jsx("div",{className:"flex items-center gap-1",children:a&&e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx("button",{onClick:ke,className:`
                p-1.5 rounded-full transition-all duration-200
                ${de?"bg-green-100 text-green-600":"text-[var(--muted)] hover:text-[var(--text)] hover:bg-[var(--hover)]"}
              `,title:"Copy post","aria-label":"Copy post content",children:e.jsx(Ee,{size:14})}),e.jsx("button",{onClick:a.trim()?ee:void 0,disabled:!a.trim(),className:"p-1.5 rounded-full transition-all duration-200 text-[var(--muted)] hover:text-[var(--text)] hover:bg-[var(--hover)] disabled:opacity-50 disabled:cursor-not-allowed",title:o?"Schedule or post to X":"Sign in to schedule","aria-label":o?"Schedule or post to X":"Sign in to schedule",children:e.jsx(oe,{size:14})}),e.jsx("button",{onClick:je,className:`
                p-1.5 rounded-full transition-all duration-200
                ${o&&N&&v(`compose-${k(a)}`)?"bg-yellow-100 text-yellow-600":"text-[var(--muted)] hover:text-[var(--text)] hover:bg-[var(--hover)]"}
              `,title:o?N?v(`compose-${k(a)}`)?"Remove bookmark":"Bookmark post":"Upgrade to Pro to bookmark":"Sign in to bookmark","aria-label":o?N?v(`compose-${k(a)}`)?"Remove bookmark":"Bookmark post":"Upgrade to Pro to bookmark":"Sign in to bookmark",children:e.jsx(Je,{size:14,fill:v(`compose-${k(a)}`)?"currentColor":"none"})})]})})]}),e.jsxs("div",{className:"flex-1 min-w-0 overflow-x-hidden max-w-full",children:[e.jsx("div",{className:"relative max-w-full overflow-x-hidden h-full",children:T?e.jsx("div",{ref:X,className:`
                  w-full max-w-full bg-transparent border-none outline-none resize-none h-full
                  lg:min-h-[200px]
                  text-[var(--text)] text-sm sm:text-base min-h-[80px] leading-relaxed
                  cursor-text p-0 whitespace-pre-wrap break-words overflow-x-hidden overflow-y-auto
                `,dangerouslySetInnerHTML:{__html:a},onClick:()=>{const t=a.replace(/<span[^>]*>/g,"").replace(/<\/span>/g,"");L(t)},"aria-label":"Improved post preview (click to edit)"}):e.jsx("textarea",{ref:q,value:a,onChange:t=>L(t.target.value),onKeyDown:be,placeholder:"What's happening?",rows:ge(),className:`
                  w-full max-w-full bg-transparent border-none outline-none resize-none
                  text-[var(--text)] text-sm sm:text-base placeholder-[var(--muted)]
                  focus:ring-0 leading-relaxed break-words overflow-x-hidden overflow-y-auto
                `,"aria-label":"Compose your post"})}),S&&e.jsxs("div",{className:"mt-3 relative inline-block w-full",children:[e.jsx("img",{src:S,alt:"Selected attachment",className:"w-full h-auto rounded-2xl border border-gray-200 object-contain"}),e.jsx("button",{onClick:Ce,className:"absolute top-2 right-2 w-8 h-8 bg-gray-900/80 hover:bg-gray-900 text-white rounded-full flex items-center justify-center transition-all duration-200","aria-label":"Remove image",children:e.jsx(Xe,{size:16})})]})]}),e.jsxs("div",{className:"flex items-center justify-between mt-4 flex-shrink-0",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("input",{ref:I,type:"file",accept:"image/*",onChange:Te,className:"hidden","aria-label":"Select image to attach"}),e.jsx("button",{onClick:Ie,className:"w-8 h-8 rounded-full flex items-center justify-center text-[var(--accent)] hover:bg-[var(--accent)]/10 transition-all duration-200",title:"Attach image","aria-label":"Attach image",children:e.jsx(He,{size:18})}),e.jsx("div",{className:"flex items-center justify-center w-6 h-6 ml-1",title:`${M}/280`,children:e.jsxs("svg",{className:"transform -rotate-90 w-full h-full",children:[e.jsx("circle",{cx:"12",cy:"12",r:"9",stroke:"currentColor",strokeWidth:"2",fill:"transparent",className:"text-gray-200"}),e.jsx("circle",{cx:"12",cy:"12",r:"9",stroke:"currentColor",strokeWidth:"2",fill:"transparent",strokeDasharray:2*Math.PI*9,strokeDashoffset:2*Math.PI*9-Math.min(M/280,1)*(2*Math.PI*9),strokeLinecap:"round",className:`transition-all duration-300 ease-in-out ${_e?"text-red-500":Pe?"text-yellow-500":"text-[var(--accent)]"}`})]})}),ie&&e.jsx("button",{onClick:ce,className:`w-9 h-9 rounded-full flex items-center justify-center transition-all duration-200 ${F?"text-green-600 bg-green-50 hover:bg-green-100":"text-gray-400 hover:text-gray-600 hover:bg-gray-50"}`,title:F?"Viewing improved version (click to see original)":"Viewing original version (click to see improved)",children:F?e.jsx(Ye,{size:18}):e.jsx(We,{size:18})})]}),e.jsxs("div",{className:"flex items-center gap-2 flex-shrink-0",children:[e.jsxs("div",{className:"relative",children:[e.jsx("button",{onClick:Re,disabled:!V,className:`
              px-6 py-1.5 rounded-full font-bold text-sm transition-all duration-200
              focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--accent)]
              ${V?m?(c!=null&&c.canImprove,"bg-green-600 text-white hover:bg-green-700"):"bg-[var(--accent)] text-white hover:bg-[var(--accent)]/90":m?"bg-green-600/50 text-white cursor-not-allowed":"bg-[var(--muted)]/20 text-[var(--muted)] cursor-not-allowed"}
              ${$?"":"bg-[var(--muted)]/20 text-[var(--muted)] cursor-not-allowed"}
            `,"aria-label":$?m?p?"Improving post...":c!=null&&c.canImprove?"Improve post":"Upgrade to improve posts":u?"Analyzing post...":"Analyze post":"Sign in to analyze posts",children:Be}),m&&c&&!c.canImprove&&V&&e.jsx("div",{className:"absolute -top-2 -right-2 bg-[var(--accent)] text-white text-[10px] px-1.5 py-0.5 rounded-full font-bold cursor-pointer hover:bg-[var(--accent)]/90 transition-all duration-200",onClick:()=>x==null?void 0:x(),children:"PRO"})]}),e.jsx("button",{onClick:a.trim()?ee:void 0,disabled:!a.trim(),className:"w-8 h-8 rounded-full bg-black hover:bg-gray-800 disabled:bg-gray-300 disabled:cursor-not-allowed flex items-center justify-center transition-all duration-200",title:o?"Schedule or post to X":"Sign in to schedule","aria-label":o?"Schedule or post to X":"Sign in to schedule",children:e.jsx(oe,{size:16,className:"text-white"})})]})]}),e.jsx(ze,{isOpen:me,onClose:()=>E(!1),postText:a,onSchedule:Se,onPostNow:Ne,onSuccess:te,imageUrl:S}),xe&&e.jsx("div",{className:`fixed top-4 left-1/2 transform -translate-x-1/2 z-[9998] transition-all duration-300 ${ve?"opacity-0 scale-95":"opacity-100 scale-100"}`,children:e.jsxs("div",{className:"bg-green-500 text-white rounded-lg px-6 py-3 flex items-center gap-3 shadow-lg",children:[e.jsx("svg",{className:"w-5 h-5 flex-shrink-0",fill:"none",stroke:"currentColor",viewBox:"0 0 24 24",children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:2,d:"M5 13l4 4L19 7"})}),e.jsx("span",{className:"text-white text-sm font-medium",children:pe})]})}),ue&&e.jsx("div",{className:"fixed top-4 left-1/2 transform -translate-x-1/2 z-[9998] transition-all duration-300",children:e.jsxs("div",{className:`rounded-lg px-6 py-3 flex items-center gap-3 shadow-lg ${z==="Bookmarked!"?"bg-green-500":z==="Bookmark removed"?"bg-gray-500":"bg-red-500"}`,children:[e.jsx("svg",{className:"w-5 h-5 flex-shrink-0 text-white",fill:"none",stroke:"currentColor",viewBox:"0 0 24 24",children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:2,d:"M5 13l4 4L19 7"})}),e.jsx("span",{className:"text-white text-sm font-medium",children:z})]})})]})}export{Le as ComposeCard};