import{r,a as ye}from"./vendor-react-DP_h-t3b.js";import{c as Y,s as D,a as we,j as t}from"./index-5JG4S71q.js";import{X as ke,ah as G,n as Se,ai as W,f as je}from"./vendor-ui-B25LK1Jt.js";function Pe(a){const{profile:p}=Y(a),x=(p==null?void 0:p.tier)==="pro"||(p==null?void 0:p.tier)==="xpert",[B,I]=r.useState([]),[P,j]=r.useState(new Set),[b,N]=r.useState(!0),[y,L]=r.useState(null),h=r.useCallback(async()=>{if(!(!a||!x))try{N(!0),L(null);const{data:o,error:i}=await D.from("bookmarks").select("*").eq("user_id",a.id).order("created_at",{ascending:!1});if(i)throw i;I(o||[]);const m=(o||[]).filter(g=>g.source_id!=null).map(g=>g.source_id),c=new Set(m);j(c)}catch(o){L(o instanceof Error?o.message:"Failed to fetch bookmarks")}finally{N(!1)}},[a,x]);r.useEffect(()=>{if(!a||!x){I([]),j(new Set),N(!1);return}h()},[a,x,h]),r.useEffect(()=>{if(!a||!x)return;const o=()=>{document.visibilityState==="visible"&&h()},i=()=>{h()};return document.addEventListener("visibilitychange",o),window.addEventListener("bookmark-sync",i),()=>{document.removeEventListener("visibilitychange",o),window.removeEventListener("bookmark-sync",i)}},[a,x,h]);const A=r.useCallback(async(o,i="💠Bookmark",m="custom",c,g,w,E,U,M,H,O)=>{if(!a||!x)return!1;try{c&&j(z=>new Set(z).add(c));const{data:k,error:S}=await D.from("bookmarks").insert({user_id:a.id,post_content:o,post_tag:i,source_type:m,source_id:c,generated_for_username:g,original_created_at:w,author_profile_picture:E,voice_display_name:U,voice_username:M,voice_is_verified:H,image_url:O}).select().single();if(S)throw S;return await h(),!0}catch{return c&&j(S=>{const z=new Set(S);return z.delete(c),z}),!1}},[a,x,h]),T=r.useCallback(async o=>{if(!a||!x)return!1;try{j(c=>{const g=new Set(c);return g.delete(o),g});const{data:i,error:m}=await D.from("bookmarks").delete().eq("user_id",a.id).eq("source_id",o).select();if(m)throw m;return!i||i.length===0||await h(),!0}catch{return j(m=>{const c=new Set(m);return c.add(o),c}),!1}},[a,x,h]),F=r.useCallback(o=>P.has(o),[P]),_=r.useCallback(async()=>{if(a)try{const o=localStorage.getItem("bookmarked_posts_data");if(!o)return;const i=JSON.parse(o),m=Object.entries(i);if(m.length===0)return;for(const[c,g]of m){const w=g;await A(w.post_content,w.post_tag||"💠Bookmark",w.user_id?"daily_post":"compose",c)}localStorage.removeItem("bookmarked_posts_data")}catch{}},[a,A]);return r.useEffect(()=>{a&&B.length===0&&!b&&_()},[a,B.length,b,_]),{bookmarks:B,loading:b,error:y,addBookmark:A,removeBookmark:T,isBookmarked:F,refresh:h,canUseBookmarks:x}}function Te({isOpen:a,onClose:p,postText:x,onSchedule:B,onPostNow:I,onSuccess:P,imageUrl:j}){const[b,N]=r.useState("queue"),[y,L]=r.useState(null),[h,A]=r.useState([]),[T,F]=r.useState(""),[_,o]=r.useState(""),[i,m]=r.useState(!1),[c,g]=r.useState(!1),[w,E]=r.useState(null),[U,M]=r.useState(!0),[H,O]=r.useState([]),[k,S]=r.useState(new Set),[z,Z]=r.useState(!1),{user:f,signInWithXOAuth2:Ne}=we(),{profile:$}=Y(f),ee=Intl.DateTimeFormat().resolvedOptions().timeZone;r.useEffect(()=>{a&&f&&(ae(),te(),N("queue"),se())},[a,f]),r.useEffect(()=>{if(a&&b==="custom"){const e=new Date;e.setMinutes(e.getMinutes()+15);const s=e.getFullYear(),l=String(e.getMonth()+1).padStart(2,"0"),d=String(e.getDate()).padStart(2,"0");F(`${s}-${l}-${d}`);const u=String(e.getHours()).padStart(2,"0"),n=String(Math.ceil(e.getMinutes()/15)*15).padStart(2,"0");o(`${u}:${n}`)}},[a,b]),r.useEffect(()=>{async function e(){if(!f||!a){M(!1);return}M(!0);try{const{data:s,error:l}=await D.from("social_connections").select("platform").eq("user_id",f.id).eq("is_active",!0);if(l)E(!1);else{const d=(s||[]).some(u=>u.platform!=="google");E(d)}}catch{E(!1)}finally{M(!1)}}e()},[f,a]);const te=async()=>{if(f)try{const{data:e,error:s}=await D.from("social_connections").select("platform").eq("user_id",f.id);if(s)throw s;const l=["x","linkedin","threads","bluesky"],d=new Set((e||[]).map(n=>n.platform)),u=l.map(n=>({platform:n,connected:d.has(n)}));if(O(u),!z){const n=new Set(u.filter(v=>v.connected).map(v=>v.platform));n.size>0&&S(n)}}catch{O(["x","linkedin","threads","bluesky"].map(l=>({platform:l,connected:!1})))}},se=()=>{if(f)try{const e=localStorage.getItem(`platform_preferences_${f.id}`);if(e){const s=JSON.parse(e);S(new Set(s)),Z(!0)}}catch{}},re=e=>{if(f)try{localStorage.setItem(`platform_preferences_${f.id}`,JSON.stringify(Array.from(e)))}catch{}},ae=async()=>{if(f)try{const{data:e,error:s}=await D.from("queue_schedule_settings").select("schedule_data").eq("user_id",f.id).maybeSingle();if(s)throw s;let l=[];e!=null&&e.schedule_data?l=e.schedule_data:l=[{time:"09:00",days:{sun:!1,mon:!0,tue:!0,wed:!0,thu:!0,fri:!0,sat:!1}},{time:"13:00",days:{sun:!1,mon:!0,tue:!0,wed:!0,thu:!0,fri:!0,sat:!1}}];const{data:d}=await D.from("scheduled_posts").select("scheduled_time").eq("status","pending"),u=new Set((d||[]).map(v=>new Date(v.scheduled_time).getTime())),n=oe(l,u,10);A(n),n.length>0&&L(n[0])}catch{}},oe=(e,s,l)=>{const d=[],u=new Date,n=["sun","mon","tue","wed","thu","fri","sat"];let v=0;const ue=90;for(;d.length<l&&v<ue;){const C=new Date(u);C.setDate(C.getDate()+v);const me=C.getDay(),fe=n[me],xe=e.filter(Q=>Q.days[fe]);for(const Q of xe){const[q,X]=Q.time.split(":").map(Number),V=new Date(C);if(V.setHours(q,X,0,0),V>u&&!s.has(V.getTime())){const he=q===0?12:q>12?q-12:q,ge=q<12?"AM":"PM",be=`${he}:${X.toString().padStart(2,"0")} ${ge}`;let J;const pe=C.toDateString()===u.toDateString(),R=new Date(u);R.setDate(R.getDate()+1);const ve=C.toDateString()===R.toDateString();if(pe?J="Today":ve?J="Tomorrow":J=C.toLocaleDateString("en-US",{weekday:"short",month:"short",day:"numeric"}),d.push({date:V,time:Q.time,displayDate:J,displayTime:be}),d.length>=l)break}}v++}return d},ne=async()=>{let e;if(b==="queue"){if(!y)return;e=y.date}else{if(!T||!_)return;if(e=new Date(`${T}T${_}`),e<=new Date){alert("Please select a future date and time");return}}m(!0);try{const s=Array.from(k);await B(e.toISOString(),s);const l=e.toLocaleDateString("en-US",{month:"short",day:"numeric",year:"numeric"}),d=e.toLocaleTimeString("en-US",{hour:"numeric",minute:"2-digit"});P==null||P(`Post successfully scheduled for ${l} at ${d}`),p()}catch{alert("Failed to schedule post. Please try again.")}finally{m(!1)}},le=()=>new Date().toISOString().split("T")[0],ie=()=>{const e=new Date;return e.setDate(e.getDate()+90),e.toISOString().split("T")[0]},ce=()=>{window.location.href="/connections"},K=x.replace(/<span[^>]*>/g,"").replace(/<\/span>/g,"");if(K.length>100&&K.substring(0,100)+"",!a)return null;const de=t.jsx("div",{className:"fixed inset-0 z-[10002] flex items-center justify-center p-4 bg-black/50 backdrop-blur-sm",children:t.jsxs("div",{className:"bg-[var(--surface)] rounded-2xl border border-[var(--border)] max-w-md w-full p-6 shadow-xl max-h-[90vh] overflow-y-auto",children:[t.jsxs("div",{className:"flex items-center justify-between mb-6",children:[t.jsx("h2",{className:"text-xl font-bold text-[var(--text)]",style:{fontFamily:"KaioBold, sans-serif"},children:"Schedule Post"}),t.jsx("button",{onClick:p,className:"text-[var(--muted)] hover:text-[var(--text)] transition-colors","aria-label":"Close",children:t.jsx(ke,{size:20})})]}),!U&&w===!1&&($==null?void 0:$.tier)!=="free"&&t.jsx("div",{className:"mb-6 p-4 bg-yellow-500/10 border border-yellow-500/30 rounded-lg",children:t.jsxs("div",{className:"flex items-start gap-3",children:[t.jsx(G,{size:20,className:"text-yellow-500 flex-shrink-0 mt-0.5"}),t.jsxs("div",{className:"flex-1",children:[t.jsx("p",{className:"text-sm text-[var(--text)] font-medium mb-2",children:"Permission Required"}),t.jsx("p",{className:"text-xs text-[var(--muted)] mb-3",children:"To schedule or share posts, you need to grant posting permissions."}),t.jsxs("button",{onClick:ce,className:"w-full flex items-center justify-center gap-2 px-4 py-2 bg-black text-white hover:bg-gray-800 rounded-lg font-medium transition-all duration-200",children:[t.jsx(G,{size:16}),"Connect an Account"]})]})]})}),t.jsxs("div",{className:"mb-4",children:[t.jsxs("div",{className:"flex gap-1.5 mb-3",children:[t.jsx("button",{onClick:()=>N("queue"),className:`flex-1 px-2.5 py-1.5 rounded-full text-xs font-medium transition-all ${b==="queue"?"bg-blue-500 text-white":"bg-[var(--bg)] text-[var(--text)] hover:bg-[var(--bg)]/80 border border-[var(--border)]"}`,children:"Next Available"}),t.jsx("button",{onClick:()=>N("custom"),className:`flex-1 px-2.5 py-1.5 rounded-full text-xs font-medium transition-all ${b==="custom"?"bg-blue-500 text-white":"bg-[var(--bg)] text-[var(--text)] hover:bg-[var(--bg)]/80 border border-[var(--border)]"}`,children:"Custom Time"})]}),t.jsx("div",{className:"border-t border-[var(--border)] pt-4 mb-3"}),t.jsxs("label",{className:"flex items-center justify-between text-xs font-medium text-[var(--text)] mb-2",children:[t.jsxs("span",{className:"flex items-center gap-1.5",children:[t.jsx(Se,{size:14}),"Schedule time"]}),t.jsx("span",{className:"text-xs text-[var(--muted)] font-normal",children:ee})]}),b==="queue"?t.jsx("div",{className:"space-y-1.5 max-h-[100px] overflow-y-auto mb-4",children:h.length===0?t.jsx("div",{className:"p-3 text-center text-xs text-[var(--muted)]",children:"No available queue slots. Create a schedule in Queue settings."}):h.map((e,s)=>t.jsxs("button",{onClick:()=>L(e),className:`w-full flex items-center justify-between px-3 py-2 rounded-full text-left transition-all ${(y==null?void 0:y.date.getTime())===e.date.getTime()?"bg-blue-500 text-white":"bg-[var(--bg)] text-[var(--text)] hover:bg-[var(--bg)]/80 border border-[var(--border)]"}`,children:[t.jsx("span",{className:"font-medium text-xs",children:e.displayDate}),t.jsx("span",{className:"text-xs",children:e.displayTime})]},s))}):t.jsxs("div",{className:"flex gap-2",children:[t.jsx("input",{type:"date",value:T,onChange:e=>F(e.target.value),min:le(),max:ie(),className:"flex-1 px-3 py-1.5 bg-[var(--bg)] border border-[var(--border)] rounded-full text-xs text-[var(--text)] focus:outline-none focus:ring-2 focus:ring-blue-500"}),t.jsx("input",{type:"time",value:_,onChange:e=>o(e.target.value),step:"900",className:"flex-1 px-3 py-1.5 bg-[var(--bg)] border border-[var(--border)] rounded-full text-xs text-[var(--text)] focus:outline-none focus:ring-2 focus:ring-blue-500"})]})]}),t.jsx("div",{className:"border-t border-[var(--border)] pt-4 mb-3"}),t.jsxs("div",{className:"mb-6",children:[t.jsxs("div",{className:"flex items-center justify-between mb-2",children:[t.jsxs("label",{className:"flex items-center gap-1.5 text-xs font-medium text-[var(--text)]",children:[t.jsx(W,{size:14}),"Schedule for"]}),t.jsx("a",{href:"/connections",className:"text-xs text-blue-500 hover:text-blue-600 font-medium transition-colors",children:"Add New Connections"})]}),t.jsx("div",{className:"flex gap-2",children:H.map(e=>{const s=k.has(e.platform),l=e.platform==="x"?s?t.jsx("svg",{viewBox:"0 0 24 24",fill:"currentColor",className:"w-full h-full text-white",children:t.jsx("path",{d:"M18.244 2.25h3.308l-7.227 8.26 8.502 11.24H16.17l-5.214-6.817L4.99 21.75H1.68l7.73-8.835L1.254 2.25H8.08l4.713 6.231zm-1.161 17.52h1.833L7.084 4.126H5.117z"})}):t.jsx("svg",{viewBox:"0 0 24 24",fill:"currentColor",className:"w-full h-full text-gray-600",children:t.jsx("path",{d:"M18.244 2.25h3.308l-7.227 8.26 8.502 11.24H16.17l-5.214-6.817L4.99 21.75H1.68l7.73-8.835L1.254 2.25H8.08l4.713 6.231zm-1.161 17.52h1.833L7.084 4.126H5.117z"})}):e.platform==="linkedin"?t.jsx("svg",{viewBox:"0 0 24 24",fill:"currentColor",className:"w-full h-full",children:t.jsx("path",{d:"M20.447 20.452h-3.554v-5.569c0-1.328-.027-3.037-1.852-3.037-1.853 0-2.136 1.445-2.136 2.939v5.667H9.351V9h3.414v1.561h.046c.477-.9 1.637-1.85 3.37-1.85 3.601 0 4.267 2.37 4.267 5.455v6.286zM5.337 7.433c-1.144 0-2.063-.926-2.063-2.065 0-1.138.92-2.063 2.063-2.063 1.14 0 2.064.925 2.064 2.063 0 1.139-.925 2.065-2.064 2.065zm1.782 13.019H3.555V9h3.564v11.452zM22.225 0H1.771C.792 0 0 .774 0 1.729v20.542C0 23.227.792 24 1.771 24h20.451C23.2 24 24 23.227 24 22.271V1.729C24 .774 23.2 0 22.222 0h.003z"})}):e.platform==="threads"?t.jsx("img",{src:"/threads_logo.png",alt:"Threads",className:"w-full h-full object-contain"}):e.platform==="bluesky"?t.jsx("svg",{viewBox:"0 0 24 24",fill:"currentColor",className:"w-full h-full",children:t.jsx("path",{d:"M12 10.8c-1.087-2.114-4.046-6.053-6.798-7.995C2.566.944 1.561 1.266.902 1.565.139 1.908 0 3.08 0 3.768c0 .69.378 5.65.624 6.479.815 2.736 3.713 3.66 6.383 3.364.136-.02.275-.038.416-.054-.138.022-.276.04-.416.054-3.912.58-7.387 2.005-2.83 7.078 5.013 5.19 6.87-1.113 7.823-4.308.953 3.195 2.05 9.271 7.733 4.308 4.267-4.308 1.172-6.498-2.74-7.078a8.741 8.741 0 0 1-.416-.054c.14.016.28.034.416.054 2.67.297 5.568-.628 6.383-3.364.246-.828.624-5.79.624-6.478 0-.69-.139-1.861-.902-2.206-.659-.298-1.664-.62-4.3 1.24C16.046 4.748 13.087 8.687 12 10.8z"})}):null,d=()=>{if(!e.connected||!s)return"";switch(e.platform){case"x":return"bg-black";default:return""}},u=()=>{if(!e.connected||!s)return"text-gray-400";switch(e.platform){case"x":return"";case"linkedin":return"text-[#0A66C2]";case"threads":return"text-black";case"bluesky":return"text-[#1185FE]";default:return"text-gray-400"}};return t.jsx("button",{onClick:()=>{if(e.connected){const n=new Set(k);s?n.delete(e.platform):n.add(e.platform),S(n),re(n)}},disabled:!e.connected,className:`w-10 h-10 flex items-center justify-center rounded border transition-all ${e.connected&&s?`border-[var(--border)] ${d()||"bg-blue-50 dark:bg-blue-950/30"}`:e.connected?"border-[var(--border)] bg-[var(--bg)] hover:bg-[var(--bg)]/80":"border-[var(--border)] bg-[var(--bg)] opacity-50 cursor-not-allowed"}`,title:e.connected?e.platform.charAt(0).toUpperCase()+e.platform.slice(1):`Connect ${e.platform.charAt(0).toUpperCase()+e.platform.slice(1)} to post`,children:t.jsx("div",{className:`w-8 h-8 flex items-center justify-center ${u()}`,children:l})},e.platform)})})]}),t.jsxs("div",{className:"flex gap-2",children:[t.jsxs("button",{onClick:ne,disabled:i||b==="queue"&&!y||b==="custom"&&(!T||!_)||($==null?void 0:$.tier)==="free"||w===!1||U||H.every(e=>!e.connected)||k.size===0,className:"flex-1 flex items-center justify-center gap-1.5 px-4 py-2.5 bg-blue-500 text-white hover:bg-blue-600 disabled:opacity-50 disabled:cursor-not-allowed rounded-full font-medium transition-all duration-200 text-sm",children:[t.jsx(je,{size:14}),i?"Adding...":"Add to Queue"]}),t.jsx("button",{onClick:async()=>{g(!0);try{await I(Array.from(k))}finally{g(!1)}},disabled:H.every(e=>!e.connected)||k.size===0||c,className:"flex-1 flex items-center justify-center gap-1.5 px-4 py-2.5 bg-black text-white hover:bg-gray-800 disabled:opacity-50 disabled:cursor-not-allowed rounded-full font-medium transition-all duration-200 text-sm",children:c?t.jsxs(t.Fragment,{children:[t.jsx(W,{size:14,className:"animate-pulse"}),t.jsx("span",{children:"Posting..."})]}):t.jsxs(t.Fragment,{children:[t.jsx(W,{size:14}),t.jsx("span",{children:"Post Now"})]})})]})]})});return ye.createPortal(de,document.body)}export{Te as S,Pe as u};