// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0-or-later
// @source: https://gitlab.com/soapbox-pub/ditto/-/tree/f0a3877c
import{r as e}from"./rolldown-runtime-QTnfLwEv.js";import{Tt as t,a as n,qi as r,un as i}from"./lucide-icons-BdRF0moN.js";import{t as a}from"./jsx-runtime-By8HlURe.js";import{t as o}from"./purify.es-DCh266OY.js";var s=e(r(),1),c=[`svg`,`path`,`circle`,`ellipse`,`rect`,`line`,`polyline`,`polygon`,`g`],l=`xmlns.viewBox.width.height.d.fill.stroke.stroke-width.stroke-linecap.stroke-linejoin.stroke-dasharray.stroke-dashoffset.stroke-opacity.fill-opacity.opacity.transform.cx.cy.r.rx.ry.x.y.x1.y1.x2.y2.points`.split(`.`),u=256*1024;function d(e){return e.length>u?``:o.sanitize(e,{USE_PROFILES:{svg:!0,svgFilters:!1},ALLOWED_TAGS:c,ALLOWED_ATTR:l,FORBID_TAGS:[`script`,`style`,`foreignObject`,`use`,`image`,`a`,`iframe`,`embed`,`object`],FORBID_ATTR:[`onload`,`onerror`,`onclick`,`onmouseover`,`onmouseout`,`onfocus`,`onblur`,`style`,`href`,`xlink:href`],RETURN_DOM:!1,RETURN_DOM_FRAGMENT:!1})}var f=a(),p=.5,m=4,h=14,g=5;function _(e){try{return new URL(e).protocol===`https:`}catch{return!1}}function v({sticker:e,sizeCqw:t,className:n}){return e.svg?(0,f.jsx)(`div`,{style:{width:t,height:t,maxWidth:`none`},className:`sticker-svg-wrap ${n??``}`,dangerouslySetInnerHTML:{__html:d(e.svg)}}):_(e.url)?(0,f.jsx)(`img`,{src:e.url,alt:e.shortcode,style:{width:t,height:t,maxWidth:`none`},className:n,draggable:!1,decoding:`async`}):null}function y({sticker:e}){let t=`${h*(e.scale??1)}cqw`;return(0,f.jsx)(`div`,{className:`absolute pointer-events-none select-none`,style:{left:`${e.x}%`,top:`${e.y}%`,transform:`translate(-50%, -50%) rotate(${e.rotation}deg)`,zIndex:30},children:(0,f.jsx)(v,{sticker:e,sizeCqw:t,className:`object-contain drop-shadow-md`})})}function b({sticker:e,index:r,selected:a,onSelect:o,onUpdate:c,onRemove:l,containerRef:u}){let d=e.scale??1,_=`${h*d}cqw`,y=(0,s.useRef)(!1),b=(0,s.useRef)(!1),[x,S]=(0,s.useState)(!1),C=(0,s.useCallback)((t,n)=>{let r=u.current?.getBoundingClientRect();return r?{x:Math.max(g,Math.min(100-g,(t-r.left)/r.width*100)),y:Math.max(g,Math.min(100-g,(n-r.top)/r.height*100))}:{x:e.x,y:e.y}},[u,e.x,e.y]),w=(0,s.useCallback)(e=>{if(e.preventDefault(),e.stopPropagation(),!a){o(r);return}y.current=!0,b.current=!1,S(!0),e.target.setPointerCapture(e.pointerId)},[a,r,o]),T=(0,s.useCallback)(e=>{if(!y.current)return;e.preventDefault(),b.current=!0;let{x:t,y:n}=C(e.clientX,e.clientY);c(r,{x:t,y:n})},[r,c,C]),E=(0,s.useCallback)(e=>{if(y.current){y.current=!1,S(!1);try{e.target.releasePointerCapture(e.pointerId)}catch{}}},[]),D=(0,s.useRef)(null),O=(0,s.useCallback)(()=>{let t=u.current?.getBoundingClientRect();return t?{cx:t.left+e.x/100*t.width,cy:t.top+e.y/100*t.height}:{cx:0,cy:0}},[u,e.x,e.y]),k=(0,s.useCallback)(t=>{t.preventDefault(),t.stopPropagation();let{cx:n,cy:r}=O(),i=Math.atan2(t.clientY-r,t.clientX-n)*(180/Math.PI);D.current={startAngle:i,startRotation:e.rotation},t.target.setPointerCapture(t.pointerId)},[O,e.rotation]),A=(0,s.useCallback)(e=>{if(!D.current)return;e.preventDefault();let{cx:t,cy:n}=O(),i=Math.atan2(e.clientY-n,e.clientX-t)*(180/Math.PI)-D.current.startAngle,a=D.current.startRotation+i;for(;a>180;)a-=360;for(;a<-180;)a+=360;c(r,{rotation:Math.round(a)})},[O,r,c]),j=(0,s.useCallback)(e=>{D.current=null;try{e.target.releasePointerCapture(e.pointerId)}catch{}},[]),M=(0,s.useRef)(null),N=(0,s.useCallback)(e=>{e.preventDefault(),e.stopPropagation();let{cx:t,cy:n}=O(),r=Math.hypot(e.clientX-t,e.clientY-n);M.current={startDist:r,startScale:d},e.target.setPointerCapture(e.pointerId)},[O,d]),P=(0,s.useCallback)(e=>{if(!M.current)return;e.preventDefault();let{cx:t,cy:n}=O(),i=Math.hypot(e.clientX-t,e.clientY-n)/M.current.startDist,a=Math.max(p,Math.min(m,M.current.startScale*i));c(r,{scale:Math.round(a*100)/100})},[O,r,c]),F=(0,s.useCallback)(e=>{M.current=null;try{e.target.releasePointerCapture(e.pointerId)}catch{}},[]);return(0,f.jsxs)(`div`,{className:`absolute select-none pointer-events-auto ${x?`cursor-grabbing`:a?`cursor-grab`:`cursor-pointer`}`,style:{left:`${e.x}%`,top:`${e.y}%`,transform:`translate(-50%, -50%) rotate(${e.rotation}deg)`,zIndex:a?50:30,transition:x?`none`:`filter 0.15s ease-out`,touchAction:`none`},onPointerDown:w,onPointerMove:T,onPointerUp:E,children:[a&&(0,f.jsx)(`div`,{className:`absolute border-2 border-primary border-dashed rounded-lg pointer-events-none`,style:{inset:-8}}),(0,f.jsx)(v,{sticker:e,sizeCqw:_,className:`object-contain drop-shadow-lg`}),a&&(0,f.jsxs)(f.Fragment,{children:[(0,f.jsx)(`button`,{type:`button`,onPointerDown:e=>{e.stopPropagation(),e.preventDefault(),l(r)},className:`absolute -top-3 -right-3 w-6 h-6 rounded-full bg-destructive text-destructive-foreground flex items-center justify-center shadow-md active:scale-90 transition-transform`,style:{transform:`rotate(${-e.rotation}deg)`},children:(0,f.jsx)(n,{className:`w-3.5 h-3.5`,strokeWidth:3})}),(0,f.jsx)(`div`,{className:`absolute -bottom-3 -right-3 w-6 h-6 rounded-full bg-primary text-primary-foreground flex items-center justify-center shadow-md cursor-alias active:scale-90 transition-transform`,style:{transform:`rotate(${-e.rotation}deg)`,touchAction:`none`},onPointerDown:k,onPointerMove:A,onPointerUp:j,children:(0,f.jsx)(t,{className:`w-3.5 h-3.5`,strokeWidth:2.5})}),(0,f.jsx)(`div`,{className:`absolute -bottom-3 -left-3 w-6 h-6 rounded-full bg-primary text-primary-foreground flex items-center justify-center shadow-md cursor-nwse-resize active:scale-90 transition-transform`,style:{transform:`rotate(${-e.rotation}deg)`,touchAction:`none`},onPointerDown:N,onPointerMove:P,onPointerUp:F,children:(0,f.jsx)(i,{className:`w-3.5 h-3.5`,strokeWidth:2.5})})]})]})}function x({stickers:e,editable:t=!1,onUpdate:n,onRemove:r,containerRef:i}){let[a,o]=(0,s.useState)(null);return(0,s.useEffect)(()=>{if(!t||a===null)return;let e=i?.current;if(!e)return;let n=e=>{e.target.closest(`[data-sticker]`)||o(null)};return e.addEventListener(`pointerdown`,n),()=>e.removeEventListener(`pointerdown`,n)},[t,a,i]),(0,s.useEffect)(()=>{a!==null&&a>=e.length&&o(null)},[e.length,a]),e.length===0?null:(0,f.jsx)(f.Fragment,{children:e.map((e,s)=>t&&n&&r&&i?(0,f.jsx)(`div`,{"data-sticker":!0,children:(0,f.jsx)(b,{sticker:e,index:s,selected:a===s,onSelect:o,onUpdate:n,onRemove:e=>{r(e),o(null)},containerRef:i})},`${e.shortcode}-${s}`):(0,f.jsx)(y,{sticker:e},`${e.shortcode}-${s}`))})}export{x as t};
// @license-end
