1import{j as e,A as ie,m as B}from"./ui-vendor-3gqMb7zq.js";import{r as s}from"./react-vendor-Cdr-Ll2J.js";import{D as V,a as H,b as W,c as Y,d as G,P as L,e as de}from"./dialog-CQ8F29P5.js";import{d as ce,s as x,a as n,B as J}from"./index-DAu1j8SZ.js";import{b as K,s as me,C as xe}from"./shareUrl-99umYgHP.js";import{c as ue,V as _,W as fe}from"./DrAftApp-B85USh3W.js";import{u as pe}from"./useCollaborators-Bv9rbgWD.js";import{S as he}from"./zap-DyVvBM0U.js";import{C as ge}from"./circle-alert-DExy24cn.js";import{C as ve}from"./sparkles-B6qnSy0K.js";import{C as u}from"./check-Dm33dms4.js";import{L as je}from"./loader-circle-CTgJLPpT.js";import{T as be}from"./trash-2-CX9PvLGG.js";import{U as Ne}from"./users-DpyF6Gfh.js";import{E as we}from"./ellipsis-vertical-LbHyisIV.js";import{U as ye}from"./user-minus-Ck7VoGpg.js";import"./input-D-aDs-SA.js";import"./slate-vendor-DuXTMglW.js";import"./tv-BidsaquF.js";import"./arrow-left-CRrTOR1J.js";import"./file-text-F8u_RHbW.js";import"./upload-qlLlo6xS.js";import"./sun-DMRVqMC0.js";import"./AiPassMark-QkmuOWtC.js";import"./aipassService-D7czyp0k.js";import"./shield-DOKBfSna.js";import"./CookiePreferences-8al_Ohso.js";const Ze=({isOpen:f,onClose:Q,script:o})=>{const X=ce(),{user:Z}=ue(),[a,v]=s.useState("viewer"),[E,j]=s.useState(!1),[D,d]=s.useState(null),[U,p]=s.useState(null),[$,h]=s.useState(null),[b,ee]=s.useState([]),[Ce,T]=s.useState(!1),[c,g]=s.useState(null),{collaborators:M,refreshCollaborators:P}=pe(o?.id??null,null,{enabled:f&&!!o?.id}),N=s.useMemo(()=>M.filter(t=>t.role!=="owner"),[M]),[A,m]=s.useState(!1),w=s.useRef(null),[y,i]=s.useState(null),C=s.useRef(null),F=s.useRef(null),k=s.useRef(new Map),[S,q]=s.useState(null);s.useEffect(()=>{!f||!o?.id||(v("viewer"),p(null),h(null),m(!1),i(null),R())},[f,o?.id]),s.useEffect(()=>{const t=r=>{w.current&&!w.current.contains(r.target)&&m(!1),C.current&&!C.current.contains(r.target)&&i(null)};return document.addEventListener("mousedown",t),()=>document.removeEventListener("mousedown",t)},[]);const R=async()=>{if(o?.id){T(!0);try{const{data:t}=await x.from("share_links").select("id, code, role, expires_at").eq("script_id",o.id).gt("expires_at",new Date().toISOString()).order("created_at",{ascending:!1});t&&ee(t)}finally{T(!1)}}};if(!o)return null;const te=U?K(U):"",re=async()=>{if(o?.id){if(j(!0),d(null),h(null),b.length>=10){d("You can have a maximum of 10 active links. Please delete some existing links to create a new one."),j(!1);return}try{if(!Z){d("Please sign in again.");return}const{data:t,error:r}=await x.functions.invoke("share-create",{body:{scriptId:o.id,role:a}});if(r||!t?.linkId){d(r?.message||t?.error||"Unable to create link.");return}X?.capture("share_link_created",{role:a,script_id:o.id}),p(t.token??t.linkId),R()}catch(t){d(t?.message||"Unable to create link.")}finally{j(!1)}}},se=async t=>{try{const{error:r}=await x.from("share_links").delete().eq("id",t);if(r)throw r;R()}catch(r){console.error("Failed to delete share link",r)}},oe=t=>{g(t)},ae=async()=>{if(!(!c||!o?.id))try{const{error:t}=await x.from("script_collaborators").delete().eq("script_id",o.id).eq("user_id",c.id);if(t)throw t;P()}catch(t){console.error("Failed to remove collaborator",t)}finally{g(null)}},I=async(t,r)=>{if(t.role===r){i(null);return}if(o?.id)try{const{error:l}=await x.from("script_collaborators").update({role:r}).eq("script_id",o.id).eq("user_id",t.id);if(l)throw l;P()}catch(l){console.error("Failed to change collaborator role",l)}finally{i(null)}},ne=t=>{if(y===t){i(null),q(null);return}const r=k.current.get(t),l=F.current;if(r&&l){const O=r.getBoundingClientRect(),z=l.getBoundingClientRect();q({top:O.bottom-z.top+4,left:O.right-z.left-176})}i(t)},le=async t=>{const r=t?K(t):te;if(r)try{await navigator.clipboard.writeText(r),t&&(h(t),setTimeout(()=>h(null),2e3))}catch(l){console.error("Failed to copy share link",l)}};return e.jsx(V,{open:f,onOpenChange:t=>!t&&Q(),children:e.jsxs(H,{ref:F,className:"sm:max-w-[480px] p-0 bg-background border border-border overflow-visible rounded-xl",children:[e.jsxs("div",{className:"p-6 px-8",children:[e.jsx(W,{className:"mb-6",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"p-2 rounded-lg bg-primary/10 text-primary",children:e.jsx(he,{className:"w-5 h-5"})}),e.jsxs("div",{children:[e.jsx(Y,{className:"text-lg font-bold text-foreground",children:"Share Script"}),e.jsx(G,{className:"text-sm text-muted-foreground font-medium truncate max-w-[320px]",children:o.title})]})]})}),e.jsxs("div",{className:"space-y-6",children:[D&&e.jsxs("div",{className:"flex items-center gap-2 p-3 rounded-lg bg-destructive/10 border border-destructive/20 text-destructive text-sm font-medium",children:[e.jsx(ge,{className:"w-4 h-4 shrink-0"}),e.jsx("span",{children:D})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsx("span",{className:"text-sm font-semibold text-foreground",children:"Invite as"}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs("div",{className:"relative flex-1",ref:w,children:[e.jsxs("button",{onClick:()=>m(!A),className:"h-10 px-3 w-full flex items-center justify-between text-sm font-medium bg-muted border border-border rounded-lg hover:border-border/80 transition-colors",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[a==="viewer"?e.jsx(_,{className:"w-4 h-4 text-muted-foreground"}):e.jsx(L,{className:"w-4 h-4 text-muted-foreground"}),e.jsx("span",{className:"capitalize text-foreground font-semibold",children:a==="viewer"?"Viewer":"Editor"})]}),e.jsx(ve,{className:"w-4 h-4 text-muted-foreground opacity-50"})]}),A&&e.jsxs("div",{className:"absolute top-full left-0 mt-1 w-full bg-popover border border-border rounded-xl overflow-hidden z-[var(--z-dropdown)] py-1 shadow-xl",children:[e.jsxs("button",{onClick:()=>{v("viewer"),m(!1),p(null)},className:"w-full flex items-center gap-2 px-3 py-2 text-sm hover:bg-muted transition-colors text-left",children:[e.jsx(_,{className:n("w-4 h-4",a==="viewer"?"text-primary":"text-muted-foreground")}),e.jsxs("div",{className:"flex flex-col flex-1",children:[e.jsx("span",{className:n("font-medium",a==="viewer"?"text-primary":"text-foreground"),children:"Viewer"}),e.jsx("span",{className:"text-[10px] text-muted-foreground",children:"Read-only access"})]}),a==="viewer"&&e.jsx(u,{className:"w-3.5 h-3.5 text-primary ml-auto"})]}),e.jsxs("button",{onClick:()=>{v("editor"),m(!1),p(null)},className:"w-full flex items-center gap-2 px-3 py-2 text-sm hover:bg-muted transition-colors text-left",children:[e.jsx(L,{className:n("w-4 h-4",a==="editor"?"text-primary":"text-muted-foreground")}),e.jsxs("div",{className:"flex flex-col flex-1",children:[e.jsx("span",{className:n("font-medium",a==="editor"?"text-primary":"text-foreground"),children:"Editor"}),e.jsx("span",{className:"text-[10px] text-muted-foreground",children:"Can edit script"})]}),a==="editor"&&e.jsx(u,{className:"w-3.5 h-3.5 text-primary ml-auto"})]})]})]}),e.jsxs("button",{onClick:re,disabled:E,className:"h-10 bg-primary text-primary-foreground px-5 rounded-lg text-sm font-bold hover:opacity-90 transition-all disabled:opacity-50 flex items-center justify-center gap-2 whitespace-nowrap shrink-0",children:[E?e.jsx(je,{className:"w-4 h-4 animate-spin"}):e.jsx(fe,{className:"w-4 h-4"}),e.jsx("span",{children:"Create Link"})]})]})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsx("span",{className:"text-sm font-semibold text-foreground",children:"Active Links"}),b.length>0?e.jsx("div",{className:"space-y-2 max-h-[140px] overflow-y-auto pr-1 custom-scrollbar",children:b.map(t=>e.jsxs("div",{className:"flex items-center gap-2 p-2 px-3 bg-muted/20 rounded-lg border border-border group hover:border-border/60 transition-colors w-full overflow-hidden",children:[e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-0.5",children:[e.jsx("span",{className:"text-[11px] font-bold uppercase text-foreground leading-none",children:t.role}),e.jsx("span",{className:"text-[10px] text-muted-foreground leading-none",children:t.expires_at?new Date(t.expires_at).toLocaleStr
1ing("en-US",{dateStyle:"medium",timeStyle:"short"}):"No expiration"})]}),e.jsx("div",{className:"text-[10px] text-muted-foreground break-all whitespace-normal font-mono leading-tight",children:me(t.code??t.id)})]}),e.jsxs("div",{className:"flex items-center gap-1 shrink-0",children:[e.jsx("button",{onClick:()=>le(t.code??t.id),className:"p-1.5 rounded-md hover:bg-muted text-muted-foreground hover:text-foreground transition-colors relative w-7 h-7 flex items-center justify-center",children:e.jsx(ie,{mode:"wait",initial:!1,children:$===(t.code??t.id)?e.jsx(B.div,{initial:{scale:.5,opacity:0},animate:{scale:1,opacity:1},exit:{scale:.5,opacity:0},transition:{duration:.2},children:e.jsx(u,{className:"w-3.5 h-3.5 text-green-500"})},"check"):e.jsx(B.div,{initial:{scale:.5,opacity:0},animate:{scale:1,opacity:1},exit:{scale:.5,opacity:0},transition:{duration:.2},children:e.jsx(xe,{className:"w-3.5 h-3.5"})},"copy")})}),e.jsx("button",{onClick:()=>se(t.id),className:"p-1.5 rounded-md hover:bg-destructive/10 text-muted-foreground hover:text-destructive transition-colors",children:e.jsx(be,{className:"w-3.5 h-3.5"})})]})]},t.id))}):e.jsx("div",{className:"text-xs text-muted-foreground italic pl-1",children:"No active share links."})]}),e.jsxs("div",{className:"space-y-4 pb-6",children:[e.jsx("span",{className:"text-sm font-semibold text-foreground",children:"Collaborators"}),N.length>0?e.jsx("div",{className:"space-y-2 max-h-[180px] overflow-y-auto overflow-x-visible pr-1 custom-scrollbar",children:N.map(t=>e.jsxs("div",{className:"flex items-center gap-3 p-2 px-3 bg-muted/10 rounded-lg border border-border/50",children:[e.jsx("div",{className:"w-8 h-8 rounded-full bg-primary/10 flex items-center justify-center shrink-0 border border-primary/5 overflow-hidden",children:t.avatarUrl?e.jsx("img",{src:t.avatarUrl,alt:"",className:"w-full h-full object-cover",referrerPolicy:"no-referrer"}):e.jsx(Ne,{className:"w-4 h-4 text-primary"})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"text-sm font-bold text-foreground truncate",children:t.name||(t.email??"").split("@")[0]}),e.jsx("span",{className:"text-[10px] font-bold uppercase py-0.5 px-1.5 bg-muted rounded text-muted-foreground",children:t.role})]}),e.jsx("div",{className:"text-xs text-muted-foreground truncate",children:t.email??""})]}),e.jsx("button",{ref:r=>{r?k.current.set(t.id,r):k.current.delete(t.id)},onClick:()=>ne(t.id),className:"p-2 rounded-md hover:bg-muted text-muted-foreground hover:text-foreground transition-colors shrink-0",title:"Options",children:e.jsx(we,{className:"w-4 h-4"})})]},t.id))}):e.jsx("div",{className:"text-xs text-muted-foreground italic pl-1",children:"No collaborators yet."})]})]})]}),y&&S&&e.jsxs("div",{ref:C,className:"absolute w-44 bg-popover border border-border rounded-xl shadow-xl z-[var(--z-modal-nested)] py-1",style:{to
1p:S.top,left:S.left},children:[e.jsx("div",{className:"px-3 py-1.5 text-[10px] font-semibold text-muted-foreground uppercase tracking-wide",children:"Change Role"}),(()=>{const t=N.find(r=>r.id===y);return t?e.jsxs(e.Fragment,{children:[e.jsxs("button",{onClick:()=>I(t,"viewer"),className:n("w-full flex items-center gap-2 px-3 py-2 text-sm hover:bg-muted transition-colors text-left",t.role==="viewer"&&"bg-muted/50"),children:[e.jsx(_,{className:n("w-4 h-4",t.role==="viewer"?"text-primary":"text-muted-foreground")}),e.jsxs("div",{className:"flex flex-col",children:[e.jsx("span",{className:n("font-medium",t.role==="viewer"?"text-primary":"text-foreground"),children:"Viewer"}),e.jsx("span",{className:"text-[10px] text-muted-foreground",children:"Read-only access"})]}),t.role==="viewer"&&e.jsx(u,{className:"w-3.5 h-3.5 text-primary ml-auto"})]}),e.jsxs("button",{onClick:()=>I(t,"editor"),className:n("w-full flex items-center gap-2 px-3 py-2 text-sm hover:bg-muted transition-colors text-left",t.role==="editor"&&"bg-muted/50"),children:[e.jsx(L,{className:n("w-4 h-4",t.role==="editor"?"text-primary":"text-muted-foreground")}),e.jsxs("div",{className:"flex flex-col",children:[e.jsx("span",{className:n("font-medium",t.role==="editor"?"text-primary":"text-foreground"),children:"Editor"}),e.jsx("span",{className:"text-[10px] text-muted-foreground",children:"Can edit script"})]}),t.role==="editor"&&e.jsx(u,{className:"w-3.5 h-3.5 text-primary ml-auto"})]}),e.jsx("div",{className:"h-px bg-border my-1"}),e.jsxs("button",{onClick:()=>{i(null),oe(t)},className:"w-full flex items-center gap-2 px-3 py-2 text-sm hover:bg-destructive/10 text-destructive transition-colors text-left",children:[e.jsx(ye,{className:"w-4 h-4"}),e.jsx("span",{className:"font-medium",children:"Remove Access"})]})]}):null})()]}),e.jsx(V,{open:!!c,onOpenChange:t=>!t&&g(null),children:e.jsxs(H,{className:"sm:max-w-[400px]",children:[e.jsxs(W,{children:[e.jsx(Y,{children:"Remove Collaborator"}),e.jsxs(G,{children:["Are you sure you want to remove ",e.jsx("span",{className:"font-medium text-foreground",children:c?.name||c?.email}),"? They will lose access to this script."]})]}),e.jsxs(de,{className:"mt-4",children:[e.jsx(J,{variant:"outline",onClick:()=>g(null),className:"hover:bg-muted/50",children:"Cancel"}),e.jsx(J,{variant:"destructive",onClick:ae,children:"Remove"})]})]})})]})})};export{Ze as ShareScriptModal};
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.