@tvolden/block-code-editor 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ import { type Theme } from './theme';
2
+ export declare const ThemeContext: import("react").Context<Theme>;
3
+ export declare function useTheme(): Theme;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const t=require("react/jsx-runtime"),m=require("react"),he={id:"dark",label:"Dark",appBg:"#0e1118",paletteBg:"#141820",paletteHeaderBg:"#0e1118",canvasBg:"#111420",consoleBg:"#0e1118",consoleHeaderBg:"#141820",border:"#252d42",accent:"#c9a43a",textPrimary:"#dde8f8",textSecondary:"#7a90b8",textMuted:"#5a6888",inputBg:"#0e1118",inputBorder:"#303858",gridDot:"#252d42",blockBody:"#1a2236",blockBorder:"#303858",blockText:"#e8f0fc",socketBody:"#141e30",consoleOutput:"#b8d4ec",consoleSeparator:"#4a6090",deleteBorder:"#252d42",deleteText:"#5a6888",deleteActiveBg:"#3a0c0c",deleteActiveBorder:"#8a2020",deleteActiveText:"#e06060",titleBg:"#0e1118",titlePrimaryText:"#c9a43a",titleSecondaryText:"#5a6888",pkgBg:"#141820",pkgBorder:"#252d42",pkgActiveBg:"#1a2236",pkgActiveBorder:"#3a4d70",pkgText:"#7a8aaa",pkgActiveText:"#c9a43a",pkgSubText:"#5a6888",switcherBg:"transparent",switcherActiveBg:"#1a2236",switcherText:"#5a6888",switcherActiveText:"#c9a43a",switcherBorder:"#252d42"},Rt={id:"light",label:"Light",appBg:"#f0f2f6",paletteBg:"#ffffff",paletteHeaderBg:"#f8f9fc",canvasBg:"#e8ebf2",consoleBg:"#ffffff",consoleHeaderBg:"#f8f9fc",border:"#dde2ec",accent:"#2a50a0",textPrimary:"#1a2030",textSecondary:"#5060a0",textMuted:"#a0aac0",inputBg:"#f0f2f6",inputBorder:"#d0d8e8",gridDot:"#c0c8d8",blockBody:"#ffffff",blockBorder:"#c8d0e0",blockText:"#1a2030",socketBody:"#eaecf4",consoleOutput:"#2a50a0",consoleSeparator:"#dde2ec",deleteBorder:"#dde2ec",deleteText:"#b0b8d0",deleteActiveBg:"#fff0f0",deleteActiveBorder:"#e08080",deleteActiveText:"#c04040",titleBg:"#ffffff",titlePrimaryText:"#1a2030",titleSecondaryText:"#8090b0",pkgBg:"#f8f9fc",pkgBorder:"#dde2ec",pkgActiveBg:"#eef2fc",pkgActiveBorder:"#a0b8e0",pkgText:"#7080a0",pkgActiveText:"#2a50a0",pkgSubText:"#9090a0",switcherBg:"transparent",switcherActiveBg:"#eaedf4",switcherText:"#9090a0",switcherActiveText:"#1a2030",switcherBorder:"#dde2ec"},we=[he,Rt],Ie=m.createContext(he);function J(){return m.useContext(Ie)}const oe=280,ae=392,Be=16,I=48;function ht(){return{version:"1",meta:{name:"New Card",description:"",cardType:"",tags:"",customFields:[]},background:{color:"#1a2a4a",imageUrl:""},elements:[]}}let Dt=0;function ze(){return`el${++Dt}_${Date.now()}`}function gt(e,n){const r={name:n.name,description:n.description,type:n.cardType,tags:n.tags};for(const{key:s,value:i}of n.customFields)s.trim()&&(r[s.trim().toLowerCase()]=i);return e.replace(/\{\{(\w+)\}\}/g,(s,i)=>Object.prototype.hasOwnProperty.call(r,i.toLowerCase())?r[i.toLowerCase()]:s)}const He=8,Wt=oe+2*I,zt=ae+2*I;function Ht({design:e,selectedId:n,onSelect:r,onMove:s,onResize:i}){const o=m.useRef(null),c=m.useRef(null);function d(l){const k=o.current,g=k.createSVGPoint();g.x=l.clientX,g.y=l.clientY;const P=g.matrixTransform(k.getScreenCTM().inverse());return{x:P.x-I,y:P.y-I}}function T(l,k){l.stopPropagation(),r(k.id);const{x:g,y:P}=d(l);c.current={kind:"move",id:k.id,ox:g-k.x,oy:P-k.y},o.current.setPointerCapture(l.pointerId)}function a(l,k,g){l.stopPropagation();const{x:P,y:w}=d(l);c.current={kind:"resize",id:k.id,handle:g,startX:k.x,startY:k.y,startW:k.w,startH:k.h,px0:P,py0:w},o.current.setPointerCapture(l.pointerId)}function C(l){const k=c.current;if(!k)return;const{x:g,y:P}=d(l);if(k.kind==="move"){s(k.id,Math.round(g-k.ox),Math.round(P-k.oy));return}const w=g-k.px0,H=P-k.py0,Q=20;let{startX:ne,startY:D,startW:u,startH:M}=k;if(k.handle==="nw"){const _=Math.min(w,u-Q),x=Math.min(H,M-Q);ne+=_,D+=x,u-=_,M-=x}else if(k.handle==="ne"){const _=Math.min(H,M-Q);D+=_,M-=_,u=Math.max(Q,u+w)}else if(k.handle==="sw"){const _=Math.min(w,u-Q);ne+=_,u-=_,M=Math.max(Q,M+H)}else u=Math.max(Q,u+w),M=Math.max(Q,M+H);i(k.id,Math.round(ne),Math.round(D),Math.round(u),Math.round(M))}function $(){c.current=null}const v=e.elements.find(l=>l.id===n)??null;return t.jsxs("svg",{ref:o,viewBox:`0 0 ${Wt} ${zt}`,preserveAspectRatio:"xMidYMid meet",style:{width:"100%",height:"100%",display:"block"},onPointerMove:C,onPointerUp:$,onPointerDown:()=>r(null),children:[t.jsxs("defs",{children:[t.jsx("filter",{id:"cd-shadow",x:"-25%",y:"-25%",width:"150%",height:"150%",children:t.jsx("feDropShadow",{dx:"0",dy:"6",stdDeviation:"10",floodColor:"rgba(0,0,0,0.65)"})}),t.jsx("clipPath",{id:"cd-clip",children:t.jsx("rect",{x:I,y:I,width:oe,height:ae,rx:Be})})]}),t.jsx("rect",{x:I,y:I,width:oe,height:ae,rx:Be,fill:e.background.color,filter:"url(#cd-shadow)"}),e.background.imageUrl&&t.jsx("image",{href:e.background.imageUrl,x:I,y:I,width:oe,height:ae,clipPath:"url(#cd-clip)",preserveAspectRatio:"xMidYMid slice"}),t.jsx("g",{clipPath:"url(#cd-clip)",children:e.elements.map(l=>t.jsx(_t,{el:l,meta:e.meta,onDown:T},l.id))}),v&&t.jsxs(t.Fragment,{children:[t.jsx("rect",{x:I+v.x-1,y:I+v.y-1,width:v.w+2,height:v.h+2,fill:"none",stroke:"#4a9eff",strokeWidth:1.5,strokeDasharray:"4 2",pointerEvents:"none"}),["nw","ne","sw","se"].map(l=>{const k=I+v.x+(l.includes("e")?v.w:0),g=I+v.y+(l.includes("s")?v.h:0);return t.jsx("rect",{x:k-He/2,y:g-He/2,width:He,height:He,fill:"white",stroke:"#4a9eff",strokeWidth:1.5,style:{cursor:`${l}-resize`},onPointerDown:P=>{P.stopPropagation(),a(P,v,l)}},l)})]}),t.jsx("rect",{x:I,y:I,width:oe,height:ae,rx:Be,fill:"none",stroke:"rgba(255,255,255,0.13)",strokeWidth:1.5,pointerEvents:"none"})]})}function _t({el:e,meta:n,onDown:r}){const s=i=>r(i,e);return e.type==="rectangle"?t.jsx("rect",{x:I+e.x,y:I+e.y,width:e.w,height:e.h,rx:e.rx,fill:e.fill,stroke:e.stroke,strokeWidth:e.strokeWidth,style:{cursor:"move"},onPointerDown:s}):e.type==="circle"?t.jsx("ellipse",{cx:I+e.x+e.w/2,cy:I+e.y+e.h/2,rx:e.w/2,ry:e.h/2,fill:e.fill,stroke:e.stroke,strokeWidth:e.strokeWidth,style:{cursor:"move"},onPointerDown:s}):e.type==="image"?t.jsx("image",{href:e.src,x:I+e.x,y:I+e.y,width:e.w,height:e.h,preserveAspectRatio:"xMidYMid slice",style:{cursor:"move"},onPointerDown:s}):e.type==="text"?t.jsx("foreignObject",{x:I+e.x,y:I+e.y,width:e.w,height:e.h,style:{cursor:"move",overflow:"visible"},onPointerDown:s,children:t.jsx("div",{style:{width:"100%",height:"100%",overflow:"hidden",fontSize:`${e.fontSize}px`,fontWeight:e.fontWeight,fontStyle:e.fontStyle,color:e.color,textAlign:e.align,userSelect:"none",lineHeight:1.35,boxSizing:"border-box",padding:"2px 4px",whiteSpace:"pre-wrap",wordBreak:"break-word"},children:gt(e.content,n)})}):null}function yt({onSelect:e}){const n=J();return t.jsx("div",{style:{display:"flex",gap:4},children:we.map(r=>t.jsx("button",{onClick:()=>e(r),style:{background:n.id===r.id?n.switcherActiveBg:n.switcherBg,border:`1px solid ${n.switcherBorder}`,borderRadius:4,padding:"2px 9px",color:n.id===r.id?n.switcherActiveText:n.switcherText,fontSize:10,fontWeight:n.id===r.id?"bold":"normal",letterSpacing:.5,cursor:"pointer",transition:"color 0.1s, background 0.1s"},children:r.label},r.id))})}const Ye=248,mt="cardcq-card-design";function Yt(){try{const e=localStorage.getItem(mt);if(e){const n=JSON.parse(e);return Array.isArray(n.meta.customFields)||(n.meta.customFields=[]),n}}catch{}return ht()}function Ft({onThemeChange:e,navTabs:n,themeId:r,hideTitleBar:s,compactMode:i,workspaceOnly:o,design:c,onDesignChange:d,selectedId:T,onSelectedIdChange:a,onExport:C,onImport:$}){const v=J(),[l,k]=m.useState(()=>r?we.find(p=>p.id===r)??he:he);m.useEffect(()=>{r&&k(we.find(p=>p.id===r)??he)},[r]);const g=r?l:v,P=c!==void 0,[w,H]=m.useState(Yt),[Q,ne]=m.useState(null),D=P?c:w,u=T!==void 0?T:Q;function M(p){P?d?.(p(D)):H(p)}function _(p){a?a(p):ne(p)}const x=m.useRef(null);m.useEffect(()=>{P||(x.current&&clearTimeout(x.current),x.current=setTimeout(()=>{localStorage.setItem(mt,JSON.stringify(w))},150))},[w,P]);function F(p){M(B=>({...B,meta:{...B.meta,...p}}))}function K(p){M(B=>({...B,background:{...B.background,...p}}))}function ue(p,B){M(A=>({...A,elements:A.elements.map(W=>W.id===p?{...W,...B}:W)}))}function fe(p){M(B=>({...B,elements:B.elements.filter(A=>A.id!==p)})),_(null)}function N(p){M(B=>({...B,elements:[...B.elements,p]})),_(p.id)}function xe(p,B){M(A=>{const W=A.elements.findIndex(ye=>ye.id===p),X=W+B;if(X<0||X>=A.elements.length)return A;const Z=[...A.elements];return[Z[W],Z[X]]=[Z[X],Z[W]],{...A,elements:Z}})}m.useEffect(()=>{function p(B){if(!u)return;const A=B.target;if(A instanceof HTMLInputElement||A instanceof HTMLTextAreaElement||A instanceof HTMLSelectElement)return;const W=B.shiftKey?10:1;let X=0,Z=0;if(B.key==="ArrowLeft")X=-W;else if(B.key==="ArrowRight")X=W;else if(B.key==="ArrowUp")Z=-W;else if(B.key==="ArrowDown")Z=W;else return;B.preventDefault(),M(ye=>({...ye,elements:ye.elements.map(me=>me.id===u?{...me,x:me.x+X,y:me.y+Z}:me)}))}return window.addEventListener("keydown",p),()=>window.removeEventListener("keydown",p)},[u]);function ie(){N({id:ze(),type:"text",x:20,y:20,w:oe-40,h:36,content:"Card Name",fontSize:18,fontWeight:"bold",fontStyle:"normal",color:"#ffffff",align:"center"})}function Te(){N({id:ze(),type:"rectangle",x:40,y:60,w:200,h:100,fill:"#2a3a5a",stroke:"#4a6090",strokeWidth:2,rx:6})}function je(){N({id:ze(),type:"circle",x:90,y:100,w:100,h:100,fill:"#2a3a5a",stroke:"#4a6090",strokeWidth:2})}function dt(){N({id:ze(),type:"image",x:20,y:60,w:oe-40,h:160,src:""})}function Ae(){const p=JSON.stringify(D,null,2),B=new Blob([p],{type:"application/json"}),A=URL.createObjectURL(B),W=document.createElement("a");W.href=A,W.download=`${D.meta.name.replace(/\s+/g,"_")||"card"}.json`,W.click(),URL.revokeObjectURL(A)}function qe(){const p=document.createElement("input");p.type="file",p.accept=".json,application/json",p.onchange=()=>{const B=p.files?.[0];if(!B)return;const A=new FileReader;A.onload=W=>{try{const X=JSON.parse(W.target?.result);Array.isArray(X.meta.customFields)||(X.meta.customFields=[]),M(()=>X),_(null)}catch{alert("Invalid JSON file")}},A.readAsText(B)},p.click()}const se=D.elements.find(p=>p.id===u)??null,le=ot(g),Ce=i?{width:Ye,flexShrink:0}:{flex:1,minWidth:0},Re={design:D,selectedId:u,sel:se,theme:g,onSelect:_,onMove:(p,B,A)=>ue(p,{x:B,y:A}),onResize:(p,B,A,W,X)=>ue(p,{x:B,y:A,w:W,h:X}),onUpdate:p=>{se&&ue(se.id,p)},onDelete:()=>{se&&fe(se.id)},onMoveZ:p=>{se&&xe(se.id,p)},onUpdateBg:K,onAddText:ie,onAddRect:Te,onAddCircle:je,onAddImage:dt};return o?t.jsx(Ie.Provider,{value:g,children:t.jsx("div",{style:{display:"flex",flex:1,minWidth:0,minHeight:0,overflow:"hidden",fontFamily:"system-ui, sans-serif"},children:t.jsx(ut,{...Re})})}):t.jsx(Ie.Provider,{value:g,children:t.jsxs("div",{style:{display:"flex",flexDirection:"column",...Ce,overflow:"hidden",background:g.appBg,color:g.textPrimary,fontFamily:"system-ui, sans-serif"},children:[!s&&t.jsxs("div",{style:{flexShrink:0,display:"flex",alignItems:"center",gap:10,padding:"0 14px",height:38,background:g.titleBg,borderBottom:`1px solid ${g.border}`},children:[t.jsx("span",{style:{color:g.titlePrimaryText,fontWeight:"bold",fontSize:13,letterSpacing:1,textTransform:"uppercase"},children:"CardCQ"}),t.jsx("span",{style:{color:g.titleSecondaryText,fontSize:11,letterSpacing:.5},children:"Card Designer"}),n,t.jsx("div",{style:{marginLeft:"auto"},children:t.jsx(yt,{onSelect:p=>e?.(p)})})]}),t.jsxs("div",{style:{display:"flex",flex:1,minHeight:0,overflow:"hidden"},children:[t.jsxs("div",{style:{width:Ye,flexShrink:0,background:g.paletteBg,borderRight:`1px solid ${g.border}`,display:"flex",flexDirection:"column",overflow:"hidden"},children:[t.jsx(Ke,{children:"Card Properties"}),t.jsx("div",{style:{flex:1,overflowY:"auto",padding:12},children:t.jsxs(Je,{children:[t.jsx(Ze,{children:"Meta"}),t.jsx(Se,{lbl:"Name",children:t.jsx("input",{type:"text",value:D.meta.name,style:le,onChange:p=>F({name:p.target.value})})}),t.jsx(Se,{lbl:"Type",children:t.jsx("input",{type:"text",value:D.meta.cardType,style:le,placeholder:"e.g. Creature, Spell…",onChange:p=>F({cardType:p.target.value})})}),t.jsx(Se,{lbl:"Description",children:t.jsx("textarea",{value:D.meta.description,onChange:p=>F({description:p.target.value}),style:{...le,height:56,resize:"none"}})}),t.jsx(Se,{lbl:"Tags (comma-separated)",children:t.jsx("input",{type:"text",value:D.meta.tags,style:le,onChange:p=>F({tags:p.target.value})})}),t.jsxs("div",{style:{marginTop:4},children:[t.jsx("div",{style:{fontSize:11,color:g.textMuted,marginBottom:6},children:"Custom Fields"}),D.meta.customFields.map((p,B)=>t.jsxs("div",{style:{display:"flex",gap:4,marginBottom:4,alignItems:"center"},children:[t.jsx("input",{type:"text",value:p.key,placeholder:"key",onChange:A=>{const W=D.meta.customFields.map((X,Z)=>Z===B?{...X,key:A.target.value}:X);F({customFields:W})},style:{...le,flex:"0 0 80px",width:80}}),t.jsx("input",{type:"text",value:p.value,placeholder:"value",onChange:A=>{const W=D.meta.customFields.map((X,Z)=>Z===B?{...X,value:A.target.value}:X);F({customFields:W})},style:{...le,flex:1}}),t.jsx("button",{onClick:()=>F({customFields:D.meta.customFields.filter((A,W)=>W!==B)}),style:{flexShrink:0,background:"none",border:`1px solid ${g.border}`,color:g.textMuted,borderRadius:4,width:22,height:22,cursor:"pointer",fontSize:12,lineHeight:1,padding:0},title:"Remove field",children:"×"})]},B)),t.jsx("button",{onClick:()=>F({customFields:[...D.meta.customFields,{key:"",value:""}]}),style:{width:"100%",marginTop:2,background:"none",border:`1px dashed ${g.border}`,color:g.textMuted,borderRadius:4,padding:"4px",cursor:"pointer",fontSize:11},children:"+ Add Field"})]})]})}),t.jsxs("div",{style:{flexShrink:0,padding:"10px 12px",borderTop:`1px solid ${g.border}`,display:"flex",gap:6},children:[t.jsx("button",{onClick:C??Ae,style:{flex:1,background:g.accent,border:"none",color:"#0e1118",borderRadius:4,padding:"6px",cursor:"pointer",fontSize:11,fontWeight:600},children:"Export"}),t.jsx("button",{onClick:$??qe,style:{flex:1,background:g.blockBody,border:`1px solid ${g.border}`,color:g.textPrimary,borderRadius:4,padding:"6px",cursor:"pointer",fontSize:11},children:"Import"})]})]}),!i&&t.jsx(ut,{...Re})]})]})})}function ut({design:e,selectedId:n,onSelect:r,onMove:s,onResize:i,onUpdate:o,onDelete:c,onMoveZ:d,sel:T,theme:a,onUpdateBg:C,onAddText:$,onAddRect:v,onAddCircle:l,onAddImage:k}){const g=ot(a);return t.jsxs(t.Fragment,{children:[t.jsxs("div",{style:{width:Ye,flexShrink:0,background:a.paletteBg,borderRight:`1px solid ${a.border}`,display:"flex",flexDirection:"column",overflow:"hidden"},children:[t.jsx(Ke,{children:"Design Tools"}),t.jsxs("div",{style:{flex:1,overflowY:"auto",padding:12},children:[t.jsxs(Je,{children:[t.jsx(Ze,{children:"Background"}),t.jsx(Se,{lbl:"Color",children:t.jsxs("div",{style:{display:"flex",alignItems:"center",gap:8},children:[t.jsx("input",{type:"color",value:e.background.color,onChange:P=>C({color:P.target.value}),style:{width:32,height:24,border:"none",padding:0,cursor:"pointer",background:"none"}}),t.jsx("span",{style:{fontSize:11,color:a.textMuted},children:e.background.color})]})}),t.jsx(Se,{lbl:"Image URL",children:t.jsx("input",{type:"text",value:e.background.imageUrl,style:g,placeholder:"https://…",onChange:P=>C({imageUrl:P.target.value})})})]}),t.jsxs(Je,{children:[t.jsx(Ze,{children:"Add Element"}),t.jsx("div",{style:{display:"grid",gridTemplateColumns:"1fr 1fr",gap:6},children:[{label:"Text",icon:"T",fn:$},{label:"Rect",icon:"□",fn:v},{label:"Circle",icon:"○",fn:l},{label:"Image",icon:"⬛",fn:k}].map(({label:P,icon:w,fn:H})=>t.jsxs("button",{onClick:H,style:{background:a.blockBody,border:`1px solid ${a.border}`,color:a.textPrimary,borderRadius:4,padding:"7px 4px",cursor:"pointer",fontSize:11,display:"flex",alignItems:"center",gap:5,justifyContent:"center"},children:[t.jsx("span",{style:{fontFamily:"monospace",fontSize:13},children:w}),P]},P))})]})]})]}),t.jsxs("div",{style:{flex:1,minWidth:0,background:a.canvasBg,display:"flex",alignItems:"center",justifyContent:"center",overflow:"hidden",position:"relative"},children:[t.jsx("div",{style:{position:"absolute",inset:0,pointerEvents:"none",backgroundImage:`radial-gradient(circle, ${a.gridDot} 1px, transparent 1px)`,backgroundSize:"20px 20px"}}),t.jsx(Ht,{design:e,selectedId:n,onSelect:r,onMove:s,onResize:i})]}),t.jsxs("div",{style:{width:Ye,flexShrink:0,background:a.paletteBg,borderLeft:`1px solid ${a.border}`,display:"flex",flexDirection:"column",overflow:"hidden"},children:[t.jsx(Ke,{children:"Element Properties"}),t.jsx("div",{style:{flex:1,overflowY:"auto",padding:12},children:T?t.jsx(Lt,{el:T,onUpdate:o,onDelete:c,onMoveZ:d}):t.jsxs("div",{style:{color:a.textMuted,fontSize:12,textAlign:"center",marginTop:40,lineHeight:1.6},children:["Select an element on",t.jsx("br",{}),"the card to edit it"]})})]})]})}function ot(e){return{width:"100%",background:e.inputBg,border:`1px solid ${e.inputBorder}`,borderRadius:4,color:e.textPrimary,fontSize:12,padding:"4px 6px",outline:"none",boxSizing:"border-box"}}function Ke({children:e}){const n=J();return t.jsx("div",{style:{padding:"8px 12px",background:n.paletteHeaderBg,borderBottom:`1px solid ${n.border}`,fontSize:11,fontWeight:600,color:n.textSecondary,letterSpacing:"0.05em",textTransform:"uppercase",flexShrink:0},children:e})}function Ze({children:e}){const n=J();return t.jsx("div",{style:{fontSize:10,fontWeight:700,color:n.accent,marginBottom:8,letterSpacing:"0.08em",textTransform:"uppercase"},children:e})}function Je({children:e}){const n=J();return t.jsx("div",{style:{borderBottom:`1px solid ${n.border}`,paddingBottom:12,marginBottom:12},children:e})}function Se({lbl:e,children:n}){const r=J();return t.jsxs("div",{style:{marginBottom:8},children:[t.jsx("label",{style:{fontSize:11,color:r.textMuted,marginBottom:2,display:"block"},children:e}),n]})}function Lt({el:e,onUpdate:n,onDelete:r,onMoveZ:s}){const i=J(),o=ot(i),c={fontSize:11,color:i.textMuted,marginBottom:2,display:"block"},d={borderBottom:`1px solid ${i.border}`,paddingBottom:12,marginBottom:12},T={fontSize:10,fontWeight:700,color:i.accent,marginBottom:8,letterSpacing:"0.08em",textTransform:"uppercase"},a={background:i.blockBody,border:`1px solid ${i.border}`,color:i.textPrimary,borderRadius:4,padding:"4px 6px",cursor:"pointer",fontSize:11},C=l=>({...a,background:l?i.accent:i.blockBody,color:l?"#0e1118":i.textPrimary});function $(l,k,g,P,w){return t.jsxs("div",{children:[t.jsx("label",{style:c,children:l}),t.jsx("input",{type:"number",value:g,min:P,max:w,onChange:H=>n({[k]:Number(H.target.value)}),style:o})]})}function v(l,k,g){return t.jsxs("div",{style:{marginBottom:8},children:[t.jsx("label",{style:c,children:l}),t.jsxs("div",{style:{display:"flex",alignItems:"center",gap:6},children:[t.jsx("input",{type:"color",value:g,onChange:P=>n({[k]:P.target.value}),style:{width:32,height:22,border:"none",padding:0,cursor:"pointer",background:"none"}}),t.jsx("span",{style:{fontSize:11,color:i.textMuted},children:g})]})]})}return t.jsxs("div",{children:[t.jsx("div",{style:{fontSize:10,color:i.accent,fontWeight:700,letterSpacing:"0.1em",textTransform:"uppercase",marginBottom:10},children:e.type}),t.jsxs("div",{style:d,children:[t.jsx("div",{style:T,children:"Transform"}),t.jsxs("div",{style:{display:"grid",gridTemplateColumns:"1fr 1fr",gap:6},children:[$("X","x",e.x),$("Y","y",e.y),$("W","w",e.w,1),$("H","h",e.h,1)]})]}),e.type==="text"&&t.jsxs("div",{style:d,children:[t.jsx("div",{style:T,children:"Text"}),t.jsxs("div",{style:{marginBottom:8},children:[t.jsx("label",{style:c,children:"Content"}),t.jsx("textarea",{value:e.content,onChange:l=>n({content:l.target.value}),style:{...o,height:72,resize:"none"}})]}),t.jsxs("div",{style:{display:"grid",gridTemplateColumns:"1fr 1fr",gap:6,marginBottom:8},children:[t.jsxs("div",{children:[t.jsx("label",{style:c,children:"Font Size"}),t.jsx("input",{type:"number",value:e.fontSize,min:6,max:96,onChange:l=>n({fontSize:Number(l.target.value)}),style:o})]}),t.jsxs("div",{children:[t.jsx("label",{style:c,children:"Color"}),t.jsx("input",{type:"color",value:e.color,onChange:l=>n({color:l.target.value}),style:{width:"100%",height:28,border:"none",padding:0,cursor:"pointer",background:"none"}})]})]}),t.jsxs("div",{style:{display:"flex",gap:4},children:[t.jsx("button",{onClick:()=>n({fontWeight:e.fontWeight==="bold"?"normal":"bold"}),style:{...C(e.fontWeight==="bold"),flex:1,fontWeight:"bold"},children:"B"}),t.jsx("button",{onClick:()=>n({fontStyle:e.fontStyle==="italic"?"normal":"italic"}),style:{...C(e.fontStyle==="italic"),flex:1,fontStyle:"italic"},children:"I"}),["left","center","right"].map(l=>t.jsx("button",{onClick:()=>n({align:l}),style:{...C(e.align===l),flex:1},children:l==="left"?"⇤":l==="center"?"⇔":"⇥"},l))]})]}),(e.type==="rectangle"||e.type==="circle")&&t.jsxs("div",{style:d,children:[t.jsx("div",{style:T,children:"Appearance"}),v("Fill","fill",e.fill),v("Stroke","stroke",e.stroke),t.jsxs("div",{style:{marginBottom:e.type==="rectangle"?8:0},children:[t.jsx("label",{style:c,children:"Stroke Width"}),t.jsx("input",{type:"number",value:e.strokeWidth,min:0,max:20,onChange:l=>n({strokeWidth:Number(l.target.value)}),style:o})]}),e.type==="rectangle"&&t.jsxs("div",{children:[t.jsx("label",{style:c,children:"Corner Radius"}),t.jsx("input",{type:"number",value:e.rx,min:0,max:140,onChange:l=>n({rx:Number(l.target.value)}),style:o})]})]}),e.type==="image"&&t.jsxs("div",{style:d,children:[t.jsx("div",{style:T,children:"Image"}),t.jsx("label",{style:c,children:"URL or Data URI"}),t.jsx("input",{type:"text",value:e.src,style:o,placeholder:"https://…",onChange:l=>n({src:l.target.value})})]}),t.jsxs("div",{style:{display:"flex",gap:6,marginBottom:8},children:[t.jsx("button",{onClick:()=>s(-1),style:{...a,flex:1},children:"↓ Send Back"}),t.jsx("button",{onClick:()=>s(1),style:{...a,flex:1},children:"↑ Bring Fwd"})]}),t.jsx("button",{onClick:r,style:{width:"100%",background:"#3a0c0c",border:"1px solid #8a2020",color:"#e06060",borderRadius:4,padding:"6px",cursor:"pointer",fontSize:11},children:"Delete Element"})]})}const Nt=oe+2*I,Xt=ae+2*I;function qt({design:e,uid:n,style:r,className:s}){const i=`cp-shadow-${n}`,o=`cp-clip-${n}`;return t.jsxs("svg",{viewBox:`0 0 ${Nt} ${Xt}`,preserveAspectRatio:"xMidYMid meet",style:{display:"block",...r},className:s,children:[t.jsxs("defs",{children:[t.jsx("filter",{id:i,x:"-30%",y:"-20%",width:"160%",height:"150%",children:t.jsx("feDropShadow",{dx:"0",dy:"4",stdDeviation:"8",floodColor:"rgba(0,0,0,0.55)"})}),t.jsx("clipPath",{id:o,children:t.jsx("rect",{x:I,y:I,width:oe,height:ae,rx:Be})})]}),t.jsx("rect",{x:I,y:I,width:oe,height:ae,rx:Be,fill:e.background.color,filter:`url(#${i})`}),e.background.imageUrl&&t.jsx("image",{href:e.background.imageUrl,x:I,y:I,width:oe,height:ae,clipPath:`url(#${o})`,preserveAspectRatio:"xMidYMid slice"}),t.jsx("g",{clipPath:`url(#${o})`,children:e.elements.map((c,d)=>t.jsx(Ot,{el:c,meta:e.meta},c.id??d))}),t.jsx("rect",{x:I,y:I,width:oe,height:ae,rx:Be,fill:"none",stroke:"rgba(255,255,255,0.13)",strokeWidth:1.5,pointerEvents:"none"})]})}function Ot({el:e,meta:n}){return e.type==="rectangle"?t.jsx("rect",{x:I+e.x,y:I+e.y,width:e.w,height:e.h,rx:e.rx,fill:e.fill,stroke:e.stroke,strokeWidth:e.strokeWidth}):e.type==="circle"?t.jsx("ellipse",{cx:I+e.x+e.w/2,cy:I+e.y+e.h/2,rx:e.w/2,ry:e.h/2,fill:e.fill,stroke:e.stroke,strokeWidth:e.strokeWidth}):e.type==="image"?t.jsx("image",{href:e.src,x:I+e.x,y:I+e.y,width:e.w,height:e.h,preserveAspectRatio:"xMidYMid slice"}):e.type==="text"?t.jsx("foreignObject",{x:I+e.x,y:I+e.y,width:e.w,height:e.h,style:{overflow:"visible"},children:t.jsx("div",{style:{width:"100%",height:"100%",overflow:"hidden",fontSize:`${e.fontSize}px`,fontWeight:e.fontWeight,fontStyle:e.fontStyle,color:e.color,textAlign:e.align,lineHeight:1.35,boxSizing:"border-box",padding:"2px 4px",whiteSpace:"pre-wrap",wordBreak:"break-word"},children:gt(e.content,n)})}):null}const pe=new Map,Xe=new Map,$e=new Map;async function Ge(e){const n=await fetch(e.url);if(!n.ok)throw new Error(`HTTP ${n.status} fetching ${e.url}`);const r=await n.json(),s=[],i=[];for(const c of r)pe.has(c.id)?i.push(c.id):(pe.set(c.id,c),Xe.set(c.id,e.id),s.push(c.id));const o={info:e,blockIds:s,conflictIds:i};return $e.set(e.id,o),o}function ft(e){const n=$e.get(e);if(n){for(const r of n.blockIds)pe.delete(r),Xe.delete(r);$e.delete(e)}}function _e(){return Array.from($e.values())}function Me(e){const n=pe.get(e);if(!n)throw new Error(`Block definition not found: "${e}"`);return n}function te(e){return pe.get(e)}function bt(){return[...pe.values()]}function Vt(){return[...new Set(bt().map(e=>e.category))]}function Qt(e,n){const r=[],s=[];for(const o of n)pe.has(o.id)?s.push(o.id):(pe.set(o.id,o),Xe.set(o.id,e.id),r.push(o.id));const i={info:e,blockIds:r,conflictIds:s};return $e.set(e.id,i),i}async function Ut(e){pe.clear(),Xe.clear(),$e.clear(),await Ge({id:"__core__",name:"Core",description:"Built-in blocks",url:e,builtIn:!0})}const Kt=36,de=50,ge=24,it=26,et=12,pt=12,vt=8,kt=6,jt=new Set(["number","string","boolean"]);function St(e){return e.blockType==="query"&&e.returnType?`${e.name} [${e.returnType}]`:e.name}const Zt={number:{w:44,h:ge},string:{w:60,h:ge},boolean:{w:52,h:ge}},Jt={w:75,h:ge};function wt(e){return Zt[e]??Jt}function It(e,n,r){if(n?.queryBlockId&&r){const s=r.get(n.queryBlockId);if(s){const i=te(s.definitionId);if(i)return ve(i,s.paramSlots,r)}}return wt(e).w}function st(e){return Math.max(72,e.length*6+22)}function ve(e,n,r){let s=et+pt+St(e).length*kt;for(let o=0;o<e.params.length;o++)s+=vt+It(e.params[o].type,n?.[o],r);if(e.blockType==="start"&&e.provides)for(const o of e.provides)s=Math.max(s,et+st(o.name)+pt);const i=e.blockType==="start"?140:110;return Math.max(i,s)}function ke(e){if(e.blockType==="start"){const n=e.provides?.length??0;return de+(n>0?8+n*it:0)}return e.blockType==="command"?Kt:ge}function Fe(e,n,r){const s=e.blockType==="start"?de:ke(e),i=[];let o=et+St(e).length*kt;for(let c=0;c<e.params.length;c++){const d=e.params[c],T=It(d.type,n?.[c],r),a=wt(d.type).h;o+=vt,i.push({x:o,y:(s-a)/2,w:T,h:a,type:d.type,index:c}),o+=T}return i}const be=16,Le=14,tt=be+Le/2,nt=5,ee=4;function lt(e,n){return[`M ${ee} 0`,`H ${be} L ${tt} ${nt} L ${be+Le} 0`,`H ${e-ee} Q ${e} 0 ${e} ${ee}`,`V ${n-ee} Q ${e} ${n} ${e-ee} ${n}`,`H ${be+Le} L ${tt} ${n+nt} L ${be} ${n}`,`H ${ee} Q 0 ${n} 0 ${n-ee}`,`V ${ee} Q 0 0 ${ee} 0 Z`].join(" ")}function ct(e,n){return["M 0 10 Q 0 0 10 0",`H ${e-10} Q ${e} 0 ${e} 10`,`V ${n-ee} Q ${e} ${n} ${e-ee} ${n}`,`H ${be+Le} L ${tt} ${n+nt} L ${be} ${n}`,`H ${ee} Q 0 ${n} 0 ${n-ee}`,"V 10 Z"].join(" ")}function Gt(e,n){const r=n/2;return[`M ${r} 0 H ${e-r}`,`A ${r} ${r} 0 0 1 ${e-r} ${n}`,`H ${r} A ${r} ${r} 0 0 1 ${r} 0 Z`].join(" ")}function en(e,n){const r=n/2;return[`M ${r} 0 H ${e-r}`,`L ${e} ${n/2} L ${e-r} ${n}`,`H ${r} L 0 ${n/2} Z`].join(" ")}function Bt(e,n,r){return[`M ${r} 0 H ${e-r} Q ${e} 0 ${e} ${r}`,`V ${n-r} Q ${e} ${n} ${e-r} ${n}`,`H ${r} Q 0 ${n} 0 ${n-r}`,`V ${r} Q 0 0 ${r} 0 Z`].join(" ")}function tn(e,n){return Bt(e,n,5)}function nn(e,n){return Bt(e,n,9)}function Ee(e,n,r){switch(e){case"number":return Gt(n,r);case"boolean":return en(n,r);case"string":return tn(n,r);default:return nn(n,r)}}function $t(e,n,r){return Ee(e,n,r)}function Tt({def:e,onPointerDown:n,isPlaced:r}){const s=J(),i=ve(e),o=ke(e),c=Fe(e),d=`pal-${e.id}`;let T;return e.blockType==="start"?T=ct(i,o):e.blockType==="command"?T=lt(i,o):T=Ee(e.returnType,i,ge),t.jsx("div",{style:{marginBottom:6,userSelect:"none",position:"relative",opacity:r?.4:1},onPointerDown:r?void 0:a=>n(a,e.id),title:r?"Already on canvas — only one instance allowed":void 0,children:t.jsxs("svg",{width:i+2,height:o+8,style:{display:"block",cursor:r?"not-allowed":"grab",overflow:"visible"},children:[t.jsx("defs",{children:t.jsx("clipPath",{id:d,children:t.jsx("path",{d:T})})}),t.jsxs("g",{transform:"translate(1, 1)",children:[t.jsx("path",{d:T,fill:s.blockBody,stroke:s.blockBorder,strokeWidth:1.5}),t.jsx("rect",{x:0,y:0,width:4,height:o+20,fill:e.color,clipPath:`url(#${d})`}),t.jsxs("text",{x:11,y:(e.blockType==="start"?de:o)/2+4,fill:s.blockText,fontSize:11,fontWeight:"bold",fontFamily:"sans-serif",style:{userSelect:"none",pointerEvents:"none"},children:[e.name,e.blockType==="query"&&e.returnType&&t.jsx("tspan",{fill:s.textMuted,fontSize:9,fontWeight:"normal",children:` [${e.returnType}]`})]}),r&&t.jsx("text",{x:i/2,y:o/2+4,textAnchor:"middle",fill:s.textMuted,fontSize:9,fontFamily:"sans-serif",style:{userSelect:"none",pointerEvents:"none"},children:"✓ on canvas"}),c.map(a=>{const C=e.params[a.index]?.name,$=!jt.has(a.type);return t.jsxs("g",{transform:`translate(${a.x},${a.y})`,children:[t.jsx("path",{d:$t(a.type,a.w,a.h),fill:s.socketBody,stroke:s.blockBorder,strokeWidth:1}),$?t.jsx("text",{x:a.w/2,y:a.h/2+3,textAnchor:"middle",fill:s.textSecondary,fontSize:9,fontFamily:"sans-serif",style:{userSelect:"none",pointerEvents:"none"},children:a.type}):C?t.jsx("text",{x:a.w/2,y:a.h/2+3,textAnchor:"middle",fill:s.textMuted,fontSize:9,fontFamily:"sans-serif",fontStyle:"italic",style:{userSelect:"none",pointerEvents:"none"},children:C}):null]},a.index)}),e.blockType==="start"&&e.provides&&e.provides.length>0&&t.jsxs("g",{children:[t.jsx("line",{x1:8,y1:de,x2:i-8,y2:de,stroke:"rgba(255,255,255,0.15)",strokeWidth:1}),e.provides.map((a,C)=>{const v=te(a.id)?.color??e.color,l=st(a.name),k=20;return t.jsxs("g",{transform:`translate(8, ${de+6+C*it})`,children:[t.jsx("rect",{x:0,y:0,width:l,height:k,rx:10,fill:v,opacity:.82,stroke:"rgba(255,255,255,0.25)",strokeWidth:1}),t.jsx("text",{x:l/2,y:k/2+3.5,textAnchor:"middle",fill:"white",fontSize:9.5,fontFamily:"sans-serif",fontWeight:"bold",style:{userSelect:"none",pointerEvents:"none"},children:a.name})]},a.id)})]})]})]})})}function rn({cat:e,defs:n,collapsed:r,onToggle:s,onPointerDown:i,placedStartIds:o}){const c=J();return t.jsxs("div",{style:{marginBottom:2},children:[t.jsxs("button",{onClick:()=>s(e),style:{display:"flex",alignItems:"center",gap:6,width:"100%",background:"none",border:"none",padding:"5px 0 4px",borderBottom:`1px solid ${c.border}`,cursor:"pointer",textAlign:"left",marginBottom:r?0:6},children:[t.jsx("span",{style:{color:c.accent,fontSize:8,lineHeight:1,opacity:.6},children:r?"▶":"▼"}),t.jsx("span",{style:{color:c.accent,fontSize:10,fontWeight:"bold",textTransform:"uppercase",letterSpacing:1,opacity:.85},children:e}),r&&t.jsx("span",{style:{color:c.textMuted,fontSize:9,marginLeft:"auto"},children:n.length})]}),!r&&t.jsx("div",{style:{paddingBottom:4},children:n.map(d=>t.jsx(Tt,{def:d,onPointerDown:i,isPlaced:d.blockType==="start"&&o.has(d.id)},d.id))})]})}function on({pkg:e,loaded:n,toggling:r,onToggle:s}){const i=J(),o=!!n;return t.jsxs("div",{style:{display:"flex",alignItems:"flex-start",justifyContent:"space-between",gap:8,padding:"10px 12px",marginBottom:6,background:o?i.pkgActiveBg:i.pkgBg,border:`1px solid ${o?i.pkgActiveBorder:i.pkgBorder}`,borderRadius:5},children:[t.jsxs("div",{style:{flex:1,minWidth:0},children:[t.jsxs("div",{style:{display:"flex",alignItems:"center",gap:6,marginBottom:2},children:[t.jsx("span",{style:{color:o?i.pkgActiveText:i.pkgText,fontSize:11,fontWeight:"bold",letterSpacing:.3},children:e.name}),n&&n.conflictIds.length>0&&t.jsxs("span",{title:`${n.conflictIds.length} block ID(s) skipped due to conflicts`,style:{fontSize:9,color:i.accent,border:`1px solid ${i.accent}`,borderRadius:3,padding:"1px 4px",opacity:.7},children:["⚠ ",n.conflictIds.length]})]}),t.jsx("div",{style:{color:i.pkgSubText,fontSize:10},children:o?`${n.blockIds.length} block${n.blockIds.length!==1?"s":""} active`:e.description})]}),t.jsx("button",{disabled:r,onClick:()=>s(e,o),title:o?`Remove ${e.name}`:`Add ${e.name}`,style:{flexShrink:0,marginTop:1,width:22,height:22,background:"none",border:`1px solid ${o?"#804040":"#407040"}`,borderRadius:4,color:o?"#c05050":"#508050",fontSize:16,lineHeight:1,cursor:r?"wait":"pointer",display:"flex",alignItems:"center",justifyContent:"center",padding:0,opacity:r?.4:1,transition:"opacity 0.1s"},children:r?"…":o?"×":"+"})]})}function sn({onPointerDown:e,isDropTarget:n,registryVersion:r,availablePackages:s,loadedPackages:i,onPackageToggle:o,placedStartIds:c}){const d=J(),[T,a]=m.useState("blocks"),[C,$]=m.useState(""),[v,l]=m.useState(new Set),[k,g]=m.useState(null),P=m.useMemo(()=>bt().filter(x=>!x.category.startsWith("$")),[r]),w=m.useMemo(()=>Vt().filter(x=>!x.startsWith("$")),[r]),H=C.trim().toLowerCase(),Q=H?P.filter(x=>x.name.toLowerCase().includes(H)):null;function ne(x){l(F=>{const K=new Set(F);return K.has(x)?K.delete(x):K.add(x),K})}async function D(x,F){g(x.id);try{await o(x,F)}finally{g(null)}}const u=new Map(i.map(x=>[x.info.id,x])),M=i.filter(x=>x.info.id!=="__core__").length,_=x=>({flex:1,background:"none",border:"none",borderBottom:`2px solid ${T===x?d.accent:"transparent"}`,color:T===x?d.accent:d.textMuted,fontSize:10,fontWeight:"bold",textTransform:"uppercase",letterSpacing:1,padding:"8px 0",cursor:"pointer",transition:"color 0.15s, border-color 0.15s"});return t.jsxs("div",{style:{width:260,minWidth:260,backgroundColor:d.paletteBg,borderRight:`1px solid ${d.border}`,display:"flex",flexDirection:"column",height:"100%",overflow:"hidden"},children:[t.jsxs("div",{style:{display:"flex",borderBottom:`1px solid ${d.border}`,flexShrink:0,background:d.paletteHeaderBg},children:[t.jsx("button",{style:_("blocks"),onClick:()=>a("blocks"),children:"Blocks"}),t.jsxs("button",{style:_("packages"),onClick:()=>a("packages"),children:["Packages",M>0?` · ${M}`:""]})]}),T==="blocks"&&t.jsxs(t.Fragment,{children:[t.jsx("div",{style:{padding:"8px 10px 6px",flexShrink:0},children:t.jsx("input",{type:"search",placeholder:"Search blocks…",value:C,onChange:x=>$(x.target.value),style:{width:"100%",boxSizing:"border-box",background:d.inputBg,border:`1px solid ${d.inputBorder}`,borderRadius:4,color:d.textPrimary,fontSize:11,padding:"5px 9px",outline:"none"}})}),t.jsx("div",{style:{flex:1,overflowY:"auto",padding:"4px 10px 0"},children:Q?Q.length>0?Q.map(x=>t.jsx(Tt,{def:x,onPointerDown:e,isPlaced:x.blockType==="start"&&c.has(x.id)},x.id)):t.jsx("div",{style:{color:d.textMuted,fontSize:11,padding:"12px 0",textAlign:"center"},children:"No blocks match"}):w.map(x=>t.jsx(rn,{cat:x,defs:P.filter(F=>F.category===x),collapsed:v.has(x),onToggle:ne,onPointerDown:e,placedStartIds:c},x))}),t.jsx("div",{style:{flexShrink:0,margin:"8px 10px 10px",padding:"7px 10px",borderRadius:4,background:n?d.deleteActiveBg:"transparent",border:`1px dashed ${n?d.deleteActiveBorder:d.deleteBorder}`,color:n?d.deleteActiveText:d.deleteText,textAlign:"center",fontSize:10,fontWeight:"bold",letterSpacing:.5,textTransform:"uppercase",transition:"background 0.12s, color 0.12s, border-color 0.12s",pointerEvents:"none",userSelect:"none"},children:n?"Release to delete":"Drag here to delete"})]}),T==="packages"&&t.jsx("div",{style:{flex:1,overflowY:"auto",padding:"10px"},children:s.length===0?t.jsx("div",{style:{color:d.textMuted,fontSize:11,textAlign:"center",padding:"20px 0"},children:"No packages available"}):s.map(x=>t.jsx(on,{pkg:x,loaded:u.get(x.id),toggling:k===x.id,onToggle:D},x.id))})]})}function ln(e,n,r){let s=e;for(;;)if(s.embeddedInBlockId){const i=r.get(s.embeddedInBlockId);if(!i)return!1;s=i}else if(s.parentBlockId){const i=r.get(s.parentBlockId);if(!i)return!1;s=i}else return te(s.definitionId)?.provides?.some(o=>o.id===n)??!1}function cn(e,n){const r=te(e.definitionId),s=ve(r,e.paramSlots,n),i=ke(r);return r.blockType==="start"?ct(s,i):r.blockType==="command"?lt(s,i):Ee(r.returnType,s,i)}function at({block:e,allBlocks:n,onPointerDown:r,onBlockClick:s,onSocketClick:i,editingSocket:o,onSocketCommit:c,onSocketCancelEdit:d,onOutputDrag:T,isGhost:a,isDragging:C,embedded:$}){const v=J(),l=te(e.definitionId);if(!l){const u=r?x=>{x.stopPropagation(),r(x,e.instanceId)}:void 0,M=s?x=>{x.stopPropagation(),s(e.instanceId)}:void 0,_=e.definitionId.replace(/^__coll__/,"").slice(0,16);return t.jsxs("g",{transform:a||$?void 0:`translate(${e.x},${e.y})`,opacity:C?.35:.85,onPointerDown:u,onClick:M,style:{cursor:"grab"},children:[t.jsx("rect",{x:0,y:0,width:160,height:24,rx:3,fill:"rgba(160,40,40,0.18)",stroke:"#c05050",strokeWidth:1.5,strokeDasharray:"5,3"}),t.jsxs("text",{x:8,y:16,fill:"#c05050",fontSize:10,fontFamily:"sans-serif",fontStyle:"italic",style:{userSelect:"none",pointerEvents:"none"},children:["⚠ ",_," (missing)"]})]})}const k=ve(l,e.paramSlots,n),g=ke(l),P=Fe(l,e.paramSlots,n),w=a?.75:C?.35:1,H=`clip-${a?"g":"c"}-${e.instanceId}`,Q=cn(e,n),ne=r?u=>{u.stopPropagation(),r(u,e.instanceId)}:void 0,D=s?u=>{u.stopPropagation(),s(e.instanceId)}:void 0;if(l.category.startsWith("$")&&!a&&!ln(e,l.id,n)){const u=ve(l),M=ge,_=Ee(l.returnType??"string",u,M);return t.jsxs("g",{transform:a||$?void 0:`translate(${e.x},${e.y})`,opacity:C?.35:.85,onPointerDown:ne,onClick:D,style:{cursor:"grab"},children:[t.jsx("path",{d:_,fill:"rgba(180,100,0,0.15)",stroke:"#c08820",strokeWidth:1.5,strokeDasharray:"5,3"}),t.jsxs("text",{x:u/2,y:M/2+3.5,textAnchor:"middle",fill:"#c08820",fontSize:9,fontFamily:"sans-serif",fontStyle:"italic",style:{userSelect:"none",pointerEvents:"none"},children:["⚠ ",l.name]})]})}return t.jsxs("g",{transform:a||$?void 0:`translate(${e.x},${e.y})`,opacity:w,onPointerDown:ne,onClick:D,style:{cursor:a?"grabbing":"grab"},children:[t.jsx("defs",{children:t.jsx("clipPath",{id:H,children:t.jsx("path",{d:Q})})}),t.jsx("path",{d:Q,fill:v.blockBody,stroke:v.blockBorder,strokeWidth:1.5}),t.jsx("rect",{x:0,y:0,width:4,height:g+20,fill:l.color,clipPath:`url(#${H})`}),t.jsxs("text",{x:11,y:(l.blockType==="start"?de:g)/2+4,fill:v.blockText,fontSize:11,fontWeight:"bold",fontFamily:"sans-serif",style:{userSelect:"none",pointerEvents:"none"},children:[l.name,l.blockType==="query"&&l.returnType&&t.jsx("tspan",{fill:v.textMuted,fontSize:9,fontWeight:"normal",children:` [${l.returnType}]`})]}),l.blockType==="start"&&l.provides&&l.provides.length>0&&t.jsxs("g",{children:[t.jsx("line",{x1:8,y1:de,x2:k-8,y2:de,stroke:"rgba(255,255,255,0.15)",strokeWidth:1}),l.provides.map((u,M)=>{const x=te(u.id)?.color??l.color,F=st(u.name),K=20,ue=8,fe=de+6+M*it,N=!a&&!$;return t.jsxs("g",{transform:`translate(${ue}, ${fe})`,onPointerDown:N?xe=>{xe.stopPropagation(),T?.(xe,u.id)}:void 0,style:{cursor:N?"grab":"default"},children:[t.jsx("rect",{x:0,y:0,width:F,height:K,rx:10,fill:x,opacity:.82,stroke:"rgba(255,255,255,0.25)",strokeWidth:1}),t.jsx("text",{x:F/2,y:K/2+3.5,textAnchor:"middle",fill:"white",fontSize:9.5,fontFamily:"sans-serif",fontWeight:"bold",style:{userSelect:"none",pointerEvents:"none"},children:u.name})]},u.id)})]}),P.map(u=>{const M=e.paramSlots[u.index],_=M?.queryBlockId,x=_?n.get(_):null;if(x)return t.jsx("g",{transform:`translate(${u.x},${u.y})`,children:t.jsx(at,{block:x,allBlocks:n,onPointerDown:r,onBlockClick:s,onSocketClick:i,editingSocket:o,onSocketCommit:c,onSocketCancelEdit:d,onOutputDrag:T,embedded:!0})},u.index);const F=M?.textValue,K=l.params[u.index]?.name,ue=!jt.has(u.type),fe=o?.blockId===e.instanceId&&o?.slotIndex===u.index;return t.jsxs("g",{transform:`translate(${u.x},${u.y})`,onPointerDown:N=>N.stopPropagation(),onClick:N=>{N.stopPropagation(),i?.(e.instanceId,u.index)},style:{cursor:"text"},children:[t.jsx("path",{d:$t(u.type,u.w,u.h),fill:fe?v.blockBorder:v.socketBody,stroke:v.blockBorder,strokeWidth:fe?1.5:1}),fe?t.jsx("foreignObject",{x:0,y:0,width:u.w,height:u.h,children:t.jsx("input",{autoFocus:!0,defaultValue:F??"",onKeyDown:N=>{N.stopPropagation(),N.key==="Enter"&&c?.(e.instanceId,u.index,N.target.value),N.key==="Escape"&&d?.()},onBlur:N=>c?.(e.instanceId,u.index,N.target.value),onPointerDown:N=>N.stopPropagation(),style:{width:"100%",height:"100%",background:"transparent",border:"none",outline:"none",color:v.blockText,fontSize:10,fontFamily:"sans-serif",textAlign:"center",padding:"0 4px",boxSizing:"border-box",caretColor:v.blockText}})}):F?t.jsx("text",{x:u.w/2,y:u.h/2+4,textAnchor:"middle",fill:v.blockText,fontSize:10,fontFamily:"sans-serif",style:{userSelect:"none",pointerEvents:"none"},children:F}):ue?t.jsx("text",{x:u.w/2,y:u.h/2+3,textAnchor:"middle",fill:v.textSecondary,fontSize:9,fontFamily:"sans-serif",style:{userSelect:"none",pointerEvents:"none"},children:u.type}):K?t.jsx("text",{x:u.w/2,y:u.h/2+3,textAnchor:"middle",fill:v.textMuted,fontSize:9,fontFamily:"sans-serif",fontStyle:"italic",style:{userSelect:"none",pointerEvents:"none"},children:K}):null]},u.index)})]})}const Ct=m.forwardRef(({blocks:e,draggingIds:n,onBlockPointerDown:r,onBlockClick:s,onSocketClick:i,editingSocket:o,onSocketCommit:c,onSocketCancelEdit:d,onOutputDrag:T},a)=>{const C=J(),$=[...e.values()].filter(v=>!v.embeddedInBlockId);return t.jsxs("svg",{ref:a,style:{flex:1,background:C.canvasBg,display:"block"},onPointerDown:v=>{v.target===v.currentTarget&&v.preventDefault()},children:[t.jsx("defs",{children:t.jsx("pattern",{id:"grid",width:"28",height:"28",patternUnits:"userSpaceOnUse",children:t.jsx("circle",{cx:"14",cy:"14",r:"0.7",fill:C.gridDot})})}),t.jsx("rect",{width:"100%",height:"100%",fill:"url(#grid)"}),$.map(v=>t.jsx(at,{block:v,allBlocks:e,onPointerDown:r,onBlockClick:s,onSocketClick:i,isDragging:n.has(v.instanceId),editingSocket:o,onSocketCommit:c,onSocketCancelEdit:d,onOutputDrag:T},v.instanceId))]})});Ct.displayName="Canvas";let rt=1;function an(){return`b${rt++}`}function Ne(e,n){const r=[],s=[...e];for(;s.length>0;){const i=s.shift();r.push(i),n.get(i)?.paramSlots.forEach(o=>{o.queryBlockId&&s.push(o.queryBlockId)})}return r}function dn(e){const n=[];if(e.forEach(i=>{te(i.definitionId)||n.push(i.instanceId)}),n.length===0)return e;const r=new Map(e);for(const i of n){const o=r.get(i);if(o){if(o.parentBlockId||o.nextBlockId){const c=o.parentBlockId?r.get(o.parentBlockId):null,d=o.nextBlockId?r.get(o.nextBlockId):null;c&&r.set(o.parentBlockId,{...c,nextBlockId:o.nextBlockId??null}),d&&r.set(o.nextBlockId,{...d,parentBlockId:o.parentBlockId??null})}if(o.embeddedInBlockId&&o.embeddedSlotIndex>=0){const c=r.get(o.embeddedInBlockId);if(c){const d=[...c.paramSlots];d[o.embeddedSlotIndex]={textValue:null,queryBlockId:null},r.set(o.embeddedInBlockId,{...c,paramSlots:d})}}}}return new Set(Ne(n,e)).forEach(i=>r.delete(i)),r}function un(e,n,r){const s=Me(e);return{instanceId:an(),definitionId:e,x:n,y:r,nextBlockId:null,parentBlockId:null,embeddedInBlockId:null,embeddedSlotIndex:-1,paramSlots:s.params.map(()=>({textValue:null,queryBlockId:null}))}}function xt(e,n){const r=[];let s=e;for(;s;)r.push(s),s=n.get(s)?.nextBlockId??null;return r}function Pt(e,n){const r=n.get(e);return r?.parentBlockId?Pt(r.parentBlockId,n):e}function Mt(e,n){const r=te(e.definitionId);if(!r)return`[missing:${e.definitionId}]`;const s=e.paramSlots.map((i,o)=>{if(i.queryBlockId){const c=n.get(i.queryBlockId);if(c)return Mt(c,n)}return i.textValue!==null&&i.textValue!==void 0?r.params[o]?.type==="string"?`"${i.textValue}"`:i.textValue:"_"});return`${r.id}(${s.join(", ")})`}function fn(e,n){const r=new Map(e);return Ne(n,e).forEach(s=>r.delete(s)),r}function pn(e,n,r,s){const i=e.get(n);if(!i)return e;const o=[...i.paramSlots];o[r]={...o[r],textValue:s||null};const c=new Map(e);return c.set(n,{...i,paramSlots:o}),c}function xn({ghostPos:e,dragRef:n,blocks:r}){const s=J();if(!e)return null;const i=n.current;if(i?.type==="canvas"&&i.instanceId){const $=r.get(i.instanceId);if($)return t.jsx("svg",{style:{position:"fixed",top:0,left:0,width:"100%",height:"100%",pointerEvents:"none",zIndex:9999},children:t.jsx("g",{transform:`translate(${e.x},${e.y})`,opacity:.8,children:t.jsx(at,{block:$,allBlocks:r,isGhost:!0})})})}const o=Me(e.defId),c=ve(o),d=ke(o),T=o.blockType==="query"?ge:d,a=o.blockType==="command";let C;return o.blockType==="start"?C=ct(c,d):o.blockType==="command"?C=lt(c,T):C=Ee(o.returnType,c,T),t.jsxs("svg",{style:{position:"fixed",top:0,left:0,width:"100%",height:"100%",pointerEvents:"none",zIndex:9999},children:[a&&t.jsx("defs",{children:t.jsx("clipPath",{id:"ghost-pal-clip",children:t.jsx("path",{d:C})})}),t.jsxs("g",{transform:`translate(${e.x},${e.y})`,opacity:.8,children:[t.jsx("path",{d:C,fill:a?s.blockBody:o.color,stroke:a?s.blockBorder:"rgba(0,0,0,0.4)",strokeWidth:1.5}),a&&t.jsx("rect",{x:0,y:0,width:4,height:T+20,fill:o.color,clipPath:"url(#ghost-pal-clip)"}),t.jsx("text",{x:11,y:T/2+4,fill:a?s.blockText:"white",fontSize:11,fontWeight:"bold",fontFamily:"sans-serif",style:{userSelect:"none"},children:o.name})]})]})}function Et({blocksUrl:e="/blocks.json",blocksUrls:n,onRun:r,onThemeChange:s,navTabs:i,initialState:o,onSave:c,themeId:d,hideTitleBar:T,getStateRef:a,additionalBlocks:C}){const[$,v]=m.useState(()=>{if(d)return we.find(j=>j.id===d)??he;const f=localStorage.getItem("cardcq-theme");return we.find(j=>j.id===f)??he});m.useEffect(()=>{if(!d)return;const f=we.find(j=>j.id===d)??he;v(f)},[d]);const[l,k]=m.useState(!1),[g,P]=m.useState(null),[w,H]=m.useState(()=>{if(!o)return new Map;const f=new Map(o.blocks);return f.forEach((j,y)=>{const S=parseInt(y.slice(1),10);!isNaN(S)&&S>=rt&&(rt=S+1)}),f}),Q=m.useMemo(()=>{const f=new Set;for(const j of w.values())te(j.definitionId)?.blockType==="start"&&f.add(j.definitionId);return f},[w]),[ne,D]=m.useState(null),[u,M]=m.useState(new Set),[_,x]=m.useState(!1),[F,K]=m.useState(0),[ue,fe]=m.useState([]),[N,xe]=m.useState([]);m.useEffect(()=>{a&&(a.current=()=>({blocks:[...w.entries()]}))},[w,a]);const ie=m.useRef(null),Te=m.useRef(!1),je=m.useRef(null),Ae=(n??[e]).join("|");m.useEffect(()=>{const f=Ae.split("|");k(!1),P(null),Ut(f[0]).then(async()=>{for(let j=1;j<f.length;j++)await Ge({id:`__extra_${j}__`,name:`Extra ${j}`,description:"",url:f[j],builtIn:!0});k(!0),xe(_e())}).catch(j=>P(String(j)))},[Ae]),m.useEffect(()=>{fetch("/packages.json").then(f=>f.json()).then(fe).catch(()=>{})},[]),m.useEffect(()=>{l&&(ft("__inline__"),C&&C.length>0&&Qt({id:"__inline__",name:"Inline",description:"",url:"",builtIn:!0},C),xe(_e()),K(f=>f+1),H(f=>dn(f)))},[C,l]);const qe=m.useCallback(async(f,j)=>{if(j){const y=_e().find(S=>S.info.id===f.id);if(y){const S=new Set(y.blockIds);H(R=>{const E=new Map(R),L=[];E.forEach(b=>{S.has(b.definitionId)&&L.push(b.instanceId)});const Y=new Set(Ne(L,E));return E.forEach(b=>{if(Y.has(b.instanceId))return;let O=!1,h=b;b.nextBlockId&&Y.has(b.nextBlockId)&&(h={...h,nextBlockId:null},O=!0),b.parentBlockId&&Y.has(b.parentBlockId)&&(h={...h,parentBlockId:null},O=!0),O&&E.set(b.instanceId,h)}),Y.forEach(b=>E.delete(b)),E})}ft(f.id)}else await Ge(f);xe(_e()),K(y=>y+1)},[]),se=m.useCallback((f,j)=>{const y=je.current?.getBoundingClientRect()??{left:0,top:0};return{x:f-y.left,y:j-y.top}},[]),le=m.useCallback((f,j,y)=>{let R=null;return w.forEach(E=>{if(y.has(E.instanceId)||E.embeddedInBlockId||E.nextBlockId)return;const L=te(E.definitionId);if(!L||L.blockType==="query")return;const Y=E.y+ke(L),b=Math.abs(f-E.x),O=Math.abs(j-Y);if(b<40&&O<24){const h=Math.hypot(b,O);(!R||h<R.dist)&&(R={blockId:E.instanceId,snapX:E.x,snapY:Y,dist:h})}}),R},[w]),Ce=m.useCallback((f,j,y,S)=>{const R=te(y);if(!R||R.blockType!=="query")return null;const E=20;let L=null;return w.forEach(Y=>{if(S.has(Y.instanceId))return;const b=te(Y.definitionId);if(!b)return;Fe(b,Y.paramSlots,w).forEach(h=>{if(h.type!==R.returnType||Y.paramSlots[h.index]?.queryBlockId)return;const V=Y.x+h.x+h.w/2,ce=Y.y+h.y+h.h/2,U=Math.abs(f+h.w/2-V),re=Math.abs(j+h.h/2-ce);if(U<h.w/2+E&&re<h.h/2+E){const z=Math.hypot(U,re);(!L||z<L.dist)&&(L={blockId:Y.instanceId,slotIndex:h.index,snapX:Y.x+h.x,snapY:Y.y+h.y,dist:z})}})}),L},[w]),Re=m.useCallback(f=>{if(ie.current?.moved)return;const j=Pt(f,w),S=xt(j,w).map(R=>w.get(R)).filter(Boolean).map(R=>Mt(R,w));r?.(S)},[w,r]),[p,B]=m.useState(null),A=m.useCallback((f,j)=>{ie.current?.moved||B({blockId:f,slotIndex:j})},[]),W=m.useCallback((f,j,y)=>{B(null),H(S=>pn(S,f,j,y))},[]),X=m.useCallback(()=>B(null),[]),Z=m.useCallback(f=>{v(f),d||localStorage.setItem("cardcq-theme",f.id),s?.(f)},[s,d]),ye=m.useCallback((f,j)=>{f.preventDefault();const y=Me(j),S=ve(y),R=ke(y);ie.current={type:"palette",defId:j,offsetX:S/2,offsetY:R/2,startClientX:f.clientX,startClientY:f.clientY,moved:!1},D({x:f.clientX-S/2,y:f.clientY-R/2,defId:j}),f.target.setPointerCapture(f.pointerId)},[]),me=m.useCallback((f,j)=>{f.preventDefault();const y=w.get(j);if(!y)return;const{x:S,y:R}=se(f.clientX,f.clientY);let E=y.x,L=y.y;if(y.embeddedInBlockId){const V=w.get(y.embeddedInBlockId);if(V){const U=Fe(Me(V.definitionId),V.paramSlots,w)[y.embeddedSlotIndex];U&&(E=V.x+U.x,L=V.y+U.y)}}const Y=S-E,b=R-L,h=te(y.definitionId)?.blockType==="query"?[j]:xt(j,w);if(y.embeddedInBlockId||y.parentBlockId){const V=y.embeddedInBlockId,ce=y.embeddedSlotIndex,U=y.parentBlockId;H(re=>{const z=new Map(re);if(V){const q=z.get(V);if(q){const G=[...q.paramSlots];G[ce]={textValue:null,queryBlockId:null},z.set(V,{...q,paramSlots:G})}z.set(j,{...y,x:E,y:L,embeddedInBlockId:null,embeddedSlotIndex:-1})}if(U){const q=z.get(U);q&&z.set(U,{...q,nextBlockId:null});const G=z.get(j);G&&z.set(j,{...G,parentBlockId:null})}return z})}ie.current={type:"canvas",defId:y.definitionId,instanceId:j,carriedIds:h,offsetX:Y,offsetY:b,startClientX:f.clientX,startClientY:f.clientY,moved:!1},M(new Set(h)),D({x:f.clientX-Y,y:f.clientY-b,defId:y.definitionId}),f.target.setPointerCapture(f.pointerId)},[w,se]);m.useEffect(()=>{function f(y){const S=ie.current;if(S&&((Math.abs(y.clientX-S.startClientX)>4||Math.abs(y.clientY-S.startClientY)>4)&&(S.moved=!0),!!S.moved&&(D({x:y.clientX-S.offsetX,y:y.clientY-S.offsetY,defId:S.defId}),S.type==="canvas"))){const R=je.current?.getBoundingClientRect(),E=!!R&&y.clientX<R.left;E!==Te.current&&(Te.current=E,x(E))}}function j(y){const S=ie.current;if(!S||(ie.current=null,D(null),M(new Set),Te.current=!1,x(!1),!S.moved))return;const R=je.current?.getBoundingClientRect();if(!R)return;if(S.type==="canvas"&&y.clientX<R.left){H(b=>fn(b,S.carriedIds));return}const E=y.clientX-S.offsetX-R.left,L=y.clientY-S.offsetY-R.top,Y=Me(S.defId);if(S.type==="palette"){if(y.clientX<R.left||y.clientX>R.right||y.clientY<R.top||y.clientY>R.bottom||Y?.blockType==="start"&&[...w.values()].some(h=>h.definitionId===S.defId))return;const b=un(S.defId,E,L);if(Y.blockType!=="query"){const O=le(E,L,new Set);if(O){const{blockId:h,snapX:V,snapY:ce}=O;b.x=V,b.y=ce,b.parentBlockId=h,H(U=>{const re=new Map(U),z=re.get(h);return re.set(h,{...z,nextBlockId:b.instanceId}),re.set(b.instanceId,b),re});return}}else{const O=Ce(E,L,S.defId,new Set);if(O){const{blockId:h,slotIndex:V,snapX:ce,snapY:U}=O;b.x=ce,b.y=U,b.embeddedInBlockId=h,b.embeddedSlotIndex=V,H(re=>{const z=new Map(re),q=z.get(h),G=[...q.paramSlots];return G[V]={textValue:null,queryBlockId:b.instanceId},z.set(h,{...q,paramSlots:G}),z.set(b.instanceId,b),z});return}}H(O=>{const h=new Map(O);return h.set(b.instanceId,b),h})}else{const b=S.instanceId;H(O=>{const h=new Map(O),V=h.get(b);if(!V)return O;const ce=E-V.x,U=L-V.y;if(Ne(S.carriedIds,h).forEach(z=>{const q=h.get(z);q&&h.set(z,{...q,x:q.x+ce,y:q.y+U})}),Y.blockType!=="query"){const z=le(E,L,new Set(S.carriedIds));if(z){const{blockId:q,snapX:G,snapY:Ve}=z,Qe=G-E,Ue=Ve-L;S.carriedIds.forEach(Pe=>{const We=h.get(Pe);We&&h.set(Pe,{...We,x:We.x+Qe,y:We.y+Ue})});const De=h.get(q);h.set(q,{...De,nextBlockId:b}),h.set(b,{...h.get(b),parentBlockId:q})}}else{const z=Ce(E,L,S.defId,new Set([b]));if(z){const{blockId:q,slotIndex:G,snapX:Ve,snapY:Qe}=z,Ue=h.get(b);h.set(b,{...Ue,x:Ve,y:Qe,embeddedInBlockId:q,embeddedSlotIndex:G});const De=h.get(q),Pe=[...De.paramSlots];Pe[G]={textValue:null,queryBlockId:b},h.set(q,{...De,paramSlots:Pe})}}return h})}}return document.addEventListener("pointermove",f),document.addEventListener("pointerup",j),()=>{document.removeEventListener("pointermove",f),document.removeEventListener("pointerup",j)}},[w,le,Ce]);const Oe={display:"flex",flexDirection:"column",flex:1,minHeight:0,width:"100%",height:"100%",overflow:"hidden",background:$.appBg,color:$.textPrimary,fontFamily:"system-ui, sans-serif",boxSizing:"border-box"},At={display:"flex",flex:1,minHeight:0,overflow:"hidden"};return g?t.jsx(Ie.Provider,{value:$,children:t.jsxs("div",{style:{...Oe,alignItems:"center",justifyContent:"center",color:"#c06060",fontFamily:"monospace"},children:["Failed to load blocks: ",g]})}):l?t.jsx(Ie.Provider,{value:$,children:t.jsxs("div",{style:Oe,children:[!T&&t.jsxs("div",{style:{flexShrink:0,display:"flex",alignItems:"center",gap:10,padding:"0 14px",height:38,background:$.titleBg,borderBottom:`1px solid ${$.border}`},children:[t.jsx("span",{style:{color:$.titlePrimaryText,fontWeight:"bold",fontSize:13,letterSpacing:1,textTransform:"uppercase"},children:"CardCQ"}),t.jsx("span",{style:{color:$.titleSecondaryText,fontSize:11,letterSpacing:.5},children:"Behavior Editor"}),i,t.jsxs("div",{style:{marginLeft:"auto",display:"flex",alignItems:"center",gap:8},children:[c&&t.jsx("button",{onClick:()=>c({blocks:[...w.entries()]}),style:{padding:"3px 10px",fontSize:11,fontWeight:"bold",background:$.titleBg,color:$.titlePrimaryText,border:`1px solid ${$.border}`,borderRadius:4,cursor:"pointer",letterSpacing:.5},children:"Save"}),!d&&t.jsx(yt,{onSelect:Z})]})]}),t.jsxs("div",{style:At,children:[t.jsx(sn,{onPointerDown:ye,isDropTarget:_,registryVersion:F,availablePackages:ue,loadedPackages:N,onPackageToggle:qe,placedStartIds:Q}),t.jsx(Ct,{ref:je,blocks:w,draggingIds:u,onBlockPointerDown:me,onBlockClick:Re,onSocketClick:A,editingSocket:p,onSocketCommit:W,onSocketCancelEdit:X,onOutputDrag:ye})]}),t.jsx(xn,{ghostPos:ne,dragRef:ie,blocks:w})]})}):t.jsx(Ie.Provider,{value:$,children:t.jsx("div",{style:{...Oe,alignItems:"center",justifyContent:"center",color:$.textMuted},children:"Loading…"})})}exports.BlockEditor=Et;exports.CardDesigner=Ft;exports.CardPreview=qt;exports.default=Et;exports.defaultCardDesign=ht;