1import{j as e,u as K,L as mt,e as ht,P as re,G as ae,F as B,c as X,h as pt,V as ft}from"./ui-vendor-DeaCpvuz.js";import{r as n}from"./react-vendor-BwON9l4R.js";import{D as fe,e as xe,f as ge,g as be,d as Ce,I as xt,n as gt,B as G,a as Pe,s as E,u as R,L as bt,h as wt,i as St,j as vt,k as jt}from"./index-Kz-5MvZ9.js";import{C as pe}from"./card-B7gy2v7u.js";import{B as te}from"./badge-Dht8Lt2r.js";import{f as _e,t as ee,V as yt,G as Dt,a as Nt}from"./GenerateVideoDialog-C6ZYMIAb.js";import{C as Ct}from"./clock-CBETRLQI.js";import{T as Ee}from"./trending-up-B__FCMVO.js";import{V as Tt}from"./video-BZJbYVPk.js";import{g as Pt}from"./dashboardDataCache-SNkqqi8U.js";import{P as _t}from"./plus-BFZk4Bhp.js";import{C as Et,a as At}from"./chevron-right-Dp4t2JdI.js";import{C as Rt}from"./calendar-BpG5PemG.js";import"./supabase-vendor-eEOYocAC.js";import"./query-vendor-DsJQQYkK.js";import"./alert-dialog-Bzgk2_9Q.js";import"./trash-2-Be1tE6R7.js";import"./circle-alert-kCskUU8Q.js";import"./check-CIuB_zKM.js";import"./sparkles-BdSrl3dE.js";import"./UpgradeModal-ClMfV0NO.js";import"./alert-wjtFgdqf.js";import"./lightbulb-D41pt8VC.js";function Lt({open:t,onOpenChange:o,date:r,onConfirm:i}){const l=()=>{const m=new Date;return new Date(r).toDateString()===m.toDateString()?(m.setHours(m.getHours()+1),`${String(m.getHours()).padStart(2,"0")}:${String(m.getMinutes()).padStart(2,"0")}`):"12:00"},[a,c]=n.useState(l());n.useEffect(()=>{t&&c(l())},[t,r]);const u=()=>{const[m,p]=a.split(":").map(Number),b=new Date(r);b.setHours(m,p,0,0),!(b<new Date)&&(i(a),o(!1))},h=()=>{const[m,p]=a.split(":").map(Number),b=new Date(r);return b.setHours(m,p,0,0),b<new Date};return e.jsx(fe,{open:t,onOpenChange:o,children:e.jsxs(xe,{className:"sm:max-w-md",children:[e.jsx(ge,{children:e.jsx(be,{children:"Schedule Time"})}),e.jsxs("div",{className:"space-y-4 py-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(Ce,{children:"Date"}),e.jsx("div",{className:"flex items-center gap-2 p-3 bg-muted rounded-lg",children:e.jsx("span",{className:"font-medium",children:_e(r,"PPPP")})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(Ce,{htmlFor:"time",children:"Time"}),e.jsxs("div",{className:"relative",children:[e.jsx(Ct,{className:"absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground"}),e.jsx(xt,{id:"time",type:"time",value:a,onChange:m=>c(m.target.value),className:"pl-10"})]}),h()&&e.jsx("p",{className:"text-sm text-destructive",children:"Cannot schedule in the past. Please select a future time."})]})]}),e.jsxs(gt,{children:[e.jsx(G,{variant:"outline",onClick:()=>o(!1),children:"Cancel"}),e.jsx(G,{onClick:u,disabled:h(),children:"Schedule"})]})]})})}function kt(t,o){return n.useReducer((r,i)=>o[r][i]??r,t)}var we="ScrollArea",[Ae]=ht(we),[Vt,A]=Ae(we),Re=n.forwardRef((t,o)=>{const{__scopeScrollArea:r,type:i="hover",dir:l,scrollHideDelay:a=600,...c}=t,[u,h]=n.useState(null),[m,p]=n.useState(null),[b,x]=n.useState(null),[v,_]=n.useState(null),[C,k]=n.useState(null),[V,Y]=n.useState(0),[z,F]=n.useState(0),[$,I]=n.useState(!1),[Z,W]=n.useState(!1),D=K(o,P=>h(P)),T=mt(l);return e.jsx(Vt,{scope:r,type:i,dir:T,scrollHideDelay:a,scrollArea:u,viewport:m,onViewportChange:p,content:b,onContentChange:x,scrollbarX:v,onScrollbarXChange:_,scrollbarXEnabled:$,onScrollbarXEnabledChange:I,scrollbarY:C,onScrollbarYChange:k,scrollbarYEnabled:Z,onScrollbarYEnabledChange:W,onCornerWidthChange:Y,onCornerHeightChange:F,children:e.jsx(re.div,{dir:T,...c,ref:D,style:{position:"relative","--radix-scroll-area-corner-width":V+"px","--radix-scroll-area-corner-height":z+"px",...t.style}})})});Re.displayName=we;var Le="ScrollAreaViewport",ke=n.forwardRef((t,o)=>{const{__scopeScrollArea:r,children:i,nonce:l,...a}=t,c=A(Le,r),u=n.useRef(null),h=K(o,u,c.onViewportChange);return e.jsxs(e.Fragment,{children:[e.jsx("style",{dangerouslySetInnerHTML:{__html:"[data-radix-scroll-area-viewport]{scrollbar-width:none;-ms-overflow-style:none;-webkit-overflow-scrolling:touch;}[data-radix-scroll-area-viewport]::-webkit-scrollbar{display:none}"},nonce:l}),e.jsx(re.div,{"data-radix-scroll-area-viewport":"",...a,ref:h,style:{overflowX:c.scrollbarXEnabled?"scroll":"hidden",overflowY:c.scrollbarYEnabled?"scroll":"hidden",...t.style},children:e.jsx("div",{ref:c.onContentChange,style:{minWidth:"100%",display:"table"},children:i})})]})});ke.displayName=Le;var L="ScrollAreaScrollbar",Se=n.forwardRef((t,o)=>{const{forceMount:r,...i}=t,l=A(L,t.__scopeScrollArea),{onScrollbarXEnabledChange:a,onScrollbarYEnabledChange:c}=l,u=t.orientation==="horizontal";return n.useEffect(()=>(u?a(!0):c(!0),()=>{u?a(!1):c(!1)}),[u,a,c]),l.type==="hover"?e.jsx(Ft,{...i,ref:o,forceMount:r}):l.type==="scroll"?e.jsx(It,{...i,ref:o,forceMount:r}):l.type==="auto"?e.jsx(Ve,{...i,ref:o,forceMount:r}):l.type==="always"?e.jsx(ve,{...i,ref:o}):null});Se.displayName=L;var Ft=n.forwardRef((t,o)=>{const{forceMount:r,...i}=t,l=A(L,t.__scopeScrollArea),[a,c]=n.useState(!1);return n.useEffect(()=>{const u=l.scrollArea;let h=0;if(u){const m=()=>{window.clearTimeout(h),c(!0)},p=()=>{h=window.setTimeout(()=>c(!1),l.scrollHideDelay)};return u.addEventListener("pointerenter",m),u.addEventListener("pointerleave",p),()=>{window.clearTimeout(h),u.removeEventListener("pointerenter",m),u.removeEventListener("pointerleave",p)}}},[l.scrollArea,l.scrollHideDelay]),e.jsx(ae,{present:r||a,children:e.jsx(Ve,{"data-state":a?"visible":"hidden",...i,ref:o})})}),It=n.forwardRef((t,o)=>{const{forceMount:r,...i}=t,l=A(L,t.__scopeScrollArea),a=t.orientation==="horizontal",c=ce(()=>h("SCROLL_END"),100),[u,h]=kt("hidden",{hidden:{SCROLL:"scrolling"},scrolling:{SCROLL_END:"idle",POINTER_ENTER:"interacting"},interacting:{SCROLL:"interacting",POINTER_LEAVE:"idle"},idle:{HIDE:"hidden",SCROLL:"scrolling",POINTER_ENTER:"interacting"}});return n.useEffect(()=>{if(u==="idle"){const m=window.setTimeout(()=>h("HIDE"),l.scrollHideDelay);return()=>window.clearTimeout(m)}},[u,l.scrollHideDelay,h]),n.useEffect(()=>{const m=l.viewport,p=a?"scrollLeft":"scrollTop";if(m){let b=m[p];const x=()=>{const v=m[p];b!==v&&(h("SCROLL"),c()),b=v};return m.addEventListener("scroll",x),()=>m.removeEventListener("scroll",x)}},[l.viewport,a,h,c]),e.jsx(ae,{present:r||u!=="hidden",children:e.jsx(ve,{"data-state":u==="hidden"?"hidden":"visible",...i,ref:o,onPointerEnter:B(t.onPointerEnter,()=>h("POINTER_ENTER")),onPointerLeave:B(t.onPointerLeave,()=>h("POINTER_LEAVE"))})})}),Ve=n.forwardRef((t,o)=>{const r=A(L,t.__scopeScrollArea),{forceMount:i,...l}=t,[a,c]=n.useState(!1),u=t.orientation==="horizontal",h=ce(()=>{if(r.viewport){const m=r.viewport.offsetWidth<r.viewport.scrollWidth,p=r.viewport.offsetHeight<r.viewport.scrollHeight;c(u?m:p)}},10);return J(r.viewport,h),J(r.content,h),e.jsx(ae,{present:i||a,children:e.jsx(ve,{"data-state":a?"visible":"hidden",...l,ref:o})})}),ve=n.forwardRef((t,o)=>{const{orientation:r="vertical",...i}=t,l=A(L,t.__scopeScrollArea),a=n.useRef(null),c=n.useRef(0),[u,h]=n.useState({content:0,viewport:0,scrollbar:{size:0,paddingStart:0,paddingEnd:0}}),m=ze(u.viewport,u.content),p={...i,sizes:u,onSizesChange:h,hasThumb:m>0&&m<1,onThumbChange:x=>a.current=x,onThumbPointerUp:()=>c.current=0,onThumbPointerDown:x=>c.current=x};function b(x,v){return Yt(x,c.current,u,v)}return r==="horizontal"?e.jsx(Ut,{...p,ref:o,onThumbPositionChange:()=>{if(l.viewport&&a.current){const x=l.viewport.scrollLeft,v=Te(x,u,l.dir);a.current.style.transform=`translate3d(${v}px, 0, 0)`}}
vendor: 3,657 bytes, line 1
1,onWheelScroll:x=>{l.viewport&&(l.viewport.scrollLeft=x)},onDragScroll:x=>{l.viewport&&(l.viewport.scrollLeft=b(x,l.dir))}}):r==="vertical"?e.jsx(Ot,{...p,ref:o,onThumbPositionChange:()=>{if(l.viewport&&a.current){const x=l.viewport.scrollTop,v=Te(x,u);a.current.style.transform=`translate3d(0, ${v}px, 0)`}},onWheelScroll:x=>{l.viewport&&(l.viewport.scrollTop=x)},onDragScroll:x=>{l.viewport&&(l.viewport.scrollTop=b(x))}}):null}),Ut=n.forwardRef((t,o)=>{const{sizes:r,onSizesChange:i,...l}=t,a=A(L,t.__scopeScrollArea),[c,u]=n.useState(),h=n.useRef(null),m=K(o,h,a.onScrollbarXChange);return n.useEffect(()=>{h.current&&u(getComputedStyle(h.current))},[h]),e.jsx(Ie,{"data-orientation":"horizontal",...l,ref:m,sizes:r,style:{bottom:0,left:a.dir==="rtl"?"var(--radix-scroll-area-corner-width)":0,right:a.dir==="ltr"?"var(--radix-scroll-area-corner-width)":0,"--radix-scroll-area-thumb-width":ie(r)+"px",...t.style},onThumbPointerDown:p=>t.onThumbPointerDown(p.x),onDragScroll:p=>t.onDragScroll(p.x),onWheelScroll:(p,b)=>{if(a.viewport){const x=a.viewport.scrollLeft+p.deltaX;t.onWheelScroll(x),Me(x,b)&&p.preventDefault()}},onResize:()=>{h.current&&a.viewport&&c&&i({content:a.viewport.scrollWidth,viewport:a.viewport.offsetWidth,scrollbar:{size:h.current.clientWidth,paddingStart:le(c.paddingLeft),paddingEnd:le(c.paddingRight)}})}})}),Ot=n.forwardRef((t,o)=>{const{sizes:r,onSizesChange:i,...l}=t,a=A(L,t.__scopeScrollArea),[c,u]=n.useState(),h=n.useRef(null),m=K(o,h,a.onScrollbarYChange);return n.useEffect(()=>{h.current&&u(getComputedStyle(h.current))},[h]),e.jsx(Ie,{"data-orientation":"vertical",...l,ref:m,sizes:r,style:{top:0,right:a.dir==="ltr"?0:void 0,left:a.dir==="rtl"?0:void 0,bottom:"var(--radix-scroll-area-corner-height)","--radix-scroll-area-thumb-height":ie(r)+"px",...t.style},onThumbPointerDown:p=>t.onThumbPointerDown(p.y),onDragScroll:p=>t.onDragScroll(p.y),onWheelScroll:(p,b)=>{if(a.viewport){const x=a.viewport.scrollTop+p.deltaY;t.onWheelScroll(x),Me(x,b)&&p.preventDefault()}},onResize:()=>{h.current&&a.viewport&&c&&i({content:a.viewport.scrollHeight,viewport:a.viewport.offsetHeight,scrollbar:{size:h.current.clientHeight,paddingStart:le(c.paddingTop),paddingEnd:le(c.paddingBottom)}})}})}),[zt,Fe]=Ae(L),Ie=n.forwardRef((t,o)=>{const{__scopeScrollArea:r,sizes:i,hasThumb:l,onThumbChange:a,onThumbPointerUp:c,onThumbPointerDown:u,onThumbPositionChange:h,onDragScroll:m,onWheelScroll:p,onResize:b,...x}=t,v=A(L,r),[_,C]=n.useState(null),k=K(o,D=>C(D)),V=n.useRef(null),Y=n.useRef(""),z=v.viewport,F=i.content-i.viewport,$=X(p),I=X(h),Z=ce(b,10);function W(D){if(V.current){const T=D.clientX-V.current.left,P=D.clientY-V.current.top;m({x:T,y:P})}}return n.useEffect(()=>{const D=T=>{const P=T.target;_?.contains(P)&&$(T,F)};return document.addEventListener("wheel",D,{passive:!1}),()=>document.removeEventListener("wheel",D,{passive:!1})},[z,_,F,$]),n.useEffect(I,[i,I]),J(_,Z),J(v.content,Z),e.jsx(zt,{scope:r,scrollbar:_,hasThumb:l,onThumbChange:X(a),onThumbPointerUp:X(c),onThumbPositionChange:I,onThumbPointerDown:X(u),children:e.jsx(re.div,{...x,ref:k,style:{position:"absolute",...x.style},onPointerDown:B(t.onPointerDown,D=>{D.button===0&&(D.target.setPointerCapture(D.pointerId),V.current=_.getBoundingClientRect(),Y.current=document.body.style.webkitUserSelect,document.body.style.webkitUserSelect="none",v.viewport&&(v.viewport.style.scrollBehavior="auto"),W(D))}),onPointerMove:B(t.onPointerMove,W),onPointerUp:B(t.onPointerUp,D=>{const T=D.target;T.hasPointerCapture(D.pointerId)&&T.releasePointerCapture(D.pointerId),document.body.style.webkitUserSelect=Y.current,v.viewport&&(v.viewport.style.scrollBehavior=""),
1V.current=null})})})}),ne="ScrollAreaThumb",Ue=n.forwardRef((t,o)=>{const{forceMount:r,...i}=t,l=Fe(ne,t.__scopeScrollArea);return e.jsx(ae,{present:r||l.hasThumb,children:e.jsx(Ht,{ref:o,...i})})}),Ht=n.forwardRef((t,o)=>{const{__scopeScrollArea:r,style:i,...l}=t,a=A(ne,r),c=Fe(ne,r),{onThumbPositionChange:u}=c,h=K(o,b=>c.onThumbChange(b)),m=n.useRef(void 0),p=ce(()=>{m.current&&(m.current(),m.current=void 0)},100);return n.useEffect(()=>{const b=a.viewport;if(b){const x=()=>{if(p(),!m.current){const v=Wt(b,u);m.current=v,u()}};return u(),b.addEventListener("scroll",x),()=>b.removeEventListener("scroll",x)}},[a.viewport,p,u]),e.jsx(re.div,{"data-state":c.hasThumb?"visible":"hidden",...l,ref:h,style:{width:"var(--radix-scroll-area-thumb-width)",height:"var(--radix-scroll-area-thumb-height)",...i},onPointerDownCapture:B(t.onPointerDownCapture,b=>{const v=b.target.getBoundingClientRect(),_=b.clientX-v.left,C=b.clientY-v.top;c.onThumbPointerDown({x:_,y:C})}),onPointerUp:B(t.onPointerUp,c.onThumbPointerUp)})});Ue.displayName=ne;var je="ScrollAreaCorner",Oe=n.forwardRef((t,o)=>{const r=A(je,t.__scopeScrollArea),i=!!(r.scrollbarX&&r.scrollbarY);return r.type!=="scroll"&&i?e.jsx(Mt,{...t,ref:o}):null});Oe.displayName=je;var Mt=n.forwardRef((t,o)=>{const{__scopeScrollArea:r,...i}=t,l=A(je,r),[a,c]=n.useState(0),[u,h]=n.useState(0),m=!!(a&&u);return J(l.scrollbarX,()=>{const p=l.scrollbarX?.offsetHeight||0;l.onCornerHeightChange(p),h(p)}),J(l.scrollbarY,()=>{const p=l.scrollbarY?.offsetWidth||0;l.onCornerWidthChange(p),c(p)}),m?e.jsx(re.div,{...i,ref:o,style:{width:a,height:u,position:"absolute",right:l.dir==="ltr"?0:void 0,left:l.dir==="rtl"?0:void 0,bottom:0,...t.style}}):null});function le(t){return t?parseInt(t,10):0}function ze(t,o){const r=t/o;return isNaN(r)?0:r}function ie(t){const o=ze(t.viewport,t.content),r=t.scrollbar.paddingStart+t.scrollbar.paddingEnd,i=(t.scrollbar.size-r)*o;return Math.max(i,18)}function Yt(t,o,r,i="ltr"){const l=ie(r),a=l/2,c=o||a,u=l-c,h=r.scrollbar.paddingStart+c,m=r.scrollbar.size-r.scrollbar.paddingEnd-u,p=r.content-r.viewport,b=i==="ltr"?[0,p]:[p*-1,0];return He([h,m],b)(t)}function Te(t,o,r="ltr"){const i=ie(o),l=o.scrollbar.paddingStart+o.scrollbar.paddingEnd,a=o.scrollbar.size-l,c=o.content-o.viewport,u=a-i,h=r==="ltr"?[0,c]:[c*-1,0],m=ft(t,h);return He([0,c],[0,u])(m)}function He(t,o){return r=>{if(t[0]===t[1]||o[0]===o[1])return o[0];const i=(o[1]-o[0])/(t[1]-t[0]);return o[0]+i*(r-t[0])}}function Me(t,o){return t>0&&t<o}var Wt=(t,o=()=>{})=>{let r={left:t.scrollLeft,top:t.scrollTop},i=0;return function l(){const a={left:t.scrollLeft,top:t.scrollTop},c=r.left!==a.left,u=r.top!==a.top;(c||u)&&o(),r=a,i=window.requestAnimationFrame(l)}(),()=>window.cancelAnimationFrame(i)};function ce(t,o){const r=X(t),i=n.useRef(0);return n.useEffect(()=>()=>window.clearTimeout(i.current),[]),n.useCallback(()=>{window.clearTimeout(i.current),i.current=window.setTimeout(r,o)},[r,o])}function J(t,o){const r=X(o);pt(()=>{let i=0;if(t){const l=new ResizeObserver(()=>{cancelAnimationFrame(i),i=window.requestAnimationFrame(r)});return l.observe(t),()=>{window.cancelAnimationFrame(i),l.unobserve(t)}}},[t,r])}var Ye=Re,Xt=ke,Bt=Oe;const We=n.forwardRef(({className:t,children:o,...r},i)=>e.jsxs(Ye,{ref:i,className:Pe("relative overflow-hidden",t),...r,children:[e.jsx(Xt,{className:"h-full w-full rounded-[inherit]",children:o}),e.jsx(Xe,{}),e.jsx(Bt,{})]}));We.displayName=Ye.displayName;const Xe=n.forwardRef(({className:t,orientation:o="vertical",...r},i)=>e.jsx(Se,{ref:i,orientation:o,className:Pe("flex touch-none select-none transition-colors",o==="vertical"&&"h-full w-2.5 border-l border-l-transparent p-[1px]",o==="horizontal"&&"h-2.5 flex-col border-t border-t-transparent p-[1px]",t),...r,children:e.jsx(Ue,{className:"relative flex-1 rounded-full bg-border"})}));Xe.displayName=Se.displayName;function $t({open:t,onOpenChange:o,date:r,unscheduledVideos:i,onSelectVideo:l}){return e.jsx(fe,{open:t,onOpenChange:o,children:e.jsxs(xe,{className:"sm:max-w-2xl max-h-[90vh] overflow-y-auto",children:[e.jsx(ge,{children:e.jsxs(be,{className:"text-lg sm:text-xl",children:["Schedule Video for ",_e(r,"PPPP")]})}),e.jsx("div",{className:"py-4",children:i.length>0?e.jsx(We,{className:"h-[400px] pr-4",children:e.jsx("div",{className:"space-y-2",children:i.map(a=>e.jsx(pe,{onClick:()=>{l(a),o(!1)},className:"group hover-scale cursor-pointer border-2 hover:border-primary/50 transition-all p-3 sm:p-4 min-h-[80px] touch-manipulation active:scale-95",children:e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-start justify-between gap-2",children:[e.jsx("h4",{className:"font-semibold text-sm leading-tight line-clamp-2 flex-1",children:a.title}),a.virality_score&&e.jsxs(te,{variant:"secondary",className:"gap-1 shrink-0 text-xs",children:[e.jsx(Ee,{className:"h-3 w-3"}),a.virality_score,"%"]})]}),a.platforms&&a.platforms.length>0&&e.jsxs("div",{className:"flex gap-1 flex-wrap",children:[a.platforms.slice(0,3).map(c=>e.jsx(te,{variant:"outline",className:"text-[10px] px-1.5 py-0",children:c},c)),a.platforms.length>3&&e.jsxs(te,{variant:"outline",className:"text-[10px] px-1.5 py-0",children:["+",a.platforms.length-3]})]})]})},a.id))})}):e.jsxs("div",{className:"py-12 text-center",children:[e.jsx(Tt,{className:"h-10 w-10 mx-auto mb-3 text-muted-foreground
1/40"}),e.jsx("h4",{className:"font-semibold mb-1",children:"No unscheduled videos"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Create new videos to schedule them"})]})})]})})}const gr=()=>{const t=Pt("calendar:state"),[o,r]=n.useState(new Date),[i,l]=n.useState(()=>t===void 0),[a,c]=n.useState(()=>t?.scheduledVideos??[]),[u,h]=n.useState(()=>t?.autopilotPlans??[]),[m,p]=n.useState(()=>t?.unscheduledVideos??[]),[b,x]=n.useState(null),[v,_]=n.useState(!1),[C,k]=n.useState(null),[V,Y]=n.useState(!1),[z,F]=n.useState(null),[$,I]=n.useState(!1),[Z,W]=n.useState(!1),[D,T]=n.useState(null),[P,de]=n.useState(()=>t?.userTimezone??"UTC"),[oe,Be]=n.useState(null),[$e,ue]=n.useState(!1),[H,ye]=n.useState(()=>t?.autopilotEnabled??!1),[Ze,me]=n.useState(()=>t?.autopilotPlanning??!1);n.useEffect(()=>{(async()=>{try{const{data:{user:f}}=await E.auth.getUser();if(f){const{data:d}=await E.from("user_settings").select("timezone, autopilot_enabled").eq("user_id",f.id).maybeSingle();if(d?.timezone)de(d.timezone);else{const y=Intl.DateTimeFormat().resolvedOptions().timeZone;de(y||"UTC")}const g=d?.autopilot_enabled||!1;if(ye(g),g){const{count:y}=await E.from("autopilot_plans").select("*",{count:"exact",head:!0}).eq("user_id",f.id);me((y||0)===0)}}await M()}catch(f){console.error("Error loading calendar data:",f)}finally{l(!1)}})();const w=f=>{const{enabled:d,planning:g}=f.detail;ye(d),me(g||!1),g||M()},S=()=>{me(!1),M()};window.addEventListener("autopilot:toggled",w),window.addEventListener("autopilot:planning-complete",S);const j=E.channel("calendar-videos-changes").on("postgres_changes",{event:"*",schema:"public",table:"videos"},f=>{console.log("Video change detected:",f),M()}).on("postgres_changes",{event:"*",schema:"public",table:"autopilot_plans"},f=>{console.log("Autopilot plan change detected:",f),M()}).subscribe();return()=>{window.removeEventListener("autopilot:toggled",w),window.removeEventListener("autopilot:planning-complete",S),E.removeChannel(j)}},[]);const qe=()=>{const s=o.getFullYear(),w=o.getMonth(),S=new Date(s,w,1),j=new Date(s,w+1,0),f=[];for(let d=0;d<S.getDay();d++)f.push(null);for(let d=1;d<=j.getDate();d++)f.push(new Date(s,w,d));return f},Ge=s=>{if(!s)return[];const w=a.filter(f=>{if(f.status==="published"&&f.published_at){const y=new Date(f.published_at),U=ee(y,P);return U.getFullYear()===s.getFullYear()&&U.getMonth()===s.getMonth()&&U.getDate()===s.getDate()}if(!f.scheduled_for)return!1;const d=new Date(f.scheduled_for),g=ee(d,P);return g.getFullYear()===s.getFullYear()&&g.getMonth()===s.getMonth()&&g.getDate()===s.getDate()}),j=u.filter(f=>{const d=new Date(f.planned_for),g=ee(d,P),y=g.getFullYear()===s.getFullYear()&&g.getMonth()===s.getMonth()&&g.getDate()===s.getDate();return y&&console.log("Matched autopilot plan:",{planDate:f.planned_for,convertedDate:g.toString(),calendarDate:s.toString(),dayOfWeek:g.toLocaleDateString("en-US",{weekday:"long"})}),y}).map(f=>({id:f.id,title:f.prompt_context?.topic||"Autopilot Video",status:f.status,scheduled_for:f.planned_for,isAutopilotPlan:!0,planDescription:f.prompt_context?.description,failure_reason:f.failure_reason}));return[...w,...j]},Je=()=>{r(new Date(o.getFullYear(),o.getMonth()-1))},Ke=()=>{r(new Date(o.getFullYear(),o.getMonth()+1))},De=s=>{x(s),_(!0)},Qe=(s,w)=>{if(w.status==="published"||H){s.preventDefault(),R.error(H?"Autopilot is active - manual scheduling disabled":"Published videos cannot be rescheduled");return}k(w),s.dataTransfer.effectAllowed="move"},et=s=>{s.preventDefault(),s.dataTransfer.dropEffect="move"},tt=async(s,w)=>{if(s.preventDefault(),!w||!C)return;const S=new Date;S.setHours(0,0,0,0);const j=new Date(w);if(j.setHours(0,0,0,0),j<S){R.error("Cannot schedule videos in the past"),k(null);return}F(w),T(w),I(!0)},rt=s=>{if(!s||m.length===0)return;const w=new Date;w.setHours(0,0,0,0);const S=new Date(s);if(S.setHours(0,0,0,0),S<w){R.error("Cannot schedule videos in the past");return}T(s),W(!0)},ot=s=>{k(s),F(D),I(!0)},st=(s,w)=>{Be({date:s,videos:w}),ue(!0)},nt=async s=>{if(!z||!C)return;const[w,S]=s.split(":").map(Number),j=new Date(z);j.setHours(w,S,0,0);const f=Nt(j,P);if(f<new Date){R.error("Cannot schedule videos in the past"),k(null),F(null);return}const g=f.toISOString();try{let y=Array.isArray(C.platforms)?C.platforms:[];if(y.length===0){const{data:{user:O}}=await E.auth.getUser();if(!O){R.error("You must be logged in");return}const{data:q,error:Q}=await E.from("connected_accounts").select("platform").eq("user_id",O.id).eq("is_active",!0);if(Q){console.error("Error loading connected platforms:",Q),R.error("Failed to load connected platforms");return}if(y=(q||[]).map(Ne=>Ne.platform),y.length===0){R.error("No active platforms connected. Connect accounts in Settings.");return}await E.from("videos").update({platforms:y}).eq("id",C.id)}console.log("Scheduling video for:",{localDate:j.toString(),utcDate:f.toString(),isoDate:g,timezone:P,videoId:C.id,platforms:y});const{error:U}=await E.functions.invoke("schedule-video",{body:{videoId:C.id,scheduledFor:g,platforms:y}});if(U){R.error(U.message||"Failed to schedule video");return}
1R.success(`Video scheduled for ${j.toLocaleString()}`),await M(),m.find(O=>O.id===C.id)&&p(O=>O.filter(q=>q.id!==C.id))}catch(y){console.error("Error scheduling video:",y),R.error("Failed to schedule video")}finally{k(null),F(null)}},M=async()=>{try{const{data:{user:s}}=await E.auth.getUser();if(!s)return;const{data:w,error:S}=await E.functions.invoke("get-scheduled-videos");if(S)throw S;const j=w?.videos||[];c(j.filter(g=>g.scheduled_for&&g.status==="scheduled"||g.status==="published"&&g.published_at)),p(j.filter(g=>!g.scheduled_for&&g.status==="draft"));const{data:f,error:d}=await E.from("autopilot_plans").select("*").eq("user_id",s.id).order("planned_for",{ascending:!0});d||h(f||[])}catch(s){console.error("Error fetching videos:",s),R.error("Failed to load videos")}};if(i)return e.jsx(bt,{});if(Ze)return e.jsx("div",{className:"flex items-center justify-center h-[calc(100vh-200px)]",children:e.jsxs("div",{className:"text-center space-y-4",children:[e.jsx("div",{className:"animate-spin rounded-full h-12 w-12 border-b-2 border-primary mx-auto"}),e.jsx("h2",{className:"text-2xl font-bold",children:"Autopilot is preparing your calendar"}),e.jsx("p",{className:"text-muted-foreground max-w-md mx-auto",children:"AI is analyzing your channel, planning optimal content, and scheduling videos. This may take a few moments..."})]})});const lt=o.toLocaleDateString("en-US",{month:"long",year:"numeric"}),at=qe(),it=new Date().toDateString();return e.jsxs("div",{className:"space-y-8 animate-fade-in",children:[e.jsx("div",{className:"flex flex-col gap-4",children:e.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-start justify-between gap-4",children:[e.jsxs("div",{className:"flex-1",children:[e.jsx("h1",{className:"mb-2 text-2xl sm:text-3xl lg:text-4xl",children:"Content Calendar"}),e.jsx("p",{className:"text-sm sm:text-base text-muted-foreground",children:H?"Autopilot is managing your schedule - Turn off in Settings to enable manual scheduling":"Plan and schedule your video posts"})]}),e.jsxs(G,{className:"gap-2 w-full sm:w-auto min-h-[44px]",variant:"cta",size:"lg",onClick:()=>Y(!0),disabled:H,children:[e.jsx(_t,{className:"h-5 w-5"}),e.jsx("span",{className:"text-base",children:H?"Autopilot Active":"Create Video"})]})]})}),e.jsxs(pe,{className:"p-3 sm:p-4 md:p-6 border-2 overflow-hidden",children:[e.jsxs("div",{className:"flex items-center justify-between mb-4 sm:mb-6 md:mb-8",children:[e.jsx("h2",{className:"text-lg sm:text-xl md:text-2xl font-bold truncate",children:lt}),e.jsxs("div",{className:"flex items-center gap-1 sm:gap-2 shrink-0",children:[e.jsx(G,{variant:"outline",size:"icon",className:"h-9 w-9 sm:h-10 sm:w-10 touch-manipulation",onClick:Je,children:e.jsx(Et,{className:"h-4 w-4"})}),e.jsx(G,{variant:"outline",size:"sm",className:"h-9 px-3 text-xs sm:text-sm touch-manipulation",onClick:()=>r(new Date),children:"Today"}),e.jsx(G,{variant:"outline",size:"icon",className:"h-9 w-9 sm:h-10 sm:w-10 touch-manipulation",onClick:Ke,children:e.jsx(At,{className:"h-4 w-4"})})]})]}),e.jsx("div",{className:"overflow-x-auto -mx-3 px-3 sm:mx-0 sm:px-0",children:e.jsxs("div",{className:"grid grid-cols-7 gap-1 sm:gap-2 md:gap-3 min-w-[280px]",children:[["Sun","Mon","Tue","Wed","Thu","Fri","Sat"].map(s=>e.jsxs("div",{className:"text-center text-xs sm:text-sm font-semibold text-muted-foreground pb-2",children:[e.jsx("span",{className:"hidden sm:inline",children:s}),e.jsx("span",{className:"sm:hidden",children:s[0]})]},s)),at.map((s,w)=>{const S=Ge(s),j=s?.toDateString()===it,f=S.length>0;return e.jsx("div",{className:` 2 min-h-16 sm:min-h-28 p-1 sm:p-2 rounded-lg sm:rounded-xl border-2 transition-all 3 ${s?"bg-card cursor-pointer hover:border-primary/50":"bg-transparent border-transparent"} 4 ${j?"ring-1 sm:ring-2 ring-primary border-primary":""} 5 ${f?"border-primary/20":""} 6 ${C&&s?"hover:bg-primary/5":""} 7 `,onDragOver:s?et:void 0,onDrop:s?d=>tt(d,s):void 0,onClick:s&&!f&&!H?()=>rt(s):void 0,children:s&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex items-center justify-between mb-1
7sm:mb-2",children:[e.jsx("span",{className:` 8 text-xs sm:text-sm font-semibold 9 ${j?"h-5 w-5 sm:h-6 sm:w-6 rounded-full bg-primary text-primary-foreground flex items-center justify-center text-xs":""} 10 `,children:s.getDate()}),f&&e.jsx(te,{variant:"secondary",className:"h-4 sm:h-5 text-[10px] sm:text-xs px-1 sm:px-1.5",children:S.length})]}),e.jsxs("div",{className:"space-y-1 hidden sm:block",children:[S.slice(0,2).map(d=>{const g=d.isAutopilotPlan,y=d.status==="published",U=d.published_platforms||[],O=U.filter(N=>N.published).map(N=>N.platform==="youtube"?"YT":"TT").join(", "),q=d.scheduled_for||d.published_at,Q=q?new Date(q):null,he=(Q?ee(Q,P):null)?.toLocaleTimeString("en-US",{hour:"numeric",minute:"2-digit",hour12:!0}),ct=y?U.filter(N=>N.published).map(N=>{const se=N.published_at?new Date(N.published_at):null,ut=se?ee(se,P):null;return{platform:N.platform==="youtube"?"YouTube":"TikTok",time:ut?.toLocaleString("en-US",{hour:"numeric",minute:"2-digit",hour12:!0})}}):[],dt=e.jsxs("div",{className:"space-y-1 text-xs",children:[e.jsx("p",{className:"font-semibold",children:d.title}),g?e.jsx(e.Fragment,{children:d.status==="failed"?e.jsxs(e.Fragment,{children:[e.jsx("p",{className:"text-red-600 dark:text-red-400 font-medium",children:"Failed"}),d.failure_reason&&e.jsx("p",{className:"text-red-600 dark:text-red-400",children:d.failure_reason})]}):d.status==="generating"?e.jsx("p",{className:"text-yellow-600 dark:text-yellow-400",children:"Generating video..."}):d.status==="generated"?e.jsxs("p",{className:"text-blue-600 dark:text-blue-400",children:["Ready to post at ",he]}):e.jsxs(e.Fragment,{children:[e.jsx("p",{className:"text-muted-foreground",children:"Autopilot Planned"}),d.planDescription&&e.jsx("p",{className:"text-muted-foreground",children:d.planDescription}),e.jsxs("p",{className:"text-primary",children:["Posts at ",he]})]})}):y?e.jsxs(e.Fragment,{children:[e.jsx("p",{className:"text-green-600 dark:text-green-400",children:"Published"}),ct.map((N,se)=>e.jsxs("p",{className:"text-muted-foreground",children:[N.platform,": ",N.time]},se))]}):d.status==="failed"?e.jsxs(e.Fragment,{children:[e.jsx("p",{className:"text-red-600 dark:text-red-400 font-medium",children:"Failed to publish"}),d.failure_reason&&e.jsx("p",{className:"text-red-600 dark:text-red-400",children:d.failure_reason})]}):e.jsxs(e.Fragment,{children:[e.jsx("p",{className:"text-muted-foreground",children:"Scheduled"}),e.jsxs("p",{className:"text-primary",children:["Posts at ",he]}),d.platforms&&d.platforms.length>0&&e.jsxs("p",{className:"text-muted-foreground",children:["Platforms: ",d.platforms.map(N=>N==="youtube"?"YouTube":"TikTok").join(", ")]})]})]});return e.jsx(wt,{children:e.jsxs(St,{children:[e.jsx(vt,{asChild:!0,children:e.jsxs("div",{draggable:!y&&!g&&!H,onDragStart:N=>!g&&!H&&Qe(N,d),onClick:N=>{N.stopPropagation(),g||De(d)},className:` 11 text-xs p-1.5 rounded-lg truncate 12 ${g?d.status==="failed"?"bg-red-500/10 text-red-700 dark:text-red-400 border border-red-500/20":d.status==="generating"?"bg-blue-500/10 text-blue-700 dark:text-blue-400 border border-blue-500/20":"bg-yellow-500/10 text-yellow-700 dark:text-yellow-400 border border-yellow-500/20":d.status==="failed"?"bg-red-500/10 text-red-700 dark:text-red-400 cursor-pointer border border-red-500/20":y?"bg-green-500/10 text-green-700 dark:text-green-400 cursor-pointer":"bg-primary/10 text-primary cursor-move"} 13 ${!y&&!g&&"hover:ring-2 hover:ring-primary/50"} 14 `,children:[e.jsx("span",{className:"truncate block",children:d.title}),y&&O&&e.jsxs("span",{className:"text-[10px] text-muted-foreground ml-1",children:["(",O,")"]})]})}),e.jsx(jt,{side:"right",className:"max-w-xs",children:dt})]})},d.id)}),S.length>2&&e.jsxs("button",{onClick:d=>{d.stopPropagation(),st(s,S)},className:"w-full text-xs text-primary hover:text-primary/80 font-medium text-center py-1 hover:bg-primary/5 rounded transition-colors",children:["+",S.length-2," more"]})]})]})},w)})]})})]}),e.jsx(yt,{video:b,open:v,onOpenChange:_,onUpdate:M}),e.jsx(Dt,{open:V,onOpenChange:Y,onSuccess:M}),z&&e.jsx(Lt,{open:$,onOpenChange:I,date:z,onConfirm:nt}),D&&e.jsx($t,{open:Z,onOpenChange:W,date:D,unscheduledVideos:m,onSelectVideo:ot}),e.jsx(fe,{open:$e,onOpenChange:ue,children:e.jsxs(xe,{className:"max-w-2xl max-h-[80vh] overflow-y-auto",children:[e.jsx(ge,{children:e.jsxs(be,{className:"flex items-center gap-2",children:[e.jsx(Rt,{className:"h-5 w-5"}),oe&&oe.date?oe.date.toLocaleDateString("en-US",{weekday:"long",year:"numeric",month:"long",day:"numeric"}):"Selected date"]})}),e.jsx("div",{className:"space-y-2",children:oe?.videos.map(s=>{const w=s.isAutopilotPlan,S=s.status==="published",j=s.status==="failed",d=(s.published_platforms||[]).filter(g=>g.published).map(g=>g.platform==="youtube"?"YouTube":"TikTok").join(", ");return e.jsx(pe,{className:`p-4 ${w?"":"cursor-pointer"} hover:border-primary/50 transition-colors ${j?"bg-red-500/5 border-red-500/20":S?"bg-green-500/5 border-green-500/20":"bg-primary/5 border-primary/20"}`,onClick:()=>{w||(ue(!1),De(s))},children:e.jsxs("div",{className:"flex items-start justify-between gap-4",children:[e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("h4",{className:"font-semibold mb-1 truncate",children:s.title}),e.jsx("p",{className:"text-sm text-muted-foreground line-clamp-2",children:w?s.planDescription||"Autopilot planned video":s.description||"No description"}),j&&s.failure_reason&&e.jsxs("p",{className:"text-sm text-red-600 dark:text-red-400 mt-1",children:["Error: ",s.failure_reason]}),e.jsxs("div",{className:"flex items-center gap-2 mt-2",children:[e.jsx(te,{variant:j?"destructive":S?"default":"secondary",className:"text-xs",children:j?"Failed":S?"Published":w?`Autopilot - ${s.status}`:"Scheduled"}),S&&d&&e.jsx("span",{className:"text-xs text-muted-foreground",children:d}
14),s.virality_score&&e.jsxs("div",{className:"flex items-center gap-1 text-xs text-muted-foreground",children:[e.jsx(Ee,{className:"h-3 w-3"}),s.virality_score,"%"]})]})]}),(s.image_url||s.video_url)&&e.jsx("div",{className:"w-20 h-20 rounded-lg overflow-hidden bg-muted flex-shrink-0",children:s.video_url?e.jsx("video",{src:s.video_url,className:"w-full h-full object-cover",muted:!0}):e.jsx("img",{src:s.image_url,alt:s.title,className:"w-full h-full object-cover"})})]})},s.id)})})]})})]})};export{gr as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.