@embedpdf/plugin-annotation 2.12.1 → 2.13.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.
package/dist/index.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("@embedpdf/core"),e=require("@embedpdf/models"),n="annotation",o={id:n,name:"Annotation Plugin",version:"1.0.0",provides:["annotation"],requires:["interaction-manager","selection"],optional:["history","scroll"],defaultConfig:{autoCommit:!0,annotationAuthor:"Guest",deactivateToolAfterCreate:!1,selectAfterCreate:!0,autoOpenLinks:!0}},i="ANNOTATION/INIT_STATE",a="ANNOTATION/CLEANUP_STATE",s="ANNOTATION/SET_ANNOTATIONS",r="ANNOTATION/SELECT_ANNOTATION",c="ANNOTATION/DESELECT_ANNOTATION",d="ANNOTATION/ADD_TO_SELECTION",l="ANNOTATION/REMOVE_FROM_SELECTION",u="ANNOTATION/SET_SELECTION",h="ANNOTATION/SET_ACTIVE_TOOL_ID",p="ANNOTATION/CREATE_ANNOTATION",g="ANNOTATION/PATCH_ANNOTATION",f="ANNOTATION/MOVE_ANNOTATION",m="ANNOTATION/DELETE_ANNOTATION",y="ANNOTATION/COMMIT",A="ANNOTATION/PURGE_ANNOTATION",x="ANNOTATION/SET_LOCKED",b="ANNOTATION/SYNC_OBJECT",v="ANNOTATION/ADD_COLOR_PRESET",I="ANNOTATION/SET_TOOL_DEFAULTS",P="ANNOTATION/ADD_TOOL";const S=t=>({type:c,payload:{documentId:t}}),T=(t,e,n)=>({type:d,payload:{documentId:t,pageIndex:e,id:n}}),R=(t,e)=>({type:l,payload:{documentId:t,id:e}}),M=(t,e)=>({type:u,payload:{documentId:t,ids:e}}),k=(t,e,n)=>({type:h,payload:{documentId:t,toolId:e,context:n}}),C=(t,e,n)=>({type:p,payload:{documentId:t,pageIndex:e,annotation:n}}),D=(t,e,n,o)=>({type:g,payload:{documentId:t,pageIndex:e,id:n,patch:o}}),E=(t,e,n,o)=>({type:f,payload:{documentId:t,pageIndex:e,id:n,patch:o}}),L=(t,e,n)=>({type:m,payload:{documentId:t,pageIndex:e,id:n}}),j=(t,e,n)=>({type:A,payload:{documentId:t,pageIndex:e,uid:n}});var w=(t=>(t.None="none",t.All="all",t.Include="include",t.Exclude="exclude",t))(w||{});function z(t,e){return!(t.origin.x+t.size.width<e.origin.x||e.origin.x+e.size.width<t.origin.x||t.origin.y+t.size.height<e.origin.y||e.origin.y+e.size.height<t.origin.y)}function N(t){return t.object.type===e.PdfAnnotationSubtype.INK}function O(t){return t.object.type===e.PdfAnnotationSubtype.CIRCLE}function U(t){return t.object.type===e.PdfAnnotationSubtype.POLYGON}function B(t){return t.object.type===e.PdfAnnotationSubtype.SQUARE}function $(t){return t.object.type===e.PdfAnnotationSubtype.LINE}function G(t){return t.object.type===e.PdfAnnotationSubtype.POLYLINE}function F(t){return t.object.type===e.PdfAnnotationSubtype.HIGHLIGHT}function W(t){return t.object.type===e.PdfAnnotationSubtype.UNDERLINE}function H(t){return t.object.type===e.PdfAnnotationSubtype.STRIKEOUT}function _(t){return t.object.type===e.PdfAnnotationSubtype.SQUIGGLY}function K(t){return F(t)||W(t)||H(t)||_(t)}function Y(t){return t.object.type===e.PdfAnnotationSubtype.FREETEXT}function V(t){return t.object.type===e.PdfAnnotationSubtype.STAMP}function X(t){return t.object.type===e.PdfAnnotationSubtype.TEXT}function q(t){return t.object.type===e.PdfAnnotationSubtype.REDACT}function Q(t){return t.object.type===e.PdfAnnotationSubtype.CARET}function Z(t){return t.object.type===e.PdfAnnotationSubtype.WIDGET}function J(t){return X(t)&&!t.object.inReplyToId||K(t)||N(t)||B(t)||O(t)||U(t)||$(t)||G(t)||Y(t)||V(t)||q(t)||Q(t)}function tt(t){return J(t)||Z(t)}function et(t){return(null==t?void 0:t.categories)??[]}function nt(t,e){switch(e.type){case w.None:return!1;case w.All:return!0;case w.Include:return 0!==t.length&&t.some(t=>e.categories.includes(t));case w.Exclude:return 0===t.length||!t.some(t=>e.categories.includes(t))}}function ot(t){var e;return(null==(e=t.flags)?void 0:e.includes("locked"))??!1}const it=(t,e)=>(t.pages[e]??[]).map(e=>t.byUid[e]),at=t=>t.selectedUids.length>0?t.byUid[t.selectedUids[0]]??null:null,st=t=>t.selectedUids.map(e=>t.byUid[e]).filter(t=>void 0!==t),rt=t=>t.selectedUids,ct=(t,e)=>t.byUid[e]??null;const dt=t=>{const n={};for(const e of Object.values(t.pages))for(const o of e){const e=t.byUid[o];if(e&&X(e)){const t=e.object.inReplyToId;t&&(n[t]||(n[t]=[])).push(e)}}const o={},i=new Set;for(const s of Object.values(t.pages))for(const n of s){const a=t.byUid[n];if(a&&a.object.inReplyToId&&a.object.replyType===e.PdfAnnotationReplyType.Group&&a.object.type!==e.PdfAnnotationSubtype.LINK&&J(a)){const t=a.object.inReplyToId;(o[t]||(o[t]=[])).push(a),i.add(a.object.id)}}const a={};for(const[e,s]of Object.entries(t.pages)){const r=Number(e),c=[];for(const e of s){const a=t.byUid[e];if(!a||!J(a))continue;if(i.has(a.object.id))continue;const s=o[a.object.id];c.push({page:r,annotation:a,replies:n[a.object.id]??[],...s&&s.length>0?{groupMembers:s}:{}})}c.length>0&&(a[r]=c)}return a},lt=(t,e)=>{const n=[];for(const o of Object.values(t.pages))for(const i of o){const o=t.byUid[i];o&&"inReplyToId"in o.object&&o.object.inReplyToId===e&&n.push({id:o.object.id,pageIndex:o.object.pageIndex})}return n},ut=(t,e,n)=>{const o=[];for(const i of Object.values(t.pages))for(const a of i){const i=t.byUid[a];i&&"inReplyToId"in i.object&&i.object.inReplyToId===e&&n.includes(i.object.type)&&o.push(i)}return o},ht=(t,n)=>ut(t,n,[e.PdfAnnotationSubtype.LINK]),pt=(t,n)=>{const o=t.byUid[n];if(o)return o.object.inReplyToId&&o.object.replyType===e.PdfAnnotationReplyType.Group?o.object.inReplyToId:n},gt=(t,n)=>{const o=pt(t,n);if(!o)return[];const i=[],a=t.byUid[o];a&&a.object.type!==e.PdfAnnotationSubtype.LINK&&i.push(a);for(const s of Object.values(t.pages))for(const n of s){const a=t.byUid[n];a&&a.object.inReplyToId===o&&a.object.replyType===e.PdfAnnotationReplyType.Group&&a.object.type!==e.PdfAnnotationSubtype.LINK&&i.push(a)}return i},ft=(t,n)=>{const o=t.byUid[n];if(!o)return!1;if(o.object.type===e.PdfAnnotationSubtype.LINK)return!1;if(o.object.inReplyToId&&o.object.replyType===e.PdfAnnotationReplyType.Group)return!0;for(const i of Object.values(t.pages))for(const o of i){const i=t.byUid[o];if(i&&i.object.inReplyToId===n&&i.object.replyType===e.PdfAnnotationReplyType.Group&&i.object.type!==e.PdfAnnotationSubtype.LINK)return!0}return!1},mt=t=>{const e=st(t);if(0===e.length)return"disabled";const n=e[0].object.id;if(ft(t,n)){const o=gt(t,n),i=new Set(o.map(t=>t.object.id));if(e.every(t=>i.has(t.object.id)))return"ungroup"}return e.length>=2?"group":"disabled"};function yt(t){let e=t;return[()=>e,t=>{e=t}]}const At={annotationType:e.PdfAnnotationSubtype.INK,create(n){const{onCommit:o,onPreview:i,getTool:a,pageSize:s}=n,[r,c]=yt([]),[d,l]=yt(!1),u={current:null},h=e=>({x:t.clamp(e.x,0,s.width),y:t.clamp(e.y,0,s.height)}),p=()=>{const t=a();return t?{...t.defaults,strokeWidth:t.defaults.strokeWidth??1,strokeColor:t.defaults.strokeColor??t.defaults.color??"#000000",opacity:t.defaults.opacity??1,flags:t.defaults.flags??["print"]}:null},g=()=>{const t=r();if(0===t.length||0===t[0].points.length)return null;const n=p();if(!n)return null;const o=t.flatMap(t=>t.points),i=e.expandRect(e.rectFromPoints(o),n.strokeWidth/2);return{type:e.PdfAnnotationSubtype.INK,bounds:i,data:{...n,rect:i,inkList:t,blendMode:n.blendMode}}};return{onPointerDown:(t,e)=>{var n;const o=h(t);l(!0),u.current&&clearTimeout(u.current);const a=[...r(),{points:[o]}];c(a),i(g()),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{if(!d())return;const e=r();if(0===e.length)return;const n=h(t);e[e.length-1].points.push(n),c(e),i(g())},onPointerUp:(t,s)=>{var d;l(!1),null==(d=s.releasePointerCapture)||d.call(s);const h=a(),f=null==h?void 0:h.behavior;if(null==f?void 0:f.smartLineRecognition){const t=f.smartLineThreshold??.15,e=r(),n=e[e.length-1];if(n&&n.points.length>1&&function(t,e){if(t.length<3)return!0;const n=t[0],o=t[t.length-1],i=Math.hypot(o.x-n.x,o.y-n.y);return!(i<5)&&t.reduce((t,e)=>{const a=Math.abs((o.x-n.x)*(n.y-e.y)-(n.x-e.x)*(o.y-n.y))/i;return Math.max(t,a)},0)/i<e}(n.points,t)){const t=n.points[0],o=n.points[n.points.length-1],a=o.x-t.x,s=o.y-t.y,r=Math.atan2(Math.abs(s),Math.abs(a))*(180/Math.PI),d=f.snapAngleDeg??15;if(r<=d){const e=n.points.reduce((t,e)=>t+e.y,0)/n.points.length;n.points=[{x:t.x,y:e},{x:o.x,y:e}]}else if(r>=90-d){const e=n.points.reduce((t,e)=>t+e.x,0)/n.points.length;n.points=[{x:e,y:t.y},{x:e,y:o.y}]}c([...e]),i(g())}}const m=(null==f?void 0:f.commitDelay)??800;u.current&&clearTimeout(u.current),u.current=setTimeout(()=>{const t=r();if(t.length>0&&t[0].points.length>1){const i=p();if(!i)return;const a=t.flatMap(t=>t.points),s=e.expandRect(e.rectFromPoints(a),i.strokeWidth/2);o({...i,inkList:t,rect:s,type:e.PdfAnnotationSubtype.INK,pageIndex:n.pageIndex,id:e.uuidV4(),created:new Date})}c([]),i(null)},m)},onPointerCancel:(t,e)=>{var n;c([]),l(!1),i(null),u.current&&clearTimeout(u.current),null==(n=e.releasePointerCapture)||n.call(e)}}}};function xt(t){const e=t=>{const e=9*t,n=Math.PI/6;return{x:-e*Math.cos(n),y:e*Math.sin(n)}};return{getSvgPath:n=>{const{x:o,y:i}=e(n);return t?`M 0 0 L ${o} ${i} L ${o} ${-i} Z`:`M ${o} ${i} L 0 0 L ${o} ${-i}`},getLocalPoints:t=>{const{x:n,y:o}=e(t);return[{x:0,y:0},{x:n,y:o},{x:n,y:-o}]},getRotation:t=>t,filled:t}}function bt(t,e){const n=e=>e*t/2;return{getSvgPath:t=>{const e=n(t);return`M ${-e} 0 L ${e} 0`},getLocalPoints:t=>{const e=n(t);return[{x:-e,y:0},{x:e,y:0}]},getRotation:e,filled:!1}}const vt=xt(!1),It=xt(!0),Pt={[e.PdfAnnotationLineEnding.OpenArrow]:vt,[e.PdfAnnotationLineEnding.ClosedArrow]:It,[e.PdfAnnotationLineEnding.ROpenArrow]:{...vt,getRotation:t=>t+Math.PI},[e.PdfAnnotationLineEnding.RClosedArrow]:{...It,getRotation:t=>t+Math.PI},[e.PdfAnnotationLineEnding.Circle]:{getSvgPath:t=>{const e=5*t/2;return`M ${e} 0 A ${e} ${e} 0 1 1 ${-e} 0 A ${e} ${e} 0 1 1 ${e} 0`},getLocalPoints:t=>{const e=5*t/2;return[{x:-e,y:-e},{x:e,y:e}]},getRotation:()=>0,filled:!0},[e.PdfAnnotationLineEnding.Square]:{getSvgPath:t=>{const e=6*t/2;return`M ${-e} ${-e} L ${e} ${-e} L ${e} ${e} L ${-e} ${e} Z`},getLocalPoints:t=>{const e=6*t/2;return[{x:-e,y:-e},{x:e,y:-e},{x:e,y:e},{x:-e,y:e}]},getRotation:t=>t,filled:!0},[e.PdfAnnotationLineEnding.Diamond]:{getSvgPath:t=>{const e=6*t/2;return`M 0 ${-e} L ${e} 0 L 0 ${e} L ${-e} 0 Z`},getLocalPoints:t=>{const e=6*t/2;return[{x:0,y:-e},{x:e,y:0},{x:0,y:e},{x:-e,y:0}]},getRotation:t=>t,filled:!0},[e.PdfAnnotationLineEnding.Butt]:bt(6,t=>t+Math.PI/2),[e.PdfAnnotationLineEnding.Slash]:bt(18,t=>t+Math.PI/1.5)};function St(t,n,o){if(!t||0===t.length)return{origin:{x:0,y:0},size:{width:0,height:0}};const i=[...t],a=(t,e)=>Math.atan2(e.y-t.y,e.x-t.x),s=(t,o,a)=>{if(!t)return;const s=Pt[t];if(!s)return;const r=s.getLocalPoints(n),c=s.getRotation(a),d=r.map(t=>e.rotateAndTranslatePoint(t,c,o));i.push(...d)};if(t.length>=2){const e=a(t[1],t[0]);s(null==o?void 0:o.start,t[0],e);const n=t.length-1,i=a(t[n-1],t[n]);s(null==o?void 0:o.end,t[n],i)}if(i.length<=1){const e=t[0]||{x:0,y:0},o=n;return{origin:{x:e.x-o,y:e.y-o},size:{width:2*o,height:2*o}}}const r=e.rectFromPoints(i),c=n/2+1.2*n;return e.expandRect(r,c)}function Tt(t,n){if(!t.unrotatedRect)return{rect:n};const o=e.getRectCenter(n);return{rect:e.calculateRotatedRectAABBAroundPoint(n,t.rotation??0,o),unrotatedRect:n}}function Rt(t){return t.unrotatedRect?e.inferRotationCenterFromRects(t.unrotatedRect,t.rect,t.rotation??0):e.getRectCenter(t.rect)}function Mt(t,n,o){const i=Rt(t),a=t.unrotatedRect??t.rect,s=n.origin.x-a.origin.x,r=n.origin.y-a.origin.y,c={x:i.x+s,y:i.y+r};return{rect:e.calculateRotatedRectAABBAroundPoint(n,o,c),unrotatedRect:n}}function kt(t,n,o){if(!t.unrotatedRect)return n;const i=t.rotation??0;if(Math.abs(i%360)<1e-8)return n;const a=Rt(t),s=e.getRectCenter(o),r=i*Math.PI/180,c=Math.cos(r),d=Math.sin(r),l=a.x-s.x,u=a.y-s.y,h=(1-c)*l+d*u,p=-d*l+(1-c)*u;return Math.abs(h)<1e-8&&Math.abs(p)<1e-8?n:n.map(t=>({x:t.x+h,y:t.y+p}))}class Ct{constructor(){this.patches=new Map}register(t,e){this.patches.set(t,e)}transform(t,e){const n=this.patches.get(t.type);return n?n(t,e):e.changes}}const Dt=new Ct;function Et(t,e){var n;if(void 0===(null==(n=e.metadata)?void 0:n.rotationAngle))return null;const o=e.metadata.rotationAngle,i=e.changes.unrotatedRect??t.unrotatedRect??t.rect;return{...Mt(t,{origin:{...i.origin},size:{...i.size}},o),rotation:o}}function Lt(t,e){return{rotation:e,...Mt(t,t.unrotatedRect??t.rect,e)}}function jt(t,e){const n=e.origin.x-t.rect.origin.x,o=e.origin.y-t.rect.origin.y,i={rect:e};return t.unrotatedRect&&(i.unrotatedRect={origin:{x:t.unrotatedRect.origin.x+n,y:t.unrotatedRect.origin.y+o},size:{...t.unrotatedRect.size}}),{dx:n,dy:o,rects:i}}function wt(t,n,o){const i=t.unrotatedRect??t.rect;let a=n.size.width/i.size.width,s=n.size.height/i.size.height;if((n.size.width<10||n.size.height<10)&&(a=Math.max(a,10/i.size.width),s=Math.max(s,10/i.size.height),n={origin:n.origin,size:{width:i.size.width*a,height:i.size.height*s}}),null==o?void 0:o.maintainAspectRatio){const t=Math.min(a,s);a=t,s=t,n={origin:n.origin,size:{width:i.size.width*t,height:i.size.height*t}}}return{scaleX:a,scaleY:s,oldRect:i,resolvedRect:n,rects:t.unrotatedRect?{unrotatedRect:n,rect:e.calculateRotatedRectAABB(n,t.rotation??0)}:{rect:n}}}function zt(t,e){const n=t.unrotatedRect??t.rect,o=e.unrotatedRect??n;return{dx:o.origin.x-n.origin.x,dy:o.origin.y-n.origin.y}}function Nt(t,e,n){if(0===e||void 0!==t.rotation)return t;const o=(4-e)%4*90;let i=t;if(n&&(1===e||3===e)){const e=t.rect,n=e.origin.x+e.size.width/2,o=e.origin.y+e.size.height/2;i={...t,rect:{origin:{x:n-e.size.height/2,y:o-e.size.width/2},size:{width:e.size.height,height:e.size.width}}}}const{rotation:a,rect:s,unrotatedRect:r}=Lt(i,o);return{...i,rotation:a,rect:s,unrotatedRect:r}}function Ot(e,n){const o=t.clamp(e.rect.origin.x,0,n.width-e.rect.size.width),i=t.clamp(e.rect.origin.y,0,n.height-e.rect.size.height),a=o-e.rect.origin.x,s=i-e.rect.origin.y;return 0===a&&0===s?e:{...e,rect:{origin:{x:o,y:i},size:e.rect.size},...e.unrotatedRect?{unrotatedRect:{origin:{x:e.unrotatedRect.origin.x+a,y:e.unrotatedRect.origin.y+s},size:e.unrotatedRect.size}}:{}}}const Ut=Object.freeze(Object.defineProperty({__proto__:null,LINE_ENDING_HANDLERS:Pt,PatchRegistry:Ct,applyInsertUpright:Nt,calculateAABBFromVertices:function(t,n=0){if(0===t.length)return{origin:{x:0,y:0},size:{width:0,height:0}};const o=e.rectFromPoints(t);return n>0?e.expandRect(o,n):o},calculateRotatedRectAABB:e.calculateRotatedRectAABB,calculateRotatedRectAABBAroundPoint:e.calculateRotatedRectAABBAroundPoint,clampAnnotationToPage:Ot,compensateRotatedVertexEdit:kt,createEnding:function(t,e,n,o,i){if(!t)return null;const a=Pt[t];if(!a)return null;const s=a.getRotation(n);return{d:a.getSvgPath(e),transform:`translate(${o} ${i}) rotate(${r=s,180*r/Math.PI})`,filled:a.filled};var r},getRectCenter:e.getRectCenter,lineRectWithEndings:St,patchRegistry:Dt,resolveAnnotationRotationCenter:Rt,resolveRotateRects:Mt,resolveVertexEditRects:Tt,rotatePointAroundCenter:e.rotatePointAround,rotateVertices:e.rotateVertices},Symbol.toStringTag,{value:"Module"})),Bt={annotationType:e.PdfAnnotationSubtype.TEXT,create(t){const{onCommit:n,getTool:o,pageSize:i}=t;return{onPointerDown:a=>{const s=o();if(!s)return;const r={origin:{x:a.x-12,y:a.y-12},size:{width:24,height:24}};let c={...s.defaults,rect:r,type:e.PdfAnnotationSubtype.TEXT,name:s.defaults.name??e.PdfAnnotationName.Comment,contents:s.defaults.contents??"",flags:s.defaults.flags??["print","noRotate","noZoom"],pageIndex:t.pageIndex,id:e.uuidV4(),created:new Date};c=Ot(c,i),n(c)}}}};function $t({threshold:t=5,getTool:e,onClickDetected:n}){const[o,i]=yt(null),[a,s]=yt(!1);return{onStart:t=>{i(t),s(!1)},onMove:e=>{const n=o();if(!n||a())return;Math.sqrt(Math.pow(e.x-n.x,2)+Math.pow(e.y-n.y,2))>t&&s(!0)},onEnd:t=>{var r;if(o()&&!a()){const o=e();o&&"clickBehavior"in o&&(null==(r=o.clickBehavior)?void 0:r.enabled)&&n(t,o)}i(null),s(!1)},hasMoved:a,reset:()=>{i(null),s(!1)}}}const Gt={annotationType:e.PdfAnnotationSubtype.FREETEXT,create(n){const{onCommit:o,onPreview:i,getTool:a,pageSize:s,pageIndex:r,pageRotation:c}=n,[d,l]=yt(null),u=e=>({x:t.clamp(e.x,0,s.width),y:t.clamp(e.y,0,s.height)}),h=()=>{const t=a();return t?{...t.defaults,fontColor:t.defaults.fontColor??"#000000",opacity:t.defaults.opacity??1,fontSize:t.defaults.fontSize??12,fontFamily:t.defaults.fontFamily??e.PdfStandardFont.Helvetica,color:t.defaults.color??t.defaults.backgroundColor??"transparent",textAlign:t.defaults.textAlign??e.PdfTextAlignment.Left,verticalAlign:t.defaults.verticalAlign??e.PdfVerticalAlignment.Top,contents:t.defaults.contents??"Insert text here",flags:t.defaults.flags??["print"]}:null},p=$t({threshold:5,getTool:a,onClickDetected:(t,n)=>{var i;const a=h();if(!a)return;const d=n.clickBehavior;if(!(null==d?void 0:d.enabled))return;const{width:l,height:u}=d.defaultSize,p={origin:{x:t.x-l/2,y:t.y-u/2},size:{width:l,height:u}},g=d.defaultContent??a.contents;let f={...a,contents:g,type:e.PdfAnnotationSubtype.FREETEXT,rect:p,pageIndex:r,id:e.uuidV4(),created:new Date};(null==(i=n.behavior)?void 0:i.insertUpright)&&(f=Nt(f,c,!1)),f=Ot(f,s),o(f)}}),g=t=>{const n=d();if(!n)return null;const o=h();if(!o)return null;const i={origin:{x:Math.min(n.x,t.x),y:Math.min(n.y,t.y)},size:{width:Math.abs(n.x-t.x),height:Math.abs(n.y-t.y)}};return{type:e.PdfAnnotationSubtype.FREETEXT,bounds:i,data:{...o,rect:i}}};return{onPointerDown:(t,e)=>{var n;const o=u(t);l(o),p.onStart(o),i(g(o)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=u(t);p.onMove(e),d()&&p.hasMoved()&&i(g(e))},onPointerUp:(t,s)=>{var r,g;const f=d();if(!f)return;const m=h();if(!m)return;const y=u(t);if(p.hasMoved()){const t={origin:{x:Math.min(f.x,y.x),y:Math.min(f.y,y.y)},size:{width:Math.abs(f.x-y.x),height:Math.abs(f.y-y.y)}},i=a();let s={...m,type:e.PdfAnnotationSubtype.FREETEXT,rect:t,pageIndex:n.pageIndex,id:e.uuidV4(),created:new Date};(null==(r=null==i?void 0:i.behavior)?void 0:r.insertUpright)&&(s=Nt(s,c,!0)),o(s)}else p.onEnd(y);l(null),i(null),p.reset(),null==(g=s.releasePointerCapture)||g.call(s)},onPointerLeave:(t,e)=>{var n;l(null),i(null),p.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;l(null),i(null),p.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},Ft={annotationType:e.PdfAnnotationSubtype.LINE,create(n){const{pageIndex:o,onCommit:i,onPreview:a,getTool:s,pageSize:r}=n,[c,d]=yt(null),l=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.height)}),u=()=>{const t=s();return t?{...t.defaults,strokeWidth:t.defaults.strokeWidth??1,lineEndings:t.defaults.lineEndings??{start:e.PdfAnnotationLineEnding.None,end:e.PdfAnnotationLineEnding.None},color:t.defaults.color??"#000000",opacity:t.defaults.opacity??1,strokeStyle:t.defaults.strokeStyle??e.PdfAnnotationBorderStyle.SOLID,strokeDashArray:t.defaults.strokeDashArray??[],strokeColor:t.defaults.strokeColor??"#000000",flags:t.defaults.flags??["print"]}:null},h=$t({threshold:5,getTool:s,onClickDetected:(t,n)=>{const a=u();if(!a)return;const s=n.clickBehavior;if(!(null==s?void 0:s.enabled))return;const r=s.defaultAngle??0,c=s.defaultLength/2,d=t.x-c*Math.cos(r),h=t.y-c*Math.sin(r),p=t.x+c*Math.cos(r),g=t.y+c*Math.sin(r),f=l({x:d,y:h}),m=l({x:p,y:g}),y=St([f,m],a.strokeWidth,a.lineEndings);i({...a,rect:y,linePoints:{start:f,end:m},pageIndex:o,id:e.uuidV4(),created:new Date,type:e.PdfAnnotationSubtype.LINE})}}),p=t=>{const n=c();if(!n)return null;const o=u();if(!o)return null;const i=St([n,t],o.strokeWidth,o.lineEndings);return{type:e.PdfAnnotationSubtype.LINE,bounds:i,data:{...o,rect:i,linePoints:{start:n,end:t}}}};return{onPointerDown:(t,e)=>{var n;const o=l(t);d(o),h.onStart(o),a(p(o)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=l(t);h.onMove(e),c()&&h.hasMoved()&&a(p(e))},onPointerUp:(t,n)=>{var s;const r=c();if(!r)return;const p=l(t);if(h.hasMoved()){const t=u();if(!t)return;if(Math.abs(p.x-r.x)>2||Math.abs(p.y-r.y)>2){const n=St([r,p],t.strokeWidth,t.lineEndings);i({...t,rect:n,linePoints:{start:r,end:p},pageIndex:o,id:e.uuidV4(),flags:["print"],created:new Date,type:e.PdfAnnotationSubtype.LINE})}}else h.onEnd(p);d(null),a(null),h.reset(),null==(s=n.releasePointerCapture)||s.call(n)},onPointerLeave:(t,e)=>{var n;d(null),a(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;d(null),a(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},Wt={annotationType:e.PdfAnnotationSubtype.POLYLINE,create(n){const{onCommit:o,onPreview:i,getTool:a,pageSize:s}=n,[r,c]=yt([]),[d,l]=yt(null),u=e=>({x:t.clamp(e.x,0,s.width),y:t.clamp(e.y,0,s.height)}),h=()=>{const t=a();return t?{...t.defaults,strokeWidth:t.defaults.strokeWidth??1,lineEndings:t.defaults.lineEndings??{start:e.PdfAnnotationLineEnding.None,end:e.PdfAnnotationLineEnding.None},color:t.defaults.color??"#000000",opacity:t.defaults.opacity??1,strokeColor:t.defaults.strokeColor??"#000000",strokeStyle:t.defaults.strokeStyle??e.PdfAnnotationBorderStyle.SOLID,strokeDashArray:t.defaults.strokeDashArray??[],flags:t.defaults.flags??["print"]}:null},p=()=>{const t=r(),n=d();if(0===t.length||!n)return null;const o=h();if(!o)return null;const i=[...t,n],a=St(i,o.strokeWidth,o.lineEndings);return{type:e.PdfAnnotationSubtype.POLYLINE,bounds:a,data:{...o,rect:a,vertices:i,currentVertex:n}}};return{onClick:(t,e)=>{if(e.metaKey||e.ctrlKey)return;const n=u(t),o=r(),a=o[o.length-1];a&&Math.abs(a.x-n.x)<1&&Math.abs(a.y-n.y)<1||(c([...o,n]),l(n),i(p()))},onDoubleClick:()=>{(()=>{const t=r();if(t.length<2)return;const a=h();if(!a)return;const s=St(t,a.strokeWidth,a.lineEndings),d={...a,vertices:t,rect:s,type:e.PdfAnnotationSubtype.POLYLINE,pageIndex:n.pageIndex,id:e.uuidV4(),created:new Date};o(d),c([]),l(null),i(null)})()},onPointerMove:t=>{if(r().length>0){const e=u(t);l(e),i(p())}},onPointerCancel:()=>{c([]),l(null),i(null)}}}};function Ht(t,e,n,o){const i=o*Math.PI/180,a=Math.abs(Math.cos(i)),s=Math.abs(Math.sin(i)),r=a*a-s*s,c=t.size.width,d=t.size.height;let l,u;if(Math.abs(r)>1e-6)l=(a*c-s*d)/r,u=(a*d-s*c)/r,l=Math.max(l,1),u=Math.max(u,1);else{const t=e.size.width*e.size.height,o=c*d,i=t>0?Math.sqrt(o/t):1;l=n.size.width*i,u=n.size.height*i}return{origin:{x:t.origin.x+c/2-l/2,y:t.origin.y+d/2-u/2},size:{width:l,height:u}}}const _t=Math.PI,Kt=Math.PI/2,Yt=34*Math.PI/180,Vt=30*Math.PI/180,Xt=12*Math.PI/180;class qt{constructor(){this.parts=[],this.bbox={minX:1/0,minY:1/0,maxX:-1/0,maxY:-1/0},this.started=!1}moveTo(t,e){const n=-e;this.updateBBox(t,n),this.parts.push(`M ${Qt(t)} ${Qt(n)}`),this.started=!0}curveTo(t,e,n,o,i,a){const s=-e,r=-o,c=-a;this.updateBBox(t,s),this.updateBBox(n,r),this.updateBBox(i,c),this.parts.push(`C ${Qt(t)} ${Qt(s)}, ${Qt(n)} ${Qt(r)}, ${Qt(i)} ${Qt(c)}`)}close(){this.started&&this.parts.push("Z")}build(t){const e=t>0?t/2:0;return{path:this.parts.join(" "),bbox:{minX:this.bbox.minX-e,minY:this.bbox.minY-e,maxX:this.bbox.maxX+e,maxY:this.bbox.maxY+e}}}updateBBox(t,e){t<this.bbox.minX&&(this.bbox.minX=t),e<this.bbox.minY&&(this.bbox.minY=e),t>this.bbox.maxX&&(this.bbox.maxX=t),e>this.bbox.maxY&&(this.bbox.maxY=e)}}function Qt(t){return Number(t.toFixed(4)).toString()}function Zt(t,e){const n=e.x-t.x,o=e.y-t.y;return Math.sqrt(n*n+o*o)}function Jt(t,e){return 0===e?0:t/e}function te(t,e){return 0===e?0:t/e}function ee(t,e,n,o,i,a,s,r){const c=Math.cos(t),d=Math.sin(t),l=Math.cos(e),u=Math.sin(e),h=Math.sin((e-t)/2);if(0===h)return void(r&&s.moveTo(n+i*c,o+a*d));const p=4/3*(1-Math.cos((e-t)/2))/h,g=n+i*(c-p*d),f=o+a*(d+p*c),m=n+i*(l+p*u),y=o+a*(u-p*l),A=n+i*l,x=o+a*u;r&&s.moveTo(n+i*c,o+a*d),s.curveTo(g,f,m,y,A,x)}function ne(t,e,n,o,i,a){const s=Math.cos(t),r=Math.sin(t),c=Math.cos(e),d=Math.sin(e),l=Math.sin((e-t)/2);if(0===l)return[];const u=4/3*(1-Math.cos((e-t)/2))/l;return[{x:n+i*(s-u*r),y:o+a*(r+u*s)},{x:n+i*(c+u*d),y:o+a*(d-u*c)},{x:n+i*c,y:o+a*d}]}function oe(t,e,n,o,i,a,s,r){const c=Kt;let d=e-t;for(;d<0;)d+=2*Math.PI;const l=d;let u=0;for(r&&s.moveTo(i+n*Math.cos(t),a+o*Math.sin(t));d>c;)ee(t+u,t+u+c,i,a,n,o,s,!1),u+=c,d-=c;d>0&&ee(t+u,t+l,i,a,n,o,s,!1)}function ie(t,e,n,o,i,a,s,r,c){let d=t+_t+s;const l=t+_t+s-22*Math.PI/180;ee(d,l,o,i,n,n,r,c),d=l;oe(d,e-a,n,n,o,i,r,!1)}function ae(t,e,n,o,i,a){const s=t+_t;ee(s+n,s+n-Vt,o,i,e,e,a,!1),ee(s+n-Vt,s+Kt,o,i,e,e,a,!1),ee(s+Kt,s+_t-Yt,o,i,e,e,a,!1)}function se(t,e){const n=[],o=t+_t;return n.push(...ne(o+Yt,o+Xt,0,0,e,e)),n.push(...ne(o+Xt,o+Kt,0,0,e,e)),n.push(...ne(o+Kt,o+_t-Yt,0,0,e,e)),n}function re(t,e,n,o){for(let i=0;i+2<t.length;i+=3){const a=t[i],s=t[i+1],r=t[i+2];o.curveTo(a.x+e,a.y+n,s.x+e,s.y+n,r.x+e,r.y+n)}}function ce(t,e,n){if(0===n)return{n:-1,adjustedRadius:t};const o=n-2*e*t;if(o<=0)return{n:0,adjustedRadius:t};const i=2*e*t,a=Math.max(1,Math.ceil(o/i));return{n:a,adjustedRadius:o/(2*a*e)}}function de(t,e,n,o,i){let a=function(t){if(t.length<=2)return t;const e=[t[0]];for(let n=1;n<t.length;n++){const o=e[e.length-1],i=t[n];(Math.abs(i.x-o.x)>=.5||Math.abs(i.y-o.y)>=.5)&&e.push(i)}return e}(t);var s;(function(t){let e=0;const n=t.length;for(let o=0;o<n;o++){const i=(o+1)%n;e+=t[o].x*t[i].y-t[o].y*t[i].x}return e})(s=a)<0&&s.reverse();const r=a.length;if(r<2)return;if(n<=0){i.moveTo(a[0].x,a[0].y);for(let t=1;t<r;t++)i.curveTo(a[t].x,a[t].y,a[t].x,a[t].y,a[t].x,a[t].y);return}let c=e?le(n,o):ue(n,o);c<.5&&(c=.5);const d=Math.cos(Yt),l=[];for(let p=0;p+1<r;p++){const t=Zt(a[p],a[p+1]);t<=0||t>=2*d*c?l.push(Yt):l.push(Math.acos(Math.min(1,t/(2*c))))}let u=0,h=!1;for(let p=0;p+1<r;p++){const t=a[p],e=a[p+1],n=Zt(t,e);if(0===n)continue;const o=ce(c,d,n);if(o.n<0){h||(i.moveTo(t.x,t.y),h=!0);continue}const s=Math.max(.5,o.adjustedRadius),g=2*d*s,f=d*c+d*s;let m=Math.atan2(e.y-t.y,e.x-t.x);if(0===p){const e=a[r-2];u=Math.atan2(t.y-e.y,t.x-e.x)}const y=Jt(e.x-t.x,n),A=te(e.y-t.y,n);let x=t.x,b=t.y;const v=l[p],I=l[0===p?r-2:p-1]??Yt;if(ie(u,m,c,t.x,t.y,v,I,i,!h),h=!0,0===o.n)x+=n*y,b+=n*A;else{x+=f*y,b+=f*A;let t=o.n;o.n>=1&&(ae(m,s,Yt,x,b,i),x+=g*y,b+=g*A,t=o.n-1);const e=se(m,s);for(let n=0;n<t;n++)re(e,x,b,i),x+=g*y,b+=g*A}u=m}}function le(t,e){return 4.75*t+.5*e}function ue(t,e){return 4*t+.5*e}function he(t,e,n,o,i,a,s){if(i<=0){const i=Math.abs(n-t)/2,a=Math.abs(o-e)/2,r=(t+n)/2,c=(e+o)/2;return void oe(0,2*Math.PI,i,a,r,c,s,!0)}const r=n-t,c=o-e;let d=le(i,a);const l=.5*d;if(r<l&&c<l){const i=Math.abs(n-t)/2,a=Math.abs(o-e)/2,r=(t+n)/2,c=(e+o)/2;return void oe(0,2*Math.PI,i,a,r,c,s,!0)}if(r<5&&c>20||r>20&&c<5)return void de([{x:t,y:e},{x:n,y:e},{x:n,y:o},{x:t,y:o},{x:t,y:e}],!0,i,a,s);const u=Math.sin(Xt)*d-1.5;let h=t,p=n,g=e,f=o;if(r>2*u)h+=u,p-=u;else{const e=(t+n)/2;h=e-.1,p=e+.1}if(c>2*u)g+=u,f-=u;else{const t=(o+e)/2;f=t+.1,g=t-.1}const m=function(t,e,n,o){const i=(t+n)/2,a=(e+o)/2,s=(n-t)/2,r=(o-e)/2;if(s<=0||r<=0)return[];const c=Math.max(32,Math.ceil(2*Math.max(s,r))),d=[];for(let l=0;l<=c;l++){const t=2*Math.PI*l/c;d.push({x:i+s*Math.cos(t),y:a+r*Math.sin(t)})}return d}(h,g,p,f),y=m.length;if(y<2)return;let A=0;for(let k=1;k<y;k++)A+=Zt(m[k-1],m[k]);const x=Math.cos(Yt);let b=2*x*d,v=Math.ceil(A/b);if(v<2){const i=Math.abs(n-t)/2,a=Math.abs(o-e)/2,r=(t+n)/2,c=(e+o)/2;return void oe(0,2*Math.PI,i,a,r,c,s,!0)}if(b=A/v,d=b/(2*x),d<.5)d=.5,b=2*x*d;else if(d<3){const i=Math.abs(n-t)/2,a=Math.abs(o-e)/2,r=(t+n)/2,c=(e+o)/2;return void oe(0,2*Math.PI,i,a,r,c,s,!0)}const I=[];let P=0;const S=.1*a;for(let k=0;k+1<y;k++){const t=m[k],e=m[k+1],n=e.x-t.x,o=e.y-t.y,i=Zt(t,e);if(0===i)continue;let a=i+P;if(a>=b-S||k===y-2){const e=Jt(n,i),s=te(o,i);let r=b-P;for(;a>=b-S;)I.push({x:t.x+r*e,y:t.y+r*s}),a-=b,r+=b;P=Math.max(0,a)}else P+=i}const T=I.length;let R=0,M=0;for(let k=0;k<T;k++){const t=(k+1)%T,e=I[k],n=I[t];if(0===k){const t=I[T-1];R=Math.atan2(e.y-t.y,e.x-t.x),M=pe(t,e,d,b)}const o=Math.atan2(n.y-e.y,n.x-e.x),i=pe(e,n,d,b);ie(R,o,d,e.x,e.y,i,M,s,0===k),R=o,M=i}}function pe(t,e,n,o){const i=Zt(t,e);if(0===i)return Yt;const a=(o/2+(i-o)/2)/n;return a<-1||a>1?0:Math.acos(a)}function ge(t,e,n){return(n?le(t,e):ue(t,e))+e/2}const fe={annotationType:e.PdfAnnotationSubtype.POLYGON,create(n){const{onCommit:o,onPreview:i,getTool:a,scale:s,pageSize:r}=n,[c,d]=yt([]),[l,u]=yt(null),h=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.height)}),p=()=>{const t=a();return t?{...t.defaults,color:t.defaults.color??"#000000",opacity:t.defaults.opacity??1,strokeWidth:t.defaults.strokeWidth??1,strokeColor:t.defaults.strokeColor??"#000000",strokeStyle:t.defaults.strokeStyle??e.PdfAnnotationBorderStyle.SOLID,strokeDashArray:t.defaults.strokeDashArray??[],flags:t.defaults.flags??["print"]}:null},g=()=>{const t=c();if(t.length<3)return;const a=p();if(!a)return;const s=a.cloudyBorderIntensity??0,r=s>0?ge(s,a.strokeWidth,!1):a.strokeWidth/2,l=e.expandRect(e.rectFromPoints(t),r),h={...a,vertices:t,rect:l,type:e.PdfAnnotationSubtype.POLYGON,pageIndex:n.pageIndex,id:e.uuidV4(),created:new Date,...s>0&&{rectangleDifferences:{left:r,top:r,right:r,bottom:r}}};o(h),d([]),u(null),i(null)},f=()=>{const t=c(),n=l();if(0===t.length||!n)return null;const o=p();if(!o)return null;const i=o.cloudyBorderIntensity??0,a=i>0?ge(i,o.strokeWidth,!1):o.strokeWidth/2,s=[...t,n],r=e.expandRect(e.rectFromPoints(s),a);return{type:e.PdfAnnotationSubtype.POLYGON,bounds:r,data:{...o,rect:r,vertices:t,currentVertex:n}}};return{onClick:(t,e)=>{if(e.metaKey||e.ctrlKey)return;const n=h(t);if((t=>{const e=c();if(e.length<2)return!1;const n=14/s/2,o=e[0];return t.x>=o.x-n&&t.x<=o.x+n&&t.y>=o.y-n&&t.y<=o.y+n})(n)&&c().length>=3)return void g();const o=c(),a=o[o.length-1];a&&Math.abs(a.x-n.x)<1&&Math.abs(a.y-n.y)<1||(d([...o,n]),u(n),i(f()))},onDoubleClick:t=>{g()},onPointerMove:t=>{if(c().length>0){const e=h(t);u(e),i(f())}},onPointerCancel:t=>{d([]),u(null),i(null)}}}},me={annotationType:e.PdfAnnotationSubtype.SQUARE,create(n){const{pageIndex:o,onCommit:i,onPreview:a,getTool:s,pageSize:r}=n,[c,d]=yt(null),l=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.height)}),u=()=>{const t=s();return t?{...t.defaults,flags:t.defaults.flags??["print"],strokeWidth:t.defaults.strokeWidth??2,strokeColor:t.defaults.strokeColor??"#000000",strokeStyle:t.defaults.strokeStyle??e.PdfAnnotationBorderStyle.SOLID,strokeDashArray:t.defaults.strokeDashArray??[],color:t.defaults.color??"#000000",opacity:t.defaults.opacity??1}:null},h=$t({threshold:5,getTool:s,onClickDetected:(n,a)=>{const s=u();if(!s)return;const c=a.clickBehavior;if(!(null==c?void 0:c.enabled))return;const{width:d,height:l}=c.defaultSize,h=d/2,p=l/2,g=t.clamp(n.x-h,0,r.width-d),f=t.clamp(n.y-p,0,r.height-l),m=s.strokeWidth,y=s.cloudyBorderIntensity??0,A=y>0?ge(y,m,!1):m/2,x={origin:{x:g-A,y:f-A},size:{width:d+2*A,height:l+2*A}},b={...s,type:e.PdfAnnotationSubtype.SQUARE,created:new Date,id:e.uuidV4(),pageIndex:o,rect:x,...y>0&&{rectangleDifferences:{left:A,top:A,right:A,bottom:A}}};i(b)}}),p=t=>{const n=c();if(!n)return null;const o=Math.min(n.x,t.x),i=Math.min(n.y,t.y),a=Math.abs(n.x-t.x),s=Math.abs(n.y-t.y),r=u();if(!r)return null;const d=r.strokeWidth,l=r.cloudyBorderIntensity??0,h=l>0?ge(l,d,!1):d/2,p={origin:{x:o-h,y:i-h},size:{width:a+2*h,height:s+2*h}};return{type:e.PdfAnnotationSubtype.SQUARE,bounds:p,data:{rect:p,...r,...l>0&&{rectangleDifferences:{left:h,top:h,right:h,bottom:h}}}}};return{onPointerDown:(t,e)=>{var n;const o=l(t);d(o),h.onStart(o),a(p(o)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=l(t);h.onMove(e),c()&&h.hasMoved()&&a(p(e))},onPointerUp:(t,n)=>{var s;if(!c())return;if(!u())return;const r=l(t);if(h.hasMoved()){const t=u();if(!t)return;const n=p(r);if(n){const a=t.cloudyBorderIntensity??0,s=a>0?ge(a,t.strokeWidth,!1):void 0,r={...t,type:e.PdfAnnotationSubtype.SQUARE,created:new Date,id:e.uuidV4(),pageIndex:o,rect:n.data.rect,...void 0!==s&&{rectangleDifferences:{left:s,top:s,right:s,bottom:s}}};i(r)}}else h.onEnd(r);d(null),a(null),h.reset(),null==(s=n.releasePointerCapture)||s.call(n)},onPointerLeave:(t,e)=>{var n;d(null),a(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;d(null),a(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},ye=new Map,Ae={annotationType:e.PdfAnnotationSubtype.STAMP,create(t){const{services:n,onCommit:o,onPreview:i,getTool:a,pageSize:s,pageRotation:r}=t;let c=null,d=null;const l=(n,i,c)=>{const d=e.getImageMetadata(i);if(!d||"application/pdf"===d.mimeType)return!1;const l=e.fitSizeWithin(d,s);return((n,i,c,d)=>{var l;const u=a();if(!u)return;const h={origin:{x:n.x-i/2,y:n.y-c/2},size:{width:i,height:c}};let p={...u.defaults,rect:h,type:e.PdfAnnotationSubtype.STAMP,name:u.defaults.name??e.PdfAnnotationName.Image,subject:u.defaults.subject??"Stamp",flags:u.defaults.flags??["print"],pageIndex:t.pageIndex,id:e.uuidV4(),created:new Date};(null==(l=u.behavior)?void 0:l.insertUpright)&&(p=Nt(p,r,!1)),p=Ot(p,s),o(p,d)})(n,(null==c?void 0:c.width)??l.width,(null==c?void 0:c.height)??l.height,{data:i}),!0};return{onHandlerActiveStart:()=>{const t=a(),n=null==t?void 0:t.defaults.imageSrc;if(!n)return;let o=ye.get(n);if(o)o.refs++;else{o={promise:fetch(n).then(t=>t.arrayBuffer()).then(n=>{const o=e.getImageMetadata(n);if(!o||"application/pdf"===o.mimeType)return null;const i=e.fitSizeWithin(o,s),a=t.defaults.imageSize;return{buffer:n,width:(null==a?void 0:a.width)??i.width,height:(null==a?void 0:a.height)??i.height}}).catch(()=>null),refs:1},ye.set(n,o)}o.promise.then(t=>{t&&(c=t.buffer,d={width:t.width,height:t.height})})},onHandlerActiveEnd:()=>{const t=a(),e=null==t?void 0:t.defaults.imageSrc;if(e){const t=ye.get(e);t&&--t.refs<=0&&ye.delete(e)}c=null,d=null,i(null)},onPointerMove:t=>{var n;const o=a();if(!(null==(n=null==o?void 0:o.behavior)?void 0:n.showGhost)||!d||!o.defaults.imageSrc)return;const s={origin:{x:t.x-d.width/2,y:t.y-d.height/2},size:d};i({type:e.PdfAnnotationSubtype.STAMP,bounds:s,data:{rect:s,ghostUrl:o.defaults.imageSrc,pageRotation:r}})},onPointerDown:t=>{const e=a();if(!e)return;const{imageSrc:o,imageSize:s}=e.defaults;o?(i(null),c?l(t,c,s):fetch(o).then(t=>t.arrayBuffer()).then(e=>l(t,e,s))):n.requestFile({accept:"image/png,image/jpeg",onFile:e=>{e.arrayBuffer().then(e=>l(t,e))}})},onPointerLeave:()=>{i(null)}}}},xe={annotationType:e.PdfAnnotationSubtype.CIRCLE,create(n){const{pageIndex:o,onCommit:i,onPreview:a,getTool:s,pageSize:r}=n,[c,d]=yt(null),l=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.height)}),u=()=>{const t=s();return t?{...t.defaults,strokeWidth:t.defaults.strokeWidth??2,strokeColor:t.defaults.strokeColor??"#000000",strokeStyle:t.defaults.strokeStyle??e.PdfAnnotationBorderStyle.SOLID,strokeDashArray:t.defaults.strokeDashArray??[],color:t.defaults.color??"#000000",opacity:t.defaults.opacity??1,flags:t.defaults.flags??["print"]}:null},h=$t({threshold:5,getTool:s,onClickDetected:(n,a)=>{const s=u();if(!s)return;const c=a.clickBehavior;if(!(null==c?void 0:c.enabled))return;const{width:d,height:l}=c.defaultSize,h=d/2,p=l/2,g=t.clamp(n.x-h,0,r.width-d),f=t.clamp(n.y-p,0,r.height-l),m=s.strokeWidth,y=s.cloudyBorderIntensity??0,A=y>0?ge(y,m,!0):m/2,x={origin:{x:g-A,y:f-A},size:{width:d+2*A,height:l+2*A}},b={...s,type:e.PdfAnnotationSubtype.CIRCLE,created:new Date,id:e.uuidV4(),pageIndex:o,rect:x,...y>0&&{rectangleDifferences:{left:A,top:A,right:A,bottom:A}}};i(b)}}),p=t=>{const n=c();if(!n)return null;const o=Math.min(n.x,t.x),i=Math.min(n.y,t.y),a=Math.abs(n.x-t.x),s=Math.abs(n.y-t.y),r=u();if(!r)return null;const d=r.strokeWidth,l=r.cloudyBorderIntensity??0,h=l>0?ge(l,d,!0):d/2,p={origin:{x:o-h,y:i-h},size:{width:a+2*h,height:s+2*h}};return{type:e.PdfAnnotationSubtype.CIRCLE,bounds:p,data:{rect:p,...r,...l>0&&{rectangleDifferences:{left:h,top:h,right:h,bottom:h}}}}};return{onPointerDown:(t,e)=>{var n;const o=l(t);d(o),h.onStart(o),a(p(o)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=l(t);h.onMove(e),c()&&h.hasMoved()&&a(p(e))},onPointerUp:(t,n)=>{var s;if(!c())return;if(!u())return;const r=l(t);if(h.hasMoved()){const t=u();if(!t)return;const n=p(r);if(n){const a=t.cloudyBorderIntensity??0,s=a>0?ge(a,t.strokeWidth,!0):void 0,r={...t,type:e.PdfAnnotationSubtype.CIRCLE,flags:["print"],created:new Date,id:e.uuidV4(),pageIndex:o,rect:n.data.rect,...void 0!==s&&{rectangleDifferences:{left:s,top:s,right:s,bottom:s}}};i(r)}}else h.onEnd(r);d(null),a(null),h.reset(),null==(s=n.releasePointerCapture)||s.call(n)},onPointerLeave:(t,e)=>{var n;d(null),a(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;d(null),a(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},be={annotationType:e.PdfAnnotationSubtype.LINK,create(n){const{pageIndex:o,onCommit:i,onPreview:a,getTool:s,pageSize:r}=n,[c,d]=yt(null),l=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.height)}),u=()=>{const t=s();return t?{...t.defaults,flags:t.defaults.flags??["print"],strokeWidth:t.defaults.strokeWidth??2,strokeColor:t.defaults.strokeColor??"#0000FF",strokeStyle:t.defaults.strokeStyle??e.PdfAnnotationBorderStyle.UNDERLINE,strokeDashArray:t.defaults.strokeDashArray??[]}:null},h=$t({threshold:5,getTool:s,onClickDetected:(n,a)=>{const s=u();if(!s)return;const c=a.clickBehavior;if(!(null==c?void 0:c.enabled))return;const{width:d,height:l}=c.defaultSize,h=d/2,p=l/2,g={origin:{x:t.clamp(n.x-h,0,r.width-d),y:t.clamp(n.y-p,0,r.height-l)},size:{width:d,height:l}},f={...s,type:e.PdfAnnotationSubtype.LINK,target:void 0,created:new Date,id:e.uuidV4(),pageIndex:o,rect:g};i(f)}}),p=t=>{const n=c();if(!n)return null;const o=Math.min(n.x,t.x),i=Math.min(n.y,t.y),a=Math.abs(n.x-t.x),s=Math.abs(n.y-t.y),r=u();if(!r)return null;const d={origin:{x:o,y:i},size:{width:a,height:s}};return{type:e.PdfAnnotationSubtype.LINK,bounds:d,data:{rect:d,strokeColor:r.strokeColor,strokeWidth:r.strokeWidth,strokeStyle:r.strokeStyle,strokeDashArray:r.strokeDashArray}}};return{onPointerDown:(t,e)=>{var n;const o=l(t);d(o),h.onStart(o),a(p(o)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=l(t);h.onMove(e),c()&&h.hasMoved()&&a(p(e))},onPointerUp:(t,n)=>{var s;if(!c())return;const r=u();if(!r)return;const g=l(t);if(h.hasMoved()){const t=p(g);if(t){const n={...r,type:e.PdfAnnotationSubtype.LINK,target:void 0,created:new Date,id:e.uuidV4(),pageIndex:o,rect:t.data.rect};i(n)}}else h.onEnd(g);d(null),a(null),h.reset(),null==(s=n.releasePointerCapture)||s.call(n)},onPointerLeave:(t,e)=>{var n;d(null),a(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;d(null),a(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},ve={toolId:"__textMarkup__",handle(t,n,o){const i=t.getTool();if(i)for(const a of n){const n=e.uuidV4();o().then(e=>{var o;t.createAnnotation(a.pageIndex,{...i.defaults,rect:a.rect,segmentRects:a.segmentRects,pageIndex:a.pageIndex,created:new Date,id:n,...null!=e&&{custom:{text:e}}}),(null==(o=i.behavior)?void 0:o.selectAfterCreate)&&t.selectAnnotation(a.pageIndex,n)})}}};function Ie(t){const e=t.size.height,n=e/2,o=n;return{origin:{x:t.origin.x+t.size.width-o/2,y:t.origin.y+e/2},size:{width:o,height:n}}}const Pe={toolId:"insertText",handle(t,n,o){const i=t.getTool();if(!i)return;const a=()=>({strokeColor:i.defaults.strokeColor??"#E44234",opacity:i.defaults.opacity??1,flags:i.defaults.flags??["print"]});for(const s of n){const n=s.segmentRects[s.segmentRects.length-1];if(!n)continue;const r=Ie(n),c=e.uuidV4(),d=a();o().then(n=>{var o;t.createAnnotation(s.pageIndex,{type:e.PdfAnnotationSubtype.CARET,id:c,pageIndex:s.pageIndex,rect:r,strokeColor:d.strokeColor,opacity:d.opacity,intent:"Insert",rectangleDifferences:{left:.5,top:.5,right:.5,bottom:.5},created:new Date,flags:d.flags,...null!=n&&{custom:{text:n}}}),(null==(o=i.behavior)?void 0:o.selectAfterCreate)&&t.selectAnnotation(s.pageIndex,c)})}}},Se={toolId:"replaceText",handle(t,n,o){const i=t.getTool();if(!i)return;const a=()=>({strokeColor:i.defaults.strokeColor??"#E44234",opacity:i.defaults.opacity??1,flags:i.defaults.flags??["print"]});for(const s of n){const n=s.segmentRects[s.segmentRects.length-1];if(!n)continue;const r=Ie(n),c=e.uuidV4(),d=e.uuidV4(),l=a();o().then(n=>{var o;t.createAnnotation(s.pageIndex,{type:e.PdfAnnotationSubtype.CARET,id:c,pageIndex:s.pageIndex,rect:r,strokeColor:l.strokeColor,opacity:l.opacity,intent:"Replace",rectangleDifferences:{left:.5,top:.5,right:.5,bottom:.5},created:new Date,flags:l.flags}),t.createAnnotation(s.pageIndex,{type:e.PdfAnnotationSubtype.STRIKEOUT,id:d,pageIndex:s.pageIndex,rect:s.rect,segmentRects:s.segmentRects,strokeColor:l.strokeColor,opacity:l.opacity,intent:"StrikeOutTextEdit",inReplyToId:c,replyType:e.PdfAnnotationReplyType.Group,created:new Date,flags:l.flags,...null!=n&&{custom:{text:n}}}),(null==(o=i.behavior)?void 0:o.selectAfterCreate)&&t.selectAnnotation(s.pageIndex,c)})}}},Te=(t,n)=>{switch(n.type){case"vertex-edit":default:return n.changes;case"move":{if(!n.changes.rect)return n.changes;const{dx:e,dy:o,rects:i}=jt(t,n.changes.rect);return{...i,inkList:t.inkList.map(t=>({points:t.points.map(t=>({x:t.x+e,y:t.y+o}))}))}}case"resize":{if(!n.changes.rect)return n.changes;const{scaleX:e,scaleY:o,oldRect:i,resolvedRect:a,rects:s}=wt(t,n.changes.rect,n.metadata),r=(t,e)=>({origin:{x:t.origin.x+e,y:t.origin.y+e},size:{width:Math.max(1,t.size.width-2*e),height:Math.max(1,t.size.height-2*e)}}),c=.001,d=Math.abs(e-1)>c,l=Math.abs(o-1)>c,u=d&&!l?e:!d&&l?o:Math.min(e,o),h=Math.max(1,t.strokeWidth*u),p=Math.max(1,Math.min(a.size.width,a.size.height)),g=Math.min(h,p),f=Number(g.toFixed(1)),m=r(i,t.strokeWidth/2),y=r(a,f/2),A=y.size.width/Math.max(m.size.width,1e-6),x=y.size.height/Math.max(m.size.height,1e-6);return{...s,inkList:t.inkList.map(t=>({points:t.points.map(t=>({x:y.origin.x+(t.x-m.origin.x)*A,y:y.origin.y+(t.y-m.origin.y)*x}))})),strokeWidth:f}}case"rotate":{const e=Et(t,n);if(!e)return n.changes;const{dx:o,dy:i}=zt(t,e);return{...e,inkList:t.inkList.map(t=>({points:t.points.map(t=>({x:t.x+o,y:t.y+i}))}))}}case"property-update":{if(!(void 0!==n.changes.strokeWidth||void 0!==n.changes.rotation))return n.changes;const o={...t,...n.changes},i=o.inkList.flatMap(t=>t.points),a=e.expandRect(e.rectFromPoints(i),o.strokeWidth/2),s=n.changes.rotation??t.rotation??0;return t.unrotatedRect||void 0!==n.changes.rotation?{...n.changes,unrotatedRect:a,rect:e.calculateRotatedRectAABBAroundPoint(a,s,Rt(t))}:{...n.changes,rect:a}}}},Re=(t,n)=>{switch(n.type){case"vertex-edit":if(n.changes.linePoints){const{start:e,end:o}=n.changes.linePoints,i=[e,o],a=kt(t,i,St(i,t.strokeWidth,t.lineEndings));return{...Tt(t,St(a,t.strokeWidth,t.lineEndings)),linePoints:{start:a[0],end:a[1]}}}return n.changes;case"move":{if(!n.changes.rect)return n.changes;const{dx:e,dy:o,rects:i}=jt(t,n.changes.rect);return{...i,linePoints:{start:{x:t.linePoints.start.x+e,y:t.linePoints.start.y+o},end:{x:t.linePoints.end.x+e,y:t.linePoints.end.y+o}}}}case"resize":{if(!n.changes.rect)return n.changes;const{scaleX:e,scaleY:o,oldRect:i,resolvedRect:a,rects:s}=wt(t,n.changes.rect,n.metadata);return{...s,linePoints:{start:{x:a.origin.x+(t.linePoints.start.x-i.origin.x)*e,y:a.origin.y+(t.linePoints.start.y-i.origin.y)*o},end:{x:a.origin.x+(t.linePoints.end.x-i.origin.x)*e,y:a.origin.y+(t.linePoints.end.y-i.origin.y)*o}}}}case"rotate":{const e=Et(t,n);if(!e)return n.changes;const{dx:o,dy:i}=zt(t,e);return{...e,linePoints:{start:{x:t.linePoints.start.x+o,y:t.linePoints.start.y+i},end:{x:t.linePoints.end.x+o,y:t.linePoints.end.y+i}}}}case"property-update":{if(!(void 0!==n.changes.strokeWidth||void 0!==n.changes.lineEndings||void 0!==n.changes.rotation))return n.changes;const o={...t,...n.changes},i=St([o.linePoints.start,o.linePoints.end],o.strokeWidth,o.lineEndings),a=n.changes.rotation??t.rotation??0;return t.unrotatedRect||void 0!==n.changes.rotation?{...n.changes,unrotatedRect:i,rect:e.calculateRotatedRectAABBAroundPoint(i,a,Rt(t))}:{...n.changes,rect:i}}default:return n.changes}};function Me(t,e){return(t??0)>0?ge(t,e,!1):e/2}const ke=[...[{id:"highlight",name:"Highlight",labelKey:"annotation.highlight",categories:["annotation","markup"],matchScore:t=>t.type===e.PdfAnnotationSubtype.HIGHLIGHT?1:0,interaction:{exclusive:!1,textSelection:!0,isDraggable:!1,isResizable:!1,isRotatable:!1,isGroupDraggable:!1,isGroupResizable:!1},defaults:{type:e.PdfAnnotationSubtype.HIGHLIGHT,strokeColor:"#FFCD45",color:"#FFCD45",opacity:1,blendMode:e.PdfBlendMode.Multiply}},{id:"underline",name:"Underline",labelKey:"annotation.underline",categories:["annotation","markup"],matchScore:t=>t.type===e.PdfAnnotationSubtype.UNDERLINE?1:0,interaction:{exclusive:!1,textSelection:!0,isDraggable:!1,isResizable:!1,isRotatable:!1,isGroupDraggable:!1,isGroupResizable:!1},defaults:{type:e.PdfAnnotationSubtype.UNDERLINE,strokeColor:"#E44234",color:"#E44234",opacity:1}},{id:"strikeout",name:"Strikeout",labelKey:"annotation.strikeout",categories:["annotation","markup"],matchScore:t=>t.type===e.PdfAnnotationSubtype.STRIKEOUT?1:0,interaction:{exclusive:!1,textSelection:!0,isDraggable:!1,isResizable:!1,isRotatable:!1,isGroupDraggable:!1,isGroupResizable:!1},defaults:{type:e.PdfAnnotationSubtype.STRIKEOUT,strokeColor:"#E44234",color:"#E44234",opacity:1}},{id:"squiggly",name:"Squiggly",labelKey:"annotation.squiggly",categories:["annotation","markup"],matchScore:t=>t.type===e.PdfAnnotationSubtype.SQUIGGLY?1:0,interaction:{exclusive:!1,textSelection:!0,isDraggable:!1,isResizable:!1,isRotatable:!1,isGroupDraggable:!1,isGroupResizable:!1},defaults:{type:e.PdfAnnotationSubtype.SQUIGGLY,strokeColor:"#E44234",color:"#E44234",opacity:1}}],...[{id:"insertText",name:"Insert Text",labelKey:"annotation.insertText",categories:["annotation","markup"],matchScore:t=>{var n;return t.type!==e.PdfAnnotationSubtype.CARET?0:(null==(n=t.intent)?void 0:n.includes("Insert"))?2:1},interaction:{exclusive:!1,textSelection:!0,showSelectionRects:!0,isDraggable:!1,isResizable:!1,isRotatable:!1,isGroupDraggable:!1,isGroupResizable:!1},defaults:{type:e.PdfAnnotationSubtype.CARET,strokeColor:"#E44234",opacity:1,intent:"Insert"},selectionHandler:Pe}],...[{id:"replaceText",name:"Replace Text",labelKey:"annotation.replaceText",categories:["annotation","markup"],matchScore:t=>{var n,o;return t.type===e.PdfAnnotationSubtype.STRIKEOUT&&(null==(n=t.intent)?void 0:n.includes("StrikeOutTextEdit"))||t.type===e.PdfAnnotationSubtype.CARET&&(null==(o=t.intent)?void 0:o.includes("Replace"))?2:0},interaction:{exclusive:!1,textSelection:!0,isDraggable:!1,isResizable:!1,isRotatable:!1,isGroupDraggable:!1,isGroupResizable:!1},defaults:{type:e.PdfAnnotationSubtype.STRIKEOUT,strokeColor:"#E44234",opacity:1,intent:"StrikeOutTextEdit"},selectionHandler:Se}],...[{id:"ink",name:"Pen",labelKey:"annotation.ink",categories:["annotation","markup"],matchScore:t=>t.type===e.PdfAnnotationSubtype.INK&&"InkHighlight"!==t.intent?5:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!0,lockAspectRatio:!1},defaults:{type:e.PdfAnnotationSubtype.INK,strokeColor:"#E44234",color:"#E44234",opacity:1,strokeWidth:6},behavior:{commitDelay:800},transform:Te,pointerHandler:At},{id:"inkHighlighter",name:"Ink Highlighter",labelKey:"annotation.inkHighlighter",categories:["annotation","markup"],matchScore:t=>t.type===e.PdfAnnotationSubtype.INK&&"InkHighlight"===t.intent?10:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!0,lockAspectRatio:!1,lockGroupAspectRatio:t=>{const e=((t.rotation??0)%90+90)%90;return e>=6&&e<=84}},defaults:{type:e.PdfAnnotationSubtype.INK,intent:"InkHighlight",strokeColor:"#FFCD45",color:"#FFCD45",opacity:1,strokeWidth:14,blendMode:e.PdfBlendMode.Multiply},behavior:{commitDelay:800,smartLineRecognition:!0,smartLineThreshold:.15},transform:Te,pointerHandler:At}],...[{id:"circle",name:"Circle",labelKey:"annotation.circle",categories:["annotation","shape"],matchScore:t=>t.type===e.PdfAnnotationSubtype.CIRCLE?1:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!0,lockAspectRatio:!1,lockGroupAspectRatio:t=>{const e=((t.rotation??0)%90+90)%90;return e>=6&&e<=84}},defaults:{type:e.PdfAnnotationSubtype.CIRCLE,color:"transparent",opacity:1,strokeWidth:6,strokeColor:"#E44234",strokeStyle:e.PdfAnnotationBorderStyle.SOLID},clickBehavior:{enabled:!0,defaultSize:{width:100,height:100}},transform:(t,e)=>{switch(e.type){case"move":return e.changes.rect?jt(t,e.changes.rect).rects:e.changes;case"resize":return e.changes.rect?wt(t,e.changes.rect,e.metadata).rects:e.changes;case"rotate":return Et(t,e)??e.changes;case"property-update":{let n=e.changes;const o=void 0!==e.changes.cloudyBorderIntensity,i=void 0!==e.changes.strokeWidth,a=(t.cloudyBorderIntensity??0)>0;if(o||i&&a){const o={...t,...e.changes},i=o.cloudyBorderIntensity??0;if(i>0){const t=ge(i,o.strokeWidth,!0);n={...n,rectangleDifferences:{left:t,top:t,right:t,bottom:t}}}else n={...n,rectangleDifferences:void 0}}return void 0!==e.changes.rotation&&(n={...n,...Lt(t,e.changes.rotation)}),n}default:return e.changes}},pointerHandler:xe}],...[{id:"square",name:"Square",labelKey:"annotation.square",categories:["annotation","shape"],matchScore:t=>t.type===e.PdfAnnotationSubtype.SQUARE?1:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!0,lockAspectRatio:!1,lockGroupAspectRatio:t=>{const e=((t.rotation??0)%90+90)%90;return e>=6&&e<=84}},defaults:{type:e.PdfAnnotationSubtype.SQUARE,color:"transparent",opacity:1,strokeWidth:6,strokeColor:"#E44234",strokeStyle:e.PdfAnnotationBorderStyle.SOLID},clickBehavior:{enabled:!0,defaultSize:{width:100,height:100}},transform:(t,e)=>{switch(e.type){case"move":return e.changes.rect?jt(t,e.changes.rect).rects:e.changes;case"resize":return e.changes.rect?wt(t,e.changes.rect,e.metadata).rects:e.changes;case"rotate":return Et(t,e)??e.changes;case"property-update":{let n=e.changes;const o=void 0!==e.changes.cloudyBorderIntensity,i=void 0!==e.changes.strokeWidth,a=(t.cloudyBorderIntensity??0)>0;if(o||i&&a){const o={...t,...e.changes},i=o.cloudyBorderIntensity??0;if(i>0){const t=ge(i,o.strokeWidth,!1);n={...n,rectangleDifferences:{left:t,top:t,right:t,bottom:t}}}else n={...n,rectangleDifferences:void 0}}return void 0!==e.changes.rotation&&(n={...n,...Lt(t,e.changes.rotation)}),n}default:return e.changes}},pointerHandler:me}],...[{id:"line",name:"Line",labelKey:"annotation.line",categories:["annotation","shape"],matchScore:t=>t.type===e.PdfAnnotationSubtype.LINE&&"LineArrow"!==t.intent?5:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!1,lockAspectRatio:!1,isGroupResizable:!0,lockGroupAspectRatio:t=>{const e=((t.rotation??0)%90+90)%90;return e>=6&&e<=84}},defaults:{type:e.PdfAnnotationSubtype.LINE,color:"transparent",opacity:1,strokeWidth:6,strokeColor:"#E44234"},clickBehavior:{enabled:!0,defaultLength:100,defaultAngle:0},transform:Re,pointerHandler:Ft},{id:"lineArrow",name:"Arrow",labelKey:"annotation.arrow",categories:["annotation","shape"],matchScore:t=>t.type===e.PdfAnnotationSubtype.LINE&&"LineArrow"===t.intent?10:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!1,lockAspectRatio:!1,isGroupResizable:!0,lockGroupAspectRatio:t=>{const e=((t.rotation??0)%90+90)%90;return e>=6&&e<=84}},defaults:{type:e.PdfAnnotationSubtype.LINE,intent:"LineArrow",color:"transparent",opacity:1,strokeWidth:6,strokeColor:"#E44234",lineEndings:{start:e.PdfAnnotationLineEnding.None,end:e.PdfAnnotationLineEnding.OpenArrow}},clickBehavior:{enabled:!0,defaultLength:100,defaultAngle:0},transform:Re,pointerHandler:Ft}],...[{id:"polyline",name:"Polyline",labelKey:"annotation.polyline",categories:["annotation","shape"],matchScore:t=>t.type===e.PdfAnnotationSubtype.POLYLINE?1:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!1,lockAspectRatio:!1,isGroupResizable:!0,lockGroupAspectRatio:t=>{const e=((t.rotation??0)%90+90)%90;return e>=6&&e<=84}},defaults:{type:e.PdfAnnotationSubtype.POLYLINE,color:"transparent",opacity:1,strokeWidth:6,strokeColor:"#E44234"},transform:(t,n)=>{switch(n.type){case"vertex-edit":if(n.changes.vertices&&n.changes.vertices.length){const e=n.changes.vertices,o=kt(t,e,St(e,t.strokeWidth,t.lineEndings));return{...Tt(t,St(o,t.strokeWidth,t.lineEndings)),vertices:o}}return n.changes;case"move":{if(!n.changes.rect)return n.changes;const{dx:e,dy:o,rects:i}=jt(t,n.changes.rect);return{...i,vertices:t.vertices.map(t=>({x:t.x+e,y:t.y+o}))}}case"resize":{if(!n.changes.rect)return n.changes;const{scaleX:e,scaleY:o,oldRect:i,resolvedRect:a,rects:s}=wt(t,n.changes.rect,n.metadata);return{...s,vertices:t.vertices.map(t=>({x:a.origin.x+(t.x-i.origin.x)*e,y:a.origin.y+(t.y-i.origin.y)*o}))}}case"rotate":{const e=Et(t,n);if(!e)return n.changes;const{dx:o,dy:i}=zt(t,e);return{...e,vertices:t.vertices.map(t=>({x:t.x+o,y:t.y+i}))}}case"property-update":{if(!(void 0!==n.changes.strokeWidth||void 0!==n.changes.lineEndings||void 0!==n.changes.rotation))return n.changes;const o={...t,...n.changes},i=St(o.vertices,o.strokeWidth,o.lineEndings),a=n.changes.rotation??t.rotation??0;return t.unrotatedRect||void 0!==n.changes.rotation?{...n.changes,unrotatedRect:i,rect:e.calculateRotatedRectAABBAroundPoint(i,a,Rt(t))}:{...n.changes,rect:i}}default:return n.changes}},pointerHandler:Wt}],...[{id:"polygon",name:"Polygon",labelKey:"annotation.polygon",categories:["annotation","shape"],matchScore:t=>t.type===e.PdfAnnotationSubtype.POLYGON?1:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!1,lockAspectRatio:!1,isGroupResizable:!0,lockGroupAspectRatio:t=>{const e=((t.rotation??0)%90+90)%90;return e>=6&&e<=84}},defaults:{type:e.PdfAnnotationSubtype.POLYGON,color:"transparent",opacity:1,strokeWidth:6,strokeColor:"#E44234"},transform:(t,n)=>{switch(n.type){case"vertex-edit":if(n.changes.vertices&&n.changes.vertices.length){const o=Me(t.cloudyBorderIntensity,t.strokeWidth),i=n.changes.vertices,a=kt(t,i,e.expandRect(e.rectFromPoints(i),o));return{...Tt(t,e.expandRect(e.rectFromPoints(a),o)),vertices:a}}return n.changes;case"move":{if(!n.changes.rect)return n.changes;const{dx:e,dy:o,rects:i}=jt(t,n.changes.rect);return{...i,vertices:t.vertices.map(t=>({x:t.x+e,y:t.y+o}))}}case"resize":{if(!n.changes.rect)return n.changes;const{scaleX:e,scaleY:o,oldRect:i,resolvedRect:a,rects:s}=wt(t,n.changes.rect,n.metadata);return{...s,vertices:t.vertices.map(t=>({x:a.origin.x+(t.x-i.origin.x)*e,y:a.origin.y+(t.y-i.origin.y)*o}))}}case"rotate":{const e=Et(t,n);if(!e)return n.changes;const{dx:o,dy:i}=zt(t,e);return{...e,vertices:t.vertices.map(t=>({x:t.x+o,y:t.y+i}))}}case"property-update":{const o=void 0!==n.changes.cloudyBorderIntensity;if(!(void 0!==n.changes.strokeWidth||void 0!==n.changes.rotation||o))return n.changes;const i={...t,...n.changes},a=Me(i.cloudyBorderIntensity,i.strokeWidth),s=e.expandRect(e.rectFromPoints(i.vertices),a);let r=n.changes;const c=(t.cloudyBorderIntensity??0)>0;if(o||void 0!==n.changes.strokeWidth&&c){const t=i.cloudyBorderIntensity??0;if(t>0){const e=ge(t,i.strokeWidth,!1);r={...r,rectangleDifferences:{left:e,top:e,right:e,bottom:e}}}else r={...r,rectangleDifferences:void 0}}const d=n.changes.rotation??t.rotation??0;return t.unrotatedRect||void 0!==n.changes.rotation?{...r,unrotatedRect:s,rect:e.calculateRotatedRectAABBAroundPoint(s,d,Rt(t))}:{...r,rect:s}}default:return n.changes}},pointerHandler:fe}],...[{id:"textComment",name:"Comment",labelKey:"annotation.text",categories:["annotation","markup"],matchScore:t=>t.type!==e.PdfAnnotationSubtype.TEXT||t.inReplyToId?0:1,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!1,isRotatable:!1},defaults:{type:e.PdfAnnotationSubtype.TEXT,strokeColor:"#FFCD45",opacity:1},behavior:{selectAfterCreate:!0},pointerHandler:Bt}],...[{id:"freeText",name:"Free Text",labelKey:"annotation.freeText",categories:["annotation","markup"],matchScore:t=>t.type===e.PdfAnnotationSubtype.FREETEXT?1:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!0,lockAspectRatio:!1,lockGroupAspectRatio:t=>{const e=((t.rotation??0)%90+90)%90;return e>=6&&e<=84}},defaults:{type:e.PdfAnnotationSubtype.FREETEXT,contents:"Insert text",fontSize:14,fontColor:"#E44234",fontFamily:e.PdfStandardFont.Helvetica,textAlign:e.PdfTextAlignment.Left,verticalAlign:e.PdfVerticalAlignment.Top,color:"transparent",backgroundColor:"transparent",opacity:1},clickBehavior:{enabled:!0,defaultSize:{width:100,height:20},defaultContent:"Insert text"},behavior:{insertUpright:!0,editAfterCreate:!0,selectAfterCreate:!0},transform:(t,e)=>{switch(e.type){case"move":return e.changes.rect?jt(t,e.changes.rect).rects:e.changes;case"resize":return e.changes.rect?wt(t,e.changes.rect,e.metadata).rects:e.changes;case"rotate":return Et(t,e)??e.changes;case"property-update":return void 0!==e.changes.rotation?{...e.changes,...Lt(t,e.changes.rotation)}:e.changes;default:return e.changes}},pointerHandler:Gt}],...[{id:"stamp",name:"Image",labelKey:"annotation.stamp",categories:["annotation","markup"],matchScore:t=>t.type===e.PdfAnnotationSubtype.STAMP?1:0,interaction:{exclusive:!1,cursor:"copy",isDraggable:!0,isResizable:!0,lockAspectRatio:!0,lockGroupAspectRatio:!0},defaults:{type:e.PdfAnnotationSubtype.STAMP},behavior:{insertUpright:!0,useAppearanceStream:!1},transform:(t,e)=>{switch(e.type){case"move":return e.changes.rect?jt(t,e.changes.rect).rects:e.changes;case"resize":return e.changes.rect?wt(t,e.changes.rect,e.metadata).rects:e.changes;case"rotate":return Et(t,e)??e.changes;case"property-update":return void 0!==e.changes.rotation?{...e.changes,...Lt(t,e.changes.rotation)}:e.changes;default:return e.changes}},pointerHandler:Ae}],...[{id:"link",name:"Link",labelKey:"annotation.link",categories:["annotation","markup"],matchScore:t=>t.type===e.PdfAnnotationSubtype.LINK?1:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!0,isRotatable:!1},defaults:{type:e.PdfAnnotationSubtype.LINK,strokeColor:"#0000FF",strokeWidth:2,strokeStyle:e.PdfAnnotationBorderStyle.UNDERLINE},clickBehavior:{enabled:!0,defaultSize:{width:100,height:20}},pointerHandler:be}]],Ce=["#E44234","#FF8D00","#FFCD45","#5CC96E","#25D2D1","#597CE2","#C544CE","#7D2E25","#000000","#FFFFFF"],De=t=>1===t.length?t[0]:null,Ee=t=>({pages:{},byUid:{},selectedUids:[],selectedUid:null,activeToolId:null,hasPendingChanges:!1,locked:(null==t?void 0:t.locked)??{type:w.None}}),Le=(t,e,n)=>{const o=t.byUid[e];return o?{...t,byUid:{...t.byUid,[e]:{...o,commitState:"synced"===o.commitState||"moved"===o.commitState?"dirty":o.commitState,object:{...o.object,...n},dictMode:!0}},hasPendingChanges:!0}:t},je=(t,e,n)=>{const o=t.byUid[e];return o?{...t,byUid:{...t.byUid,[e]:{...o,commitState:"synced"===o.commitState?"moved":o.commitState,object:{...o.object,...n}}},hasPendingChanges:!0}:t},we=t=>{const e=new Map;ke.forEach(t=>e.set(t.id,t));const n=new Map(e);(t.tools||[]).forEach(t=>{const o=e.get(t.id);o?n.set(t.id,{...o,...t,defaults:{...o.defaults,...t.defaults},interaction:{...o.interaction,...t.interaction},behavior:{...o.behavior,...t.behavior},...o.clickBehavior||t.clickBehavior?{clickBehavior:{...o.clickBehavior,...t.clickBehavior}}:{}}):n.set(t.id,t)});return{documents:{},activeDocumentId:null,tools:Array.from(n.values()).map(e=>{var n,o,i;return{...e,behavior:{...e.behavior,deactivateToolAfterCreate:(null==(n=e.behavior)?void 0:n.deactivateToolAfterCreate)??t.deactivateToolAfterCreate??!1,selectAfterCreate:(null==(o=e.behavior)?void 0:o.selectAfterCreate)??t.selectAfterCreate??!0,editAfterCreate:(null==(i=e.behavior)?void 0:i.editAfterCreate)??t.editAfterCreate??!1}}}),colorPresets:t.colorPresets??Ce}},ze=class n extends t.BasePlugin{constructor(e,n,o){var i,a,s,r;super(e,n),this.ANNOTATION_HISTORY_TOPIC="annotations",this.state$=t.createScopedEmitter((t,e)=>({documentId:t,state:e})),this.pendingContexts=new Map,this.isInitialLoadComplete=new Map,this.importQueue=new Map,this.commitInProgress=new Map,this.activeTool$=t.createScopedEmitter((t,e)=>({documentId:t,tool:e})),this.events$=t.createScopedEmitter((t,e)=>e,{cache:!1}),this.toolsChange$=t.createBehaviorEmitter(),this.appearanceCache=new Map,this.unifiedDragStates=new Map,this.unifiedDrag$=t.createBehaviorEmitter(),this.unifiedResizeStates=new Map,this.unifiedResize$=t.createBehaviorEmitter(),this.unifiedRotateStates=new Map,this.unifiedRotate$=t.createBehaviorEmitter(),this.navigate$=t.createScopedEmitter((t,e)=>e,{cache:!1}),this.config=o,this.selection=(null==(i=n.getPlugin("selection"))?void 0:i.provides())??null,this.history=(null==(a=n.getPlugin("history"))?void 0:a.provides())??null,this.scroll=(null==(s=n.getPlugin("scroll"))?void 0:s.provides())??null,this.interactionManager=(null==(r=n.getPlugin("interaction-manager"))?void 0:r.provides())??null}onDocumentLoadingStarted(t){this.dispatch(function(t,e){return{type:i,payload:{documentId:t,state:e}}}(t,Ee(this.config))),this.pendingContexts.set(t,new Map),this.isInitialLoadComplete.set(t,!1),this.importQueue.set(t,[]),this.logger.debug("AnnotationPlugin","DocumentOpened",`Initialized annotation state for document: ${t}`)}onDocumentLoaded(t){const e=this.getCoreDocument(t);if((null==e?void 0:e.document)&&this.getAllAnnotations(t,e.document),this.selection)for(const n of this.state.tools)n.interaction.textSelection&&this.selection.enableForMode(n.interaction.mode??n.id,{showSelectionRects:n.interaction.showSelectionRects??!1,enableSelection:!0,enableMarquee:!1})}onDocumentClosed(t){this.dispatch(function(t){return{type:a,payload:t}}(t)),this.pendingContexts.delete(t),this.isInitialLoadComplete.delete(t),this.importQueue.delete(t),this.appearanceCache.delete(t),this.state$.clearScope(t),this.activeTool$.clearScope(t),this.events$.clearScope(t),this.navigate$.clearScope(t),this.logger.debug("AnnotationPlugin","DocumentClosed",`Cleaned up annotation state for document: ${t}`)}async initialize(){var t,n,o;this.state.tools.forEach(t=>this.registerInteractionForTool(t)),this.history&&this.history.onHistoryChange(t=>{t.topic===this.ANNOTATION_HISTORY_TOPIC&&!1!==this.config.autoCommit&&this.commit(t.documentId)}),null==(t=this.interactionManager)||t.onModeChange(t=>{var e,n;const o=(null==(e=this.state.tools.find(e=>(e.interaction.mode??e.id)===t.activeMode))?void 0:e.id)??null;o!==((null==(n=this.state.documents[t.documentId])?void 0:n.activeToolId)??null)&&t.documentId&&this.dispatch(k(t.documentId,o))}),null==(n=this.selection)||n.onMarqueeEnd(({documentId:t,pageIndex:e,rect:n,modeId:o})=>{if("pointerMode"!==o)return;const i=this.state.documents[t];if(!i)return;const a=(i.pages[e]??[]).map(t=>i.byUid[t]).filter(t=>void 0!==t).filter(t=>tt(t)).filter(t=>z(n,t.object.rect)).filter(e=>!this.isAnnotationLocked(e.object,t)).map(t=>t.object.id);if(a.length>0){const e=new Set;for(const n of a)if(this.isInGroupMethod(n,t)){const o=this.getGroupMembersMethod(n,t);for(const t of o)e.add(t.object.id)}else e.add(n);this.setSelectionMethod([...e],t)}}),null==(o=this.selection)||o.onEndSelection(({documentId:t})=>{var n,o,i,a;if(!this.checkPermission(t,e.PdfPermissionFlag.ModifyAnnotations))return;const s=this.getActiveTool(t);if(!s||!s.interaction.textSelection)return;const r=null==(n=this.selection)?void 0:n.getFormattedSelection(),c=null==(o=this.selection)?void 0:o.getSelectedText();if(!r||!c)return;const d={toolId:s.id,documentId:t,getTool:()=>this.getActiveTool(t)??null,createAnnotation:(e,n)=>this.createAnnotation(e,n,void 0,t),selectAnnotation:(e,n)=>this.selectAnnotation(e,n,t)};(s.selectionHandler??ve).handle(d,r,()=>new Promise(t=>{c.wait(e=>t(e.join("\n")),()=>t(void 0))})),(null==(i=s.behavior)?void 0:i.deactivateToolAfterCreate)&&this.setActiveTool(null,t),null==(a=this.selection)||a.clear()})}registerInteractionForTool(t){var e;null==(e=this.interactionManager)||e.registerMode({id:t.interaction.mode??t.id,scope:"page",exclusive:t.interaction.exclusive,cursor:t.interaction.cursor})}buildCapability(){return{getActiveTool:()=>this.getActiveTool(),setActiveTool:(t,e)=>this.setActiveTool(t,void 0,e),getState:()=>this.getDocumentState(),getPageAnnotations:t=>this.getPageAnnotations(t),getSelectedAnnotation:()=>this.getSelectedAnnotation(),getSelectedAnnotations:()=>this.getSelectedAnnotationsMethod(),getSelectedAnnotationIds:()=>this.getSelectedAnnotationIdsMethod(),getAnnotationById:t=>this.getAnnotationById(t),getAnnotations:t=>this.getAnnotationsMethod(t),selectAnnotation:(t,e)=>this.selectAnnotation(t,e),toggleSelection:(t,e)=>this.toggleSelectionMethod(t,e),addToSelection:(t,e)=>this.addToSelectionMethod(t,e),removeFromSelection:t=>this.removeFromSelectionMethod(t),setSelection:t=>this.setSelectionMethod(t),deselectAnnotation:()=>this.deselectAnnotation(),importAnnotations:t=>this.importAnnotations(t),exportAnnotations:(t,e)=>this.exportAnnotationsMethod(t,e),createAnnotation:(t,e,n)=>this.createAnnotation(t,e,n),updateAnnotation:(t,e,n)=>this.updateAnnotation(t,e,n),updateAnnotations:t=>this.updateAnnotationsMethod(t),moveAnnotation:(t,e,n,o,i)=>this.moveAnnotationMethod(t,e,n,o,i),deleteAnnotation:(t,e)=>this.deleteAnnotation(t,e),deleteAnnotations:(t,e)=>this.deleteAnnotationsMethod(t,e),deleteAllAnnotations:t=>this.deleteAllAnnotationsMethod(t),purgeAnnotation:(t,e,n)=>this.purgeAnnotationMethod(t,e,n),renderAnnotation:t=>this.renderAnnotation(t),getPageAppearances:(t,e,n)=>this.getPageAppearances(t,e,n),invalidatePageAppearances:(t,e)=>this.invalidatePageAppearances(t,e),commit:()=>this.commit(),getAttachedLinks:(t,e)=>this.getAttachedLinksMethod(t,e),hasAttachedLinks:(t,e)=>this.hasAttachedLinksMethod(t,e),deleteAttachedLinks:(t,e)=>this.deleteAttachedLinksMethod(t,e),groupAnnotations:t=>this.groupAnnotationsMethod(t),ungroupAnnotations:(t,e)=>this.ungroupAnnotationsMethod(t,e),getGroupMembers:(t,e)=>this.getGroupMembersMethod(t,e),isInGroup:(t,e)=>this.isInGroupMethod(t,e),setLocked:(t,e)=>this.setLocked(t,e),getLocked:t=>this.getLocked(t),isAnnotationLocked:(t,e)=>this.isAnnotationLocked(t,e),isCategoryLocked:(t,e)=>this.isCategoryLockedMethod(t,e),isToolLocked:(t,e)=>this.isToolLockedMethod(t,e),syncAnnotationObject:(t,e,n)=>this.syncAnnotationObject(t,e,n),navigateTarget:(t,e)=>this.navigateTargetMethod(t,e),forDocument:t=>this.createAnnotationScope(t),getTools:()=>this.state.tools,getTool:t=>this.getTool(t),addTool:t=>{this.dispatch((t=>({type:P,payload:t}))(t)),this.registerInteractionForTool(t)},findToolForAnnotation:t=>this.findToolForAnnotation(t),setToolDefaults:(t,e)=>this.dispatch(((t,e)=>({type:I,payload:{toolId:t,patch:e}}))(t,e)),getColorPresets:()=>[...this.state.colorPresets],addColorPreset:t=>this.dispatch({type:v,payload:t}),transformAnnotation:(t,e)=>this.transformAnnotation(t,e),onStateChange:this.state$.onGlobal,onActiveToolChange:this.activeTool$.onGlobal,onAnnotationEvent:this.events$.onGlobal,onToolsChange:this.toolsChange$.on,onNavigate:this.navigate$.onGlobal}}createAnnotationScope(t){return{getState:()=>this.getDocumentState(t),getPageAnnotations:e=>this.getPageAnnotations(e,t),getSelectedAnnotation:()=>this.getSelectedAnnotation(t),getSelectedAnnotations:()=>this.getSelectedAnnotationsMethod(t),getSelectedAnnotationIds:()=>this.getSelectedAnnotationIdsMethod(t),getAnnotationById:e=>this.getAnnotationById(e,t),getAnnotations:e=>this.getAnnotationsMethod(e,t),selectAnnotation:(e,n)=>this.selectAnnotation(e,n,t),toggleSelection:(e,n)=>this.toggleSelectionMethod(e,n,t),addToSelection:(e,n)=>this.addToSelectionMethod(e,n,t),removeFromSelection:e=>this.removeFromSelectionMethod(e,t),setSelection:e=>this.setSelectionMethod(e,t),deselectAnnotation:()=>this.deselectAnnotation(t),getActiveTool:()=>this.getActiveTool(t),setActiveTool:(e,n)=>this.setActiveTool(e,t,n),findToolForAnnotation:t=>this.findToolForAnnotation(t),importAnnotations:e=>this.importAnnotations(e,t),exportAnnotations:e=>this.exportAnnotationsMethod(e,t),createAnnotation:(e,n,o)=>this.createAnnotation(e,n,o,t),updateAnnotation:(e,n,o)=>this.updateAnnotation(e,n,o,t),updateAnnotations:e=>this.updateAnnotationsMethod(e,t),moveAnnotation:(e,n,o,i)=>this.moveAnnotationMethod(e,n,o,i,t),deleteAnnotation:(e,n)=>this.deleteAnnotation(e,n,t),deleteAnnotations:e=>this.deleteAnnotationsMethod(e,t),deleteAllAnnotations:()=>this.deleteAllAnnotationsMethod(t),purgeAnnotation:(e,n)=>this.purgeAnnotationMethod(e,n,t),renderAnnotation:e=>this.renderAnnotation(e,t),getPageAppearances:(e,n)=>this.getPageAppearances(e,n,t),invalidatePageAppearances:e=>this.invalidatePageAppearances(e,t),commit:()=>this.commit(t),getAttachedLinks:e=>this.getAttachedLinksMethod(e,t),hasAttachedLinks:e=>this.hasAttachedLinksMethod(e,t),deleteAttachedLinks:e=>this.deleteAttachedLinksMethod(e,t),groupAnnotations:()=>this.groupAnnotationsMethod(t),ungroupAnnotations:e=>this.ungroupAnnotationsMethod(e,t),getGroupMembers:e=>this.getGroupMembersMethod(e,t),isInGroup:e=>this.isInGroupMethod(e,t),getGroupingAction:()=>this.getGroupingActionMethod(t),setLocked:e=>this.setLocked(e,t),getLocked:()=>this.getLocked(t),isAnnotationLocked:e=>this.isAnnotationLocked(e,t),isCategoryLocked:e=>this.isCategoryLockedMethod(e,t),isToolLocked:e=>this.isToolLockedMethod(e,t),syncAnnotationObject:(e,n)=>this.syncAnnotationObject(e,n,t),navigateTarget:e=>this.navigateTargetMethod(e,t),onStateChange:this.state$.forScope(t),onAnnotationEvent:this.events$.forScope(t),onActiveToolChange:this.activeTool$.forScope(t),onNavigate:this.navigate$.forScope(t)}}onStoreUpdated(t,e){for(const n in e.documents){const o=t.documents[n],i=e.documents[n];o!==i&&(this.state$.emit(n,i),o&&o.activeToolId!==i.activeToolId&&this.activeTool$.emit(n,this.getActiveTool(n)),(null==o?void 0:o.selectedUids)!==i.selectedUids&&this.updateAnnotationSelectionActivity(n,i))}if(t.tools!==e.tools){for(const t in e.documents)this.activeTool$.emit(t,this.getActiveTool(t));this.toolsChange$.emit({tools:e.tools})}}transformAnnotation(t,e){const n={type:e.type,changes:e.changes,metadata:e.metadata},o=this.findToolForAnnotation(t);return(null==o?void 0:o.transform)?o.transform(t,n):n.changes}registerPageHandlers(t,e,o,i){var a,s;const r=this.getCoreDocument(t),c=null==(a=null==r?void 0:r.document)?void 0:a.pages[e];if(!c)return()=>{};if(!this.interactionManager)return()=>{};const d=[],l=((c.rotation??0)+((null==r?void 0:r.rotation)??0))%4;for(const u of this.state.tools){const a=u.pointerHandler??((null==(s=u.defaults)?void 0:s.type)?n.defaultHandlerFactories.get(u.defaults.type):void 0);if(!a)continue;const r={pageIndex:e,pageSize:c.size,pageRotation:l,scale:o,services:i.services,onPreview:t=>i.onPreview(u.id,t),onCommit:(n,o)=>{var i,a,s;const r=(null==(i=u.behavior)?void 0:i.editAfterCreate)??!1;this.createAnnotation(e,n,o,t,{editAfterCreate:r}),(null==(a=u.behavior)?void 0:a.deactivateToolAfterCreate)&&this.setActiveTool(null,t),((null==(s=u.behavior)?void 0:s.selectAfterCreate)||r)&&this.selectAnnotation(e,n.id,t)},getTool:()=>this.state.tools.find(t=>t.id===u.id),getToolContext:()=>{var e;return null==(e=this.state.documents[t])?void 0:e.activeToolContext}},h=this.interactionManager.registerHandlers({documentId:t,modeId:u.interaction.mode??u.id,handlers:a.create(r),pageIndex:e});d.push(h)}return()=>d.forEach(t=>t())}getDocumentState(t){const e=t??this.getActiveDocumentId(),n=this.state.documents[e];if(!n)throw new Error(`Annotation state not found for document: ${e}`);return n}getAllAnnotations(t,n){this.engine.getAllAnnotations(n).wait(e=>{this.dispatch(((t,e)=>({type:s,payload:{documentId:t,annotations:e}}))(t,e)),this.isInitialLoadComplete.set(t,!0);const n=this.importQueue.get(t);n&&n.length>0&&this.processImportQueue(t),this.events$.emit(t,{type:"loaded",documentId:t,total:Object.values(e).reduce((t,e)=>t+e.length,0)})},e.ignore)}getPageAnnotations(t,n){const{pageIndex:o}=t,i=n??this.getActiveDocumentId(),a=this.getCoreDocument(i),s=null==a?void 0:a.document;if(!s)return e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Document not found"});const r=s.pages.find(t=>t.index===o);return r?this.engine.getPageAnnotations(s,r):e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Page not found"})}getSelectedAnnotation(t){return at(this.getDocumentState(t))}getAnnotationById(t,e){const n=this.getDocumentState(e);return ct(n,t)}getAnnotationsMethod(t,e){const n=this.getDocumentState(e);return void 0!==(null==t?void 0:t.pageIndex)?it(n,t.pageIndex).filter(t=>void 0!==t):Object.values(n.byUid)}renderAnnotation({pageIndex:t,annotation:n,options:o},i){const a=i??this.getActiveDocumentId(),s=this.getCoreDocument(a),r=null==s?void 0:s.document;if(!r)return e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Document not found"});const c=r.pages.find(e=>e.index===t);return c?this.engine.renderPageAnnotation(r,c,n,o):e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Page not found"})}getPageAppearances(t,n,o){const i=o??this.getActiveDocumentId(),a=this.getCoreDocument(i),s=null==a?void 0:a.document;if(!s)return e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Document not found"});const r=s.pages.find(e=>e.index===t);if(!r)return e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Page not found"});let c=this.appearanceCache.get(i);c||(c=new Map,this.appearanceCache.set(i,c));const d=c.get(t);if(d&&!n){const t=new e.Task;return t.resolve(d),t}const l=this.engine.renderPageAnnotations(s,r,n),u=new e.Task;return l.wait(e=>{c.set(t,e),u.resolve(e)},t=>{u.fail(t)}),u}invalidatePageAppearances(t,e){const n=e??this.getActiveDocumentId(),o=this.appearanceCache.get(n);o&&o.delete(t)}invalidateAnnotationAppearance(t,e,n){const o=n??this.getActiveDocumentId(),i=this.appearanceCache.get(o);if(!i)return;const a=i.get(e);a&&delete a[t]}exportAnnotationsMethod(t,n){const o=n??this.getActiveDocumentId(),i=this.getDocumentState(o),a=this.getCoreDocument(o),s=null==a?void 0:a.document;if(!s)return e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Document not found"});const r=void 0!==(null==t?void 0:t.pageIndex)?[t.pageIndex]:Object.keys(i.pages).map(Number),c=[];for(const g of r){const t=i.pages[g]??[];for(const n of t){const t=i.byUid[n];t&&"deleted"!==t.commitState&&c.push({annotation:t.object,pageIndex:g,isStamp:t.object.type===e.PdfAnnotationSubtype.STAMP})}}const d=c.filter(t=>t.isStamp);if(0===d.length)return e.PdfTaskHelper.resolve(c.map(t=>({annotation:t.annotation})));const l=e.PdfTaskHelper.create(),u=new Map;let h=d.length,p=!1;for(const e of d){const t=s.pages.find(t=>t.index===e.pageIndex);if(!t){h--;continue}this.engine.exportAnnotationAppearanceAsPdf(s,t,e.annotation).wait(t=>{p||(u.set(e.annotation.id,t),h--,0===h&&l.resolve(c.map(t=>({annotation:t.annotation,...u.has(t.annotation.id)?{ctx:{data:u.get(t.annotation.id),mimeType:"application/pdf"}}:{}}))))},t=>{p||(p=!0,l.reject(t.reason))})}return 0===h&&l.resolve(c.map(t=>({annotation:t.annotation}))),l}importAnnotations(t,e){const n=e??this.getActiveDocumentId();if(!this.isInitialLoadComplete.get(n)){const e=this.importQueue.get(n)||[];return e.push(...t),void this.importQueue.set(n,e)}this.processImportItems(n,t)}processImportQueue(t){const e=this.importQueue.get(t);if(!e||0===e.length)return;const n=[...e];this.importQueue.set(t,[]),this.processImportItems(t,n)}processImportItems(t,e){const n=this.pendingContexts.get(t);if(n){for(const o of e){const{annotation:e,ctx:i}=o,a=e.pageIndex,s=e.id;this.dispatch(C(t,a,e)),i&&n.set(s,i)}!1!==this.config.autoCommit&&this.commit(t)}}createAnnotation(t,n,o,i,a){const s=i??this.getActiveDocumentId();if(!this.checkPermission(s,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","CreateAnnotation",`Cannot create annotation: document ${s} lacks ModifyAnnotations permission`);const r=n.id,c=this.pendingContexts.get(s);if(!c)return;const d={...n,author:n.author??this.config.annotationAuthor},l=null==a?void 0:a.editAfterCreate,u=()=>{this.dispatch(C(s,t,d)),o&&c.set(r,o),this.events$.emit(s,{type:"create",documentId:s,annotation:d,pageIndex:t,ctx:o,committed:!1,editAfterCreate:l})};if(!this.history)return u(),void(this.config.autoCommit&&this.commit(s));const h={execute:u,undo:()=>{c.delete(r),this.dispatch(S(s)),this.dispatch(L(s,t,r)),this.events$.emit(s,{type:"delete",documentId:s,annotation:d,pageIndex:t,committed:!1})},metadata:{annotationIds:[r]}};this.history.forDocument(s).register(h,this.ANNOTATION_HISTORY_TOPIC)}buildPatch(t,e){return"rect"in e?e:this.transformAnnotation(t,{type:"property-update",changes:e})}updateAnnotation(t,n,o,i){const a=i??this.getActiveDocumentId();if(!this.checkPermission(a,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","UpdateAnnotation",`Cannot update annotation: document ${a} lacks ModifyAnnotations permission`);const s=this.getDocumentState(a).byUid[n].object,r=this.buildPatch(s,{...o,author:o.author??this.config.annotationAuthor}),c=()=>{this.dispatch(D(a,t,n,r)),this.events$.emit(a,{type:"update",documentId:a,annotation:s,pageIndex:t,patch:r,committed:!1})};if(!this.history)return c(),void(!1!==this.config.autoCommit&&this.commit(a));const d=Object.fromEntries(Object.keys(o).map(t=>[t,s[t]])),l={execute:c,undo:()=>{this.dispatch(D(a,t,n,d)),this.events$.emit(a,{type:"update",documentId:a,annotation:s,pageIndex:t,patch:d,committed:!1})},metadata:{annotationIds:[n]}};this.history.forDocument(a).register(l,this.ANNOTATION_HISTORY_TOPIC)}deleteAnnotation(t,n,o){var i;const a=o??this.getActiveDocumentId();if(!this.checkPermission(a,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","DeleteAnnotation",`Cannot delete annotation: document ${a} lacks ModifyAnnotations permission`);const s=this.getDocumentState(a),r=null==(i=s.byUid[n])?void 0:i.object;if(!r)return;const c=lt(s,n),d=c.map(t=>{var e;return null==(e=s.byUid[t.id])?void 0:e.object}).filter(t=>void 0!==t),l=()=>{var e;for(const t of c){const n=null==(e=s.byUid[t.id])?void 0:e.object;n&&(this.dispatch(L(a,t.pageIndex,t.id)),this.events$.emit(a,{type:"delete",documentId:a,annotation:n,pageIndex:t.pageIndex,committed:!1}))}this.dispatch(S(a)),this.dispatch(L(a,t,n)),this.events$.emit(a,{type:"delete",documentId:a,annotation:r,pageIndex:t,committed:!1})};if(!this.history)return l(),void(!1!==this.config.autoCommit&&this.commit(a));const u={execute:l,undo:()=>{this.dispatch(C(a,t,r)),this.events$.emit(a,{type:"create",documentId:a,annotation:r,pageIndex:t,committed:!1});for(const t of d)this.dispatch(C(a,t.pageIndex,t)),this.events$.emit(a,{type:"create",documentId:a,annotation:t,pageIndex:t.pageIndex,committed:!1})},metadata:{annotationIds:[n,...c.map(t=>t.id)]}};this.history.forDocument(a).register(u,this.ANNOTATION_HISTORY_TOPIC)}deleteAnnotationsMethod(t,e){for(const{pageIndex:n,id:o}of t)this.deleteAnnotation(n,o,e)}deleteAllAnnotationsMethod(t){const e=t??this.getActiveDocumentId(),n=this.getDocumentState(e),o=[];for(const[i,a]of Object.entries(n.pages))for(const t of a){const e=n.byUid[t];e&&o.push({pageIndex:Number(i),id:e.object.id})}o.length>0&&this.deleteAnnotationsMethod(o,e)}purgeAnnotationMethod(t,e,n){const o=n??this.getActiveDocumentId();this.dispatch(j(o,t,e))}selectAnnotation(t,e,n){const o=n??this.getActiveDocumentId(),i=this.getAnnotationById(e,o);if(!i||!this.isAnnotationLocked(i.object,o))if(this.isInGroupMethod(e,o)){const t=this.getGroupMembersMethod(e,o).map(t=>t.object.id);this.dispatch(M(o,t))}else this.dispatch(((t,e,n)=>({type:r,payload:{documentId:t,pageIndex:e,id:n}}))(o,t,e))}deselectAnnotation(t){const e=t??this.getActiveDocumentId();this.dispatch(S(e))}updateAnnotationSelectionActivity(t,e){var n,o;if(0===e.selectedUids.length)return void(null==(n=this.interactionManager)||n.releasePageActivity(t,"annotation-selection"));const i=e.selectedUids[0],a=e.byUid[i];a&&(null==(o=this.interactionManager)||o.claimPageActivity(t,"annotation-selection",a.object.pageIndex))}getSelectedAnnotationsMethod(t){return st(this.getDocumentState(t))}getSelectedAnnotationIdsMethod(t){return rt(this.getDocumentState(t))}toggleSelectionMethod(t,e,n){const o=n??this.getActiveDocumentId(),i=this.getAnnotationById(e,o);if(i&&this.isAnnotationLocked(i.object,o))return;const a=this.getDocumentState(o);if(a.selectedUids.includes(e))this.dispatch(R(o,e));else if(this.isInGroupMethod(e,o)){const t=this.getGroupMembersMethod(e,o);for(const e of t)a.selectedUids.includes(e.object.id)||this.dispatch(T(o,e.object.pageIndex,e.object.id))}else this.dispatch(T(o,t,e))}addToSelectionMethod(t,e,n){const o=n??this.getActiveDocumentId(),i=this.getAnnotationById(e,o);i&&this.isAnnotationLocked(i.object,o)||this.dispatch(T(o,t,e))}removeFromSelectionMethod(t,e){const n=e??this.getActiveDocumentId();this.dispatch(R(n,t))}setSelectionMethod(t,e){const n=e??this.getActiveDocumentId(),o=this.getDocumentState(n),i=t.filter(t=>{const e=o.byUid[t];return!e||!this.isAnnotationLocked(e.object,n)});this.dispatch(M(n,i))}getAttachedLinksMethod(t,e){return ht(this.getDocumentState(e),t)}hasAttachedLinksMethod(t,e){return this.getAttachedLinksMethod(t,e).length>0}deleteAttachedLinksMethod(t,e){const n=this.getAttachedLinksMethod(t,e);for(const o of n)this.deleteAnnotation(o.object.pageIndex,o.object.id,e)}groupAnnotationsMethod(t){const n=t??this.getActiveDocumentId();if(!this.checkPermission(n,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","GroupAnnotations",`Cannot group annotations: document ${n} lacks ModifyAnnotations permission`);const o=this.getSelectedAnnotationsMethod(n);if(o.length<2)return void this.logger.debug("AnnotationPlugin","GroupAnnotations","Need at least 2 annotations to group");const i=o[0],a=o.slice(1).map(t=>({pageIndex:t.object.pageIndex,id:t.object.id,patch:{inReplyToId:i.object.id,replyType:e.PdfAnnotationReplyType.Group}}));this.updateAnnotationsMethod(a,n)}ungroupAnnotationsMethod(t,n){const o=n??this.getActiveDocumentId();if(!this.checkPermission(o,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","UngroupAnnotations",`Cannot ungroup annotations: document ${o} lacks ModifyAnnotations permission`);const i=this.getGroupMembersMethod(t,o).filter(t=>t.object.inReplyToId&&t.object.replyType===e.PdfAnnotationReplyType.Group).map(t=>({pageIndex:t.object.pageIndex,id:t.object.id,patch:{inReplyToId:void 0,replyType:void 0}}));i.length>0&&this.updateAnnotationsMethod(i,o)}getGroupMembersMethod(t,e){return gt(this.getDocumentState(e),t)}isInGroupMethod(t,e){return ft(this.getDocumentState(e),t)}getGroupingActionMethod(t){return mt(this.getDocumentState(t))}computeCombinedConstraints(t){let e=1/0,n=1/0,o=1/0,i=1/0;for(const a of t){const t=a.rect.origin.y,s=a.pageSize.height-(a.rect.origin.y+a.rect.size.height),r=a.rect.origin.x,c=a.pageSize.width-(a.rect.origin.x+a.rect.size.width);e=Math.min(e,t),n=Math.min(n,s),o=Math.min(o,r),i=Math.min(i,c)}return isFinite(e)||(e=0),isFinite(n)||(n=0),isFinite(o)||(o=0),isFinite(i)||(i=0),{maxUp:e,maxDown:n,maxLeft:o,maxRight:i}}clampDelta(t,e){return{x:Math.max(-e.maxLeft,Math.min(e.maxRight,t.x)),y:Math.max(-e.maxUp,Math.min(e.maxDown,t.y))}}startDrag(t,e){const{annotationIds:n,pageSize:o}=e,i=[];for(const d of n){const e=this.getAttachedLinksMethod(d,t);for(const t of e)i.includes(t.object.id)||i.push(t.object.id)}const a=[...n,...i],s=new Map,r=[];for(const d of a){const e=this.getAnnotationById(d,t);e&&(s.set(d,{...e.object.rect}),r.push({id:d,rect:e.object.rect,pageIndex:e.object.pageIndex,pageSize:o}))}const c={documentId:t,isDragging:!0,primaryIds:n,attachedLinkIds:i,allParticipantIds:a,originalRects:s,delta:{x:0,y:0},combinedConstraints:this.computeCombinedConstraints(r)};this.unifiedDragStates.set(t,c),this.unifiedDrag$.emit({documentId:t,type:"start",state:c,previewPatches:{}})}computeDragPreviewPatches(t,e){const n={};for(const o of t.allParticipantIds){const i=this.getAnnotationById(o,e);if(!i)continue;const a=t.originalRects.get(o);if(!a)continue;const s={...a,origin:{x:a.origin.x+t.delta.x,y:a.origin.y+t.delta.y}};n[o]=this.transformAnnotation(i.object,{type:"move",changes:{rect:s}})}return n}updateDrag(t,e){const n=this.unifiedDragStates.get(t);if(!(null==n?void 0:n.isDragging))return{x:0,y:0};const o=this.clampDelta(e,n.combinedConstraints),i={...n,delta:o};this.unifiedDragStates.set(t,i);const a=this.computeDragPreviewPatches(i,t);return this.unifiedDrag$.emit({documentId:t,type:"update",state:i,previewPatches:a}),o}commitDrag(t){const e=this.unifiedDragStates.get(t);if(!e)return;const n=e.delta;if(0!==n.x||0!==n.y){const o=[];for(const i of e.allParticipantIds){const a=this.getAnnotationById(i,t);if(!a)continue;const s=e.originalRects.get(i)??a.object.rect,r={...s,origin:{x:s.origin.x+n.x,y:s.origin.y+n.y}},c=this.transformAnnotation(a.object,{type:"move",changes:{rect:r}});o.push({pageIndex:a.object.pageIndex,id:i,patch:c})}o.length>0&&this.moveAnnotationsMethod(o,t)}const o=this.computeDragPreviewPatches(e,t);this.unifiedDrag$.emit({documentId:t,type:"end",state:{...e,isDragging:!1},previewPatches:o}),this.unifiedDragStates.delete(t)}cancelDrag(t){const e=this.unifiedDragStates.get(t);e&&(this.unifiedDrag$.emit({documentId:t,type:"cancel",state:{...e,isDragging:!1,delta:{x:0,y:0}},previewPatches:{}}),this.unifiedDragStates.delete(t))}getDragState(t){return this.unifiedDragStates.get(t)??null}get onDragChange(){return this.unifiedDrag$.on}computeUnifiedGroupBoundingBox(t){if(0===t.length)return{origin:{x:0,y:0},size:{width:0,height:0}};let e=1/0,n=1/0,o=-1/0,i=-1/0;for(const a of t)e=Math.min(e,a.origin.x),n=Math.min(n,a.origin.y),o=Math.max(o,a.origin.x+a.size.width),i=Math.max(i,a.origin.y+a.size.height);return{origin:{x:e,y:n},size:{width:o-e,height:i-n}}}computeUnifiedRelativePositions(t,e){return t.map(t=>({id:t.id,originalRect:t.rect,originalUnrotatedRect:t.unrotatedRect,pageIndex:t.pageIndex,isAttachedLink:t.isAttachedLink,parentId:t.parentId,relativeX:e.size.width>0?(t.rect.origin.x-e.origin.x)/e.size.width:0,relativeY:e.size.height>0?(t.rect.origin.y-e.origin.y)/e.size.height:0,relativeWidth:e.size.width>0?t.rect.size.width/e.size.width:1,relativeHeight:e.size.height>0?t.rect.size.height/e.size.height:1}))}computeUnifiedResizedRects(t,e,n=10){const o=new Map;for(const i of t){const t=Math.max(n,i.relativeWidth*e.size.width),a=Math.max(n,i.relativeHeight*e.size.height);o.set(i.id,{origin:{x:e.origin.x+i.relativeX*e.size.width,y:e.origin.y+i.relativeY*e.size.height},size:{width:t,height:a}})}return o}computeResizePreviewPatches(t,e){const n={},o=this.unifiedResizeStates.get(e),i=o?new Map(o.participatingAnnotations.map(t=>[t.id,t])):void 0;for(const[a,s]of t){const t=this.getAnnotationById(a,e);if(!t)continue;let r=s;const c=null==i?void 0:i.get(a);(null==o?void 0:o.isGroupResize)&&(null==c?void 0:c.originalUnrotatedRect)&&(r=Ht(s,c.originalRect,c.originalUnrotatedRect,t.object.rotation??0)),n[a]=this.transformAnnotation(t.object,{type:"resize",changes:{rect:r}})}return n}startResize(t,e){const{annotationIds:n,pageSize:o,resizeHandle:i}=e,a=[],s=[];for(const g of n){const e=this.getAnnotationById(g,t);if(e){s.push({id:g,rect:e.object.rect,unrotatedRect:e.object.unrotatedRect?this.cloneRect(e.object.unrotatedRect):void 0,pageIndex:e.object.pageIndex,isAttachedLink:!1});const n=this.getAttachedLinksMethod(g,t);for(const t of n)a.includes(t.object.id)||(a.push(t.object.id),s.push({id:t.object.id,rect:t.object.rect,unrotatedRect:t.object.unrotatedRect?this.cloneRect(t.object.unrotatedRect):void 0,pageIndex:t.object.pageIndex,isAttachedLink:!0,parentId:g}))}}const r=[...n,...a],c=s.map(t=>t.rect),d=this.computeUnifiedGroupBoundingBox(c),l=this.computeUnifiedRelativePositions(s,d),u=this.computeUnifiedResizedRects(l,d),h={documentId:t,isResizing:!0,isGroupResize:n.length>1,primaryIds:n,attachedLinkIds:a,allParticipantIds:r,originalGroupBox:d,currentGroupBox:d,participatingAnnotations:l,resizeHandle:i,computedRects:u};this.unifiedResizeStates.set(t,h);const p=this.computeResizePreviewPatches(u,t);this.unifiedResize$.emit({documentId:t,type:"start",state:h,computedRects:Object.fromEntries(u),previewPatches:p})}updateResize(t,e){const n=this.unifiedResizeStates.get(t);if(!(null==n?void 0:n.isResizing))return{};const o=this.computeUnifiedResizedRects(n.participatingAnnotations,e),i={...n,currentGroupBox:e,computedRects:o};this.unifiedResizeStates.set(t,i);const a=Object.fromEntries(o),s=this.computeResizePreviewPatches(o,t);return this.unifiedResize$.emit({documentId:t,type:"update",state:i,computedRects:a,previewPatches:s}),a}commitResize(t){const e=this.unifiedResizeStates.get(t);if(!e)return;const n=this.computeUnifiedResizedRects(e.participatingAnnotations,e.currentGroupBox),o=[],i=new Map(e.participatingAnnotations.map(t=>[t.id,t]));for(const[s,r]of n){const n=this.getAnnotationById(s,t);if(!n)continue;let a=r;const c=i.get(s);e.isGroupResize&&(null==c?void 0:c.originalUnrotatedRect)&&(a=Ht(r,c.originalRect,c.originalUnrotatedRect,n.object.rotation??0));const d=this.transformAnnotation(n.object,{type:"resize",changes:{rect:a}});o.push({pageIndex:n.object.pageIndex,id:s,patch:d})}o.length>0&&this.updateAnnotationsMethod(o,t);const a=this.computeResizePreviewPatches(n,t);this.unifiedResize$.emit({documentId:t,type:"end",state:{...e,isResizing:!1},computedRects:Object.fromEntries(n),previewPatches:a}),this.unifiedResizeStates.delete(t)}cancelResize(t){const e=this.unifiedResizeStates.get(t);if(!e)return;const n=this.computeUnifiedResizedRects(e.participatingAnnotations,e.originalGroupBox);this.unifiedResize$.emit({documentId:t,type:"cancel",state:{...e,isResizing:!1,currentGroupBox:e.originalGroupBox},computedRects:Object.fromEntries(n),previewPatches:{}}),this.unifiedResizeStates.delete(t)}getResizeState(t){return this.unifiedResizeStates.get(t)??null}get onResizeChange(){return this.unifiedResize$.on}cloneRect(t){return{origin:{x:t.origin.x,y:t.origin.y},size:{width:t.size.width,height:t.size.height}}}translateRect(t,e){return{origin:{x:t.origin.x+e.x,y:t.origin.y+e.y},size:{...t.size}}}normalizeAngle(t){const e=t%360;return e<0?e+360:e}normalizeDelta(t){return(t+540)%360-180}buildRotationParticipants(t,e){const n=[],o=[];for(const i of t){const t=this.getAnnotationById(i,e);if(!t)continue;n.push({id:i,rect:this.cloneRect(t.object.rect),pageIndex:t.object.pageIndex,rotation:t.object.rotation??0,unrotatedRect:t.object.unrotatedRect?this.cloneRect(t.object.unrotatedRect):void 0,isAttachedLink:!1});const a=this.getAttachedLinksMethod(i,e);for(const e of a)o.includes(e.object.id)||(o.push(e.object.id),n.push({id:e.object.id,rect:this.cloneRect(e.object.rect),pageIndex:e.object.pageIndex,rotation:e.object.rotation??0,unrotatedRect:e.object.unrotatedRect?this.cloneRect(e.object.unrotatedRect):void 0,isAttachedLink:!0,parentId:i}))}return{participants:n,attachedLinkIds:o}}computeRotatePreviewPatches(t,n){const o={};for(const i of t.participants){const a=this.getAnnotationById(i.id,n);if(!a)continue;const s=Rt({rect:i.rect,unrotatedRect:i.unrotatedRect,rotation:i.rotation}),r=e.rotatePointAround(s,t.rotationCenter,t.delta),c={x:r.x-s.x,y:r.y-s.y},d=this.normalizeAngle(i.rotation+t.delta),l=this.transformAnnotation(a.object,{type:"rotate",changes:{rotation:d,unrotatedRect:this.translateRect(i.unrotatedRect??i.rect,c)},metadata:{rotationAngle:d,rotationDelta:t.delta,rotationCenter:t.rotationCenter}});l.rect||0===c.x&&0===c.y||(l.rect={origin:{x:a.object.rect.origin.x+c.x,y:a.object.rect.origin.y+c.y},size:{...a.object.rect.size}}),o[i.id]=l}return o}startRotation(t,e){const{annotationIds:n,cursorAngle:o,rotationCenter:i}=e,{participants:a,attachedLinkIds:s}=this.buildRotationParticipants(n,t);if(0===a.length)return;const r=a.map(t=>t.rect),c=this.computeUnifiedGroupBoundingBox(r),d=i??{x:c.origin.x+c.size.width/2,y:c.origin.y+c.size.height/2},l={documentId:t,isRotating:!0,primaryIds:n,attachedLinkIds:s,allParticipantIds:a.map(t=>t.id),rotationCenter:d,cursorStartAngle:o,currentAngle:o,delta:0,participants:a};this.unifiedRotateStates.set(t,l);const u=this.computeRotatePreviewPatches(l,t);this.unifiedRotate$.emit({documentId:t,type:"start",state:l,previewPatches:u})}updateRotation(t,e,n){const o=this.unifiedRotateStates.get(t);if(!(null==o?void 0:o.isRotating))return;const i=void 0!==n?n:this.normalizeDelta(e-o.cursorStartAngle),a={...o,currentAngle:e,delta:i};this.unifiedRotateStates.set(t,a);const s=this.computeRotatePreviewPatches(a,t);this.unifiedRotate$.emit({documentId:t,type:"update",state:a,previewPatches:s})}commitRotation(t){const e=this.unifiedRotateStates.get(t);if(!e)return;const n=this.computeRotatePreviewPatches(e,t),o=[];for(const[i,a]of Object.entries(n)){const e=this.getAnnotationById(i,t);e&&o.push({pageIndex:e.object.pageIndex,id:i,patch:a})}o.length>0&&this.updateAnnotationsMethod(o,t),this.unifiedRotate$.emit({documentId:t,type:"end",state:{...e,isRotating:!1},previewPatches:n}),this.unifiedRotateStates.delete(t)}cancelRotation(t){const e=this.unifiedRotateStates.get(t);e&&(this.unifiedRotate$.emit({documentId:t,type:"cancel",state:{...e,isRotating:!1,delta:0,currentAngle:e.cursorStartAngle},previewPatches:{}}),this.unifiedRotateStates.delete(t))}getRotateState(t){return this.unifiedRotateStates.get(t)??null}get onRotateChange(){return this.unifiedRotate$.on}updateAnnotationsMethod(t,n){const o=n??this.getActiveDocumentId();if(!this.checkPermission(o,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","UpdateAnnotations",`Cannot update annotations: document ${o} lacks ModifyAnnotations permission`);const i=this.getDocumentState(o),a=t.map(({pageIndex:t,id:e,patch:n})=>{var o;const a=null==(o=i.byUid[e])?void 0:o.object;if(!a)return null;return{pageIndex:t,id:e,patch:this.buildPatch(a,{...n,author:n.author??this.config.annotationAuthor}),originalObject:a}}).filter(t=>null!==t);if(0===a.length)return;const s=()=>{for(const{pageIndex:t,id:e,patch:n,originalObject:i}of a)this.dispatch(D(o,t,e,n)),this.events$.emit(o,{type:"update",documentId:o,annotation:i,pageIndex:t,patch:n,committed:!1})};if(!this.history)return s(),void(!1!==this.config.autoCommit&&this.commit(o));const r=a.map(({pageIndex:t,id:e,patch:n,originalObject:o})=>({pageIndex:t,id:e,originalPatch:Object.fromEntries(Object.keys(n).map(t=>[t,o[t]])),originalObject:o})),c={execute:s,undo:()=>{for(const{pageIndex:t,id:e,originalPatch:n,originalObject:i}of r)this.dispatch(D(o,t,e,n)),this.events$.emit(o,{type:"update",documentId:o,annotation:i,pageIndex:t,patch:n,committed:!1})},metadata:{annotationIds:a.map(t=>t.id)}};this.history.forDocument(o).register(c,this.ANNOTATION_HISTORY_TOPIC)}moveAnnotationsMethod(t,n){const o=n??this.getActiveDocumentId();if(!this.checkPermission(o,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","MoveAnnotations",`Cannot move annotations: document ${o} lacks ModifyAnnotations permission`);const i=this.getDocumentState(o),a=t.map(({pageIndex:t,id:e,patch:n})=>{var o;const a=null==(o=i.byUid[e])?void 0:o.object;return a?{pageIndex:t,id:e,patch:n,originalObject:a}:null}).filter(t=>null!==t);if(0===a.length)return;const s=()=>{for(const{pageIndex:t,id:e,patch:n,originalObject:i}of a)this.dispatch(E(o,t,e,n)),this.events$.emit(o,{type:"update",documentId:o,annotation:i,pageIndex:t,patch:n,committed:!1})};if(!this.history)return s(),void(!1!==this.config.autoCommit&&this.commit(o));const r=a.map(({pageIndex:t,id:e,patch:n,originalObject:o})=>({pageIndex:t,id:e,originalPatch:Object.fromEntries(Object.keys(n).map(t=>[t,o[t]])),originalObject:o})),c={execute:s,undo:()=>{for(const{pageIndex:t,id:e,originalPatch:n,originalObject:i}of r)this.dispatch(E(o,t,e,n)),this.events$.emit(o,{type:"update",documentId:o,annotation:i,pageIndex:t,patch:n,committed:!1})},metadata:{annotationIds:a.map(t=>t.id)}};this.history.forDocument(o).register(c,this.ANNOTATION_HISTORY_TOPIC)}moveAnnotationMethod(t,e,n,o="delta",i){const a=i??this.getActiveDocumentId(),s=this.getAnnotationById(e,a);if(!s)return;const r=s.object.rect,c={...r,origin:"absolute"===o?{x:n.x,y:n.y}:{x:r.origin.x+n.x,y:r.origin.y+n.y}},d=this.transformAnnotation(s.object,{type:"move",changes:{rect:c}});this.moveAnnotationsMethod([{pageIndex:t,id:e,patch:d}],a)}getActiveTool(t){const e=this.getDocumentState(t);return e.activeToolId?this.state.tools.find(t=>t.id===e.activeToolId)??null:null}setActiveTool(t,n,o){var i,a;const s=n??this.getActiveDocumentId();if(null!==t&&!this.checkPermission(s,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","SetActiveTool",`Cannot activate tool: document ${s} lacks ModifyAnnotations permission`);const r=this.getDocumentState(s);if(t===r.activeToolId&&!o)return;this.dispatch(k(s,t,o));const c=this.state.tools.find(e=>e.id===t);c?null==(i=this.interactionManager)||i.forDocument(s).activate(c.interaction.mode??c.id):null==(a=this.interactionManager)||a.forDocument(s).activateDefaultMode()}getTool(t){return this.state.tools.find(e=>e.id===t)}findToolForAnnotation(t){let e=null,n=0;for(const o of this.state.tools){const i=o.matchScore(t);i>n&&(n=i,e=o)}return e}setLocked(t,e){const n=e??this.getActiveDocumentId();this.dispatch(((t,e)=>({type:x,payload:{documentId:t,mode:e}}))(n,t));const o=this.getDocumentState(n),i=o.selectedUids.filter(t=>{const e=o.byUid[t];return!!e&&this.isAnnotationLocked(e.object,n)});if(i.length>0){const t=o.selectedUids.filter(t=>!i.includes(t));this.dispatch(M(n,t))}}getLocked(t){return this.getDocumentState(t).locked}isAnnotationLocked(t,e){if(ot(t))return!0;return nt(et(this.findToolForAnnotation(t)),this.getLocked(e))}isCategoryLockedMethod(t,e){return nt([t],this.getLocked(e))}isToolLockedMethod(t,e){const n=this.getTool(t);if(!n)return!1;return nt(et(n),this.getLocked(e))}syncAnnotationObject(t,e,n){const o=n??this.getActiveDocumentId();this.dispatch(((t,e,n)=>({type:b,payload:{documentId:t,id:e,patch:n}}))(o,t,e))}navigateTargetMethod(t,n){var o;const i=n??this.getActiveDocumentId(),a=n=>(this.navigate$.emit(i,{documentId:i,result:n,target:t}),e.PdfTaskHelper.resolve(n));let s;if("destination"===t.type)s=t.destination;else if("action"===t.type){const n=t.action;if(n.type!==e.PdfActionType.Goto&&n.type!==e.PdfActionType.RemoteGoto)return n.type===e.PdfActionType.URI?a({outcome:"uri",uri:n.uri}):a({outcome:"unsupported"});s=n.destination}if(!s)return a({outcome:"unsupported"});if(!this.scroll)return a({outcome:"destination",destination:s});const r=this.scroll.forDocument(i);if(s.zoom.mode===e.PdfZoomMode.XYZ){const t=this.getCoreDocument(i),e=null==(o=null==t?void 0:t.document)?void 0:o.pages.find(t=>t.index===s.pageIndex);r.scrollToPage({pageNumber:s.pageIndex+1,pageCoordinates:e&&s.zoom.params?{x:s.zoom.params.x,y:e.size.height-s.zoom.params.y}:void 0,behavior:"smooth"})}else r.scrollToPage({pageNumber:s.pageIndex+1,behavior:"smooth"});return a({outcome:"navigated"})}collectPendingChanges(t,e){const n=this.getDocumentState(t),o=this.pendingContexts.get(t),i={creations:[],updates:[],deletions:[],committedUids:[],isEmpty:!0};for(const[a,s]of Object.entries(n.byUid)){if("synced"===s.commitState)continue;if(e.pages.find(t=>t.index===s.object.pageIndex))switch(i.committedUids.push(a),i.isEmpty=!1,s.commitState){case"new":i.creations.push({uid:a,ta:s,ctx:null==o?void 0:o.get(s.object.id)});break;case"moved":i.updates.push({uid:a,ta:s,moved:!0});break;case"dirty":i.updates.push({uid:a,ta:s});break;case"deleted":i.deletions.push({uid:a,ta:s})}}return i}executeCommitBatch(t,n,o){const i=new e.Task,a=this.pendingContexts.get(t),s=[];for(const{uid:e,ta:c,ctx:d}of o.creations){const t=n.pages.find(t=>t.index===c.object.pageIndex);if(!t)continue;const o=this.engine.createPageAnnotation(n,t,c.object,d);s.push({type:"create",task:o,ta:c,uid:e,ctx:d})}for(const{uid:e,ta:c,moved:d}of o.updates){const t=n.pages.find(t=>t.index===c.object.pageIndex);if(!t)continue;const o=d?this.engine.updatePageAnnotation(n,t,c.object,{regenerateAppearance:!1}):this.engine.updatePageAnnotation(n,t,c.object);s.push({type:"update",task:o,ta:c,uid:e,moved:d})}for(const{uid:c,ta:d}of o.deletions){const o=n.pages.find(t=>t.index===d.object.pageIndex);if(o)if(d.object.id){const t=new e.Task;this.engine.removePageAnnotation(n,o,d.object).wait(()=>t.resolve(!0),t.fail),s.push({type:"delete",task:t,ta:d,uid:c})}else this.dispatch(j(t,d.object.pageIndex,c))}const r=s.map(t=>t.task);return e.Task.allSettled(r).wait(()=>{this.emitCommitEvents(t,s,a);for(const o of s)"update"===o.type&&o.moved||"create"!==o.type&&"update"!==o.type&&"delete"!==o.type||this.invalidateAnnotationAppearance(o.ta.object.id,o.ta.object.pageIndex,t);var e,n;this.dispatch((e=t,n=o.committedUids,{type:y,payload:{documentId:e,committedUids:n}})),i.resolve(!0)},t=>i.fail(t)),i}emitCommitEvents(t,n,o){for(const i of n)if(i.task.state.stage===e.TaskStage.Resolved)switch(i.type){case"create":this.events$.emit(t,{type:"create",documentId:t,annotation:i.ta.object,pageIndex:i.ta.object.pageIndex,ctx:i.ctx,committed:!0}),null==o||o.delete(i.ta.object.id);break;case"update":this.events$.emit(t,{type:"update",documentId:t,annotation:i.ta.object,pageIndex:i.ta.object.pageIndex,patch:i.ta.object,committed:!0});break;case"delete":this.dispatch(j(t,i.ta.object.pageIndex,i.uid)),this.events$.emit(t,{type:"delete",documentId:t,annotation:i.ta.object,pageIndex:i.ta.object.pageIndex,committed:!0})}}acquireCommitLock(t){return!this.commitInProgress.get(t)&&(this.commitInProgress.set(t,!0),!0)}releaseCommitLock(t){this.commitInProgress.set(t,!1)}commit(t){const n=t??this.getActiveDocumentId();if(!this.getDocumentState(n).hasPendingChanges)return e.PdfTaskHelper.resolve(!0);if(!this.acquireCommitLock(n))return e.PdfTaskHelper.resolve(!0);const o=this.getCoreDocument(n),i=null==o?void 0:o.document;if(!i)return this.releaseCommitLock(n),e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Document not found"});const a=this.collectPendingChanges(n,i);if(a.isEmpty)return this.releaseCommitLock(n),e.PdfTaskHelper.resolve(!0);const s=new e.Task;return this.executeCommitBatch(n,i,a).wait(()=>{this.releaseCommitLock(n);this.getDocumentState(n).hasPendingChanges?this.commit(n).wait(t=>s.resolve(t),t=>s.fail(t)):s.resolve(!0)},t=>{this.releaseCommitLock(n),s.fail(t)}),s}};ze.id="annotation",ze.defaultHandlerFactories=new Map([[e.PdfAnnotationSubtype.CIRCLE,xe],[e.PdfAnnotationSubtype.SQUARE,me],[e.PdfAnnotationSubtype.STAMP,Ae],[e.PdfAnnotationSubtype.POLYGON,fe],[e.PdfAnnotationSubtype.POLYLINE,Wt],[e.PdfAnnotationSubtype.LINE,Ft],[e.PdfAnnotationSubtype.INK,At],[e.PdfAnnotationSubtype.FREETEXT,Gt],[e.PdfAnnotationSubtype.TEXT,Bt],[e.PdfAnnotationSubtype.LINK,be]]);let Ne=ze;function Oe(t){return e=>(null==e?void 0:e.id)===t}const Ue=Oe("highlight"),Be=Oe("squiggly"),$e=Oe("underline"),Ge=Oe("strikeout"),Fe=Oe("ink"),We=Oe("inkHighlighter"),He=Oe("square"),_e=Oe("circle"),Ke=Oe("line"),Ye=Oe("polyline"),Ve=Oe("polygon"),Xe=Oe("freeText"),qe=Oe("stamp"),Qe=Oe("insertText"),Ze=Oe("replaceText"),Je={manifest:o,create:(t,e)=>new Ne(n,t,e),reducer:(t,e)=>{switch(e.type){case i:{const{documentId:n,state:o}=e.payload;return{...t,documents:{...t.documents,[n]:o},activeDocumentId:t.activeDocumentId??n}}case a:{const n=e.payload,{[n]:o,...i}=t.documents;return{...t,documents:i,activeDocumentId:t.activeDocumentId===n?null:t.activeDocumentId}}case"ANNOTATION/SET_ACTIVE_DOCUMENT":return{...t,activeDocumentId:e.payload};case s:{const{documentId:n,annotations:o}=e.payload,i=t.documents[n];if(!i)return t;const a={},s={};for(const[t,e]of Object.entries(o)){const n=Number(t),o=i.pages[n]||[];for(const t of o)delete s[t];const r=e.map(t=>{const e=t.id;return s[e]={commitState:"synced",object:t},e});a[n]=r}return{...t,documents:{...t.documents,[n]:{...i,pages:a,byUid:s}}}}case r:{const{documentId:n,id:o}=e.payload,i=t.documents[n];return i?{...t,documents:{...t.documents,[n]:{...i,selectedUids:[o],selectedUid:o}}}:t}case c:{const{documentId:n}=e.payload,o=t.documents[n];return o?{...t,documents:{...t.documents,[n]:{...o,selectedUids:[],selectedUid:null}}}:t}case d:{const{documentId:n,id:o}=e.payload,i=t.documents[n];if(!i)return t;if(i.selectedUids.includes(o))return t;const a=[...i.selectedUids,o];return{...t,documents:{...t.documents,[n]:{...i,selectedUids:a,selectedUid:De(a)}}}}case l:{const{documentId:n,id:o}=e.payload,i=t.documents[n];if(!i)return t;const a=i.selectedUids.filter(t=>t!==o);return{...t,documents:{...t.documents,[n]:{...i,selectedUids:a,selectedUid:De(a)}}}}case u:{const{documentId:n,ids:o}=e.payload,i=t.documents[n];return i?{...t,documents:{...t.documents,[n]:{...i,selectedUids:o,selectedUid:De(o)}}}:t}case h:{const{documentId:n,toolId:o,context:i}=e.payload,a=t.documents[n];return a?{...t,documents:{...t.documents,[n]:{...a,activeToolId:o,activeToolContext:i}}}:t}case p:{const{documentId:n,pageIndex:o,annotation:i}=e.payload,a=t.documents[n];if(!a)return t;const s=i.id;return{...t,documents:{...t.documents,[n]:{...a,pages:{...a.pages,[o]:[...a.pages[o]??[],s]},byUid:{...a.byUid,[s]:{commitState:"new",object:i,dictMode:!0}},hasPendingChanges:!0}}}}case m:{const{documentId:n,pageIndex:o,id:i}=e.payload,a=t.documents[n];return a&&a.byUid[i]?{...t,documents:{...t.documents,[n]:{...a,pages:{...a.pages,[o]:(a.pages[o]??[]).filter(t=>t!==i)},byUid:{...a.byUid,[i]:{...a.byUid[i],commitState:"deleted"}},hasPendingChanges:!0}}}:t}case g:{const{documentId:n,id:o,patch:i}=e.payload,a=t.documents[n];return a?{...t,documents:{...t.documents,[n]:Le(a,o,i)}}:t}case f:{const{documentId:n,id:o,patch:i}=e.payload,a=t.documents[n];return a?{...t,documents:{...t.documents,[n]:je(a,o,i)}}:t}case y:{const{documentId:n,committedUids:o}=e.payload,i=t.documents[n];if(!i)return t;const a=new Set(o),s={};let r=!1;for(const[t,e]of Object.entries(i.byUid))a.has(t)?s[t]={...e,commitState:"dirty"===e.commitState||"new"===e.commitState||"moved"===e.commitState?"synced":e.commitState}:(s[t]=e,"new"!==e.commitState&&"dirty"!==e.commitState&&"moved"!==e.commitState&&"deleted"!==e.commitState||(r=!0));return{...t,documents:{...t.documents,[n]:{...i,byUid:s,hasPendingChanges:r}}}}case A:{const{documentId:n,pageIndex:o,uid:i}=e.payload,a=t.documents[n];if(!a||!a.byUid[i])return t;const{[i]:s,...r}=a.byUid;return{...t,documents:{...t.documents,[n]:{...a,pages:{...a.pages,[o]:(a.pages[o]??[]).filter(t=>t!==i)},byUid:r}}}}case x:{const{documentId:n,mode:o}=e.payload,i=t.documents[n];return i?{...t,documents:{...t.documents,[n]:{...i,locked:o}}}:t}case b:{const{documentId:n,id:o,patch:i}=e.payload,a=t.documents[n];if(!a)return t;const s=a.byUid[o];return s?{...t,documents:{...t.documents,[n]:{...a,byUid:{...a.byUid,[o]:{...s,object:{...s.object,...i}}}}}}:t}case P:{const n=new Map(t.tools.map(t=>[t.id,t]));return n.set(e.payload.id,e.payload),{...t,tools:Array.from(n.values())}}case I:{const{toolId:n,patch:o}=e.payload;return{...t,tools:t.tools.map(t=>t.id===n?{...t,defaults:{...t.defaults,...o}}:t)}}case v:return t.colorPresets.includes(e.payload)?t:{...t,colorPresets:[...t.colorPresets,e.payload]};default:return t}},initialState:(t,e)=>we(e)};Object.defineProperty(exports,"calculateRotatedRectAABB",{enumerable:!0,get:()=>e.calculateRotatedRectAABB}),Object.defineProperty(exports,"calculateRotatedRectAABBAroundPoint",{enumerable:!0,get:()=>e.calculateRotatedRectAABBAroundPoint}),Object.defineProperty(exports,"getRectCenter",{enumerable:!0,get:()=>e.getRectCenter}),Object.defineProperty(exports,"inferRotationCenterFromRects",{enumerable:!0,get:()=>e.inferRotationCenterFromRects}),Object.defineProperty(exports,"rotatePointAroundCenter",{enumerable:!0,get:()=>e.rotatePointAround}),Object.defineProperty(exports,"rotateVertices",{enumerable:!0,get:()=>e.rotateVertices}),exports.ANNOTATION_PLUGIN_ID=n,exports.AnnotationPlugin=Ne,exports.AnnotationPluginPackage=Je,exports.LockModeType=w,exports.applyInsertUpright=Nt,exports.clampAnnotationToPage=Ot,exports.convertAABBRectToUnrotatedSpace=Ht,exports.createToolPredicate=Oe,exports.defineAnnotationTool=t=>t,exports.generateCloudyEllipsePath=function(t,e,n,o){const i=new qt;let a=0,s=0,r=t.width,c=t.height;return e&&(a+=e.left,s+=e.top,r-=e.right,c-=e.bottom),he(a,-c,r,-s,n,o,i),i.close(),i.build(o)},exports.generateCloudyPolygonPath=function(t,e,n,o){const i=new qt;if(t.length<3)return i.build(o);const a=t.map(t=>({x:t.x-e.x,y:-(t.y-e.y)})),s=a[0],r=a[a.length-1];return s.x===r.x&&s.y===r.y||a.push({x:s.x,y:s.y}),de(a,!1,n,o,i),i.close(),i.build(o)},exports.generateCloudyRectanglePath=function(t,e,n,o){const i=new qt;let a=0,s=0,r=t.width,c=t.height;return e?(a+=e.left,s+=e.top,r-=e.right,c-=e.bottom):(a+=o/2,s+=o/2,r-=o/2,c-=o/2),de([{x:a,y:-s},{x:r,y:-s},{x:r,y:-c},{x:a,y:-c},{x:a,y:-s}],!1,n,o,i),i.close(),i.build(o)},exports.getAnnotationByUid=ct,exports.getAnnotationCategories=et,exports.getAnnotations=t=>{const e={};for(const n of Object.keys(t.pages).map(Number))e[n]=it(t,n);return e},exports.getAnnotationsByPageIndex=it,exports.getAttachedLinks=ht,exports.getCloudyBorderExtent=ge,exports.getGroupLeaderId=pt,exports.getGroupMembers=gt,exports.getIRTChildIds=lt,exports.getIRTChildrenByType=ut,exports.getSelectedAnnotation=at,exports.getSelectedAnnotationByPageIndex=(t,e)=>{const n=t.pages[e]??[];for(const o of t.selectedUids)if(n.includes(o))return t.byUid[o]??null;return null},exports.getSelectedAnnotationIds=rt,exports.getSelectedAnnotations=st,exports.getSelectedAnnotationsByPageIndex=(t,e)=>{const n=new Set(t.pages[e]??[]);return t.selectedUids.filter(t=>n.has(t)).map(e=>t.byUid[e]).filter(t=>void 0!==t)},exports.getSelectionGroupingAction=mt,exports.getSidebarAnnotationsWithReplies=t=>{const e=dt(t),n=[],o=Object.keys(e).map(Number).sort((t,e)=>t-e);for(const i of o)n.push(...e[i]);return n},exports.getSidebarAnnotationsWithRepliesGroupedByPage=dt,exports.getToolDefaultsById=function(t,e){const n=t.tools.find(t=>t.id===e);return null==n?void 0:n.defaults},exports.hasAttachedLinks=(t,e)=>ht(t,e).length>0,exports.hasIRTChildren=(t,e)=>{for(const n of Object.values(t.pages))for(const o of n){const n=t.byUid[o];if(n&&"inReplyToId"in n.object&&n.object.inReplyToId===e)return!0}return!1},exports.hasLockedFlag=ot,exports.hasMultipleSelected=t=>t.selectedUids.length>1,exports.initialDocumentState=Ee,exports.initialState=we,exports.isAnnotationSelected=(t,e)=>t.selectedUids.includes(e),exports.isCaret=Q,exports.isCategoryLocked=nt,exports.isCircle=O,exports.isCircleTool=_e,exports.isFreeText=Y,exports.isFreeTextTool=Xe,exports.isHighlight=F,exports.isHighlightTool=Ue,exports.isInGroup=ft,exports.isInk=N,exports.isInkHighlighterTool=We,exports.isInkTool=Fe,exports.isInsertTextTool=Qe,exports.isLine=$,exports.isLineTool=Ke,exports.isLink=function(t){return t.object.type===e.PdfAnnotationSubtype.LINK},exports.isPolygon=U,exports.isPolygonTool=Ve,exports.isPolyline=G,exports.isPolylineTool=Ye,exports.isRedact=q,exports.isReplaceTextTool=Ze,exports.isSelectableAnnotation=tt,exports.isSidebarAnnotation=J,exports.isSquare=B,exports.isSquareTool=He,exports.isSquiggly=_,exports.isSquigglyTool=Be,exports.isStamp=V,exports.isStampTool=qe,exports.isStrikeout=H,exports.isStrikeoutTool=Ge,exports.isText=X,exports.isTextMarkup=K,exports.isUnderline=W,exports.isUnderlineTool=$e,exports.isWidget=Z,exports.manifest=o,exports.patching=Ut,exports.rectsIntersect=z,exports.resolveInteractionProp=function(t,e,n){return void 0===t?n:"function"==typeof t?t(e):t},exports.useClickDetector=$t,exports.useState=yt;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("@embedpdf/core"),e=require("@embedpdf/models"),n="annotation",o={id:n,name:"Annotation Plugin",version:"1.0.0",provides:["annotation"],requires:["interaction-manager","selection"],optional:["history","scroll"],defaultConfig:{autoCommit:!0,annotationAuthor:"Guest",deactivateToolAfterCreate:!1,selectAfterCreate:!0,autoOpenLinks:!0}},i="ANNOTATION/INIT_STATE",a="ANNOTATION/CLEANUP_STATE",s="ANNOTATION/SET_ANNOTATIONS",r="ANNOTATION/SELECT_ANNOTATION",c="ANNOTATION/DESELECT_ANNOTATION",d="ANNOTATION/ADD_TO_SELECTION",l="ANNOTATION/REMOVE_FROM_SELECTION",u="ANNOTATION/SET_SELECTION",h="ANNOTATION/SET_ACTIVE_TOOL_ID",p="ANNOTATION/CREATE_ANNOTATION",g="ANNOTATION/PATCH_ANNOTATION",f="ANNOTATION/MOVE_ANNOTATION",m="ANNOTATION/DELETE_ANNOTATION",y="ANNOTATION/COMMIT",A="ANNOTATION/PURGE_ANNOTATION",x="ANNOTATION/SET_LOCKED",b="ANNOTATION/SYNC_OBJECT",v="ANNOTATION/ADD_COLOR_PRESET",I="ANNOTATION/SET_TOOL_DEFAULTS",P="ANNOTATION/ADD_TOOL";const S=t=>({type:c,payload:{documentId:t}}),T=(t,e,n)=>({type:d,payload:{documentId:t,pageIndex:e,id:n}}),R=(t,e)=>({type:l,payload:{documentId:t,id:e}}),M=(t,e)=>({type:u,payload:{documentId:t,ids:e}}),k=(t,e,n)=>({type:h,payload:{documentId:t,toolId:e,context:n}}),C=(t,e,n)=>({type:p,payload:{documentId:t,pageIndex:e,annotation:n}}),D=(t,e,n,o)=>({type:g,payload:{documentId:t,pageIndex:e,id:n,patch:o}}),L=(t,e,n,o)=>({type:f,payload:{documentId:t,pageIndex:e,id:n,patch:o}}),E=(t,e,n)=>({type:m,payload:{documentId:t,pageIndex:e,id:n}}),j=(t,e,n)=>({type:A,payload:{documentId:t,pageIndex:e,uid:n}});var w=(t=>(t.None="none",t.All="all",t.Include="include",t.Exclude="exclude",t))(w||{});function z(t,e){return!(t.origin.x+t.size.width<e.origin.x||e.origin.x+e.size.width<t.origin.x||t.origin.y+t.size.height<e.origin.y||e.origin.y+e.size.height<t.origin.y)}function N(t){return t.object.type===e.PdfAnnotationSubtype.INK}function O(t){return t.object.type===e.PdfAnnotationSubtype.CIRCLE}function U(t){return t.object.type===e.PdfAnnotationSubtype.POLYGON}function B(t){return t.object.type===e.PdfAnnotationSubtype.SQUARE}function $(t){return t.object.type===e.PdfAnnotationSubtype.LINE}function G(t){return t.object.type===e.PdfAnnotationSubtype.POLYLINE}function F(t){return t.object.type===e.PdfAnnotationSubtype.HIGHLIGHT}function W(t){return t.object.type===e.PdfAnnotationSubtype.UNDERLINE}function H(t){return t.object.type===e.PdfAnnotationSubtype.STRIKEOUT}function _(t){return t.object.type===e.PdfAnnotationSubtype.SQUIGGLY}function K(t){return F(t)||W(t)||H(t)||_(t)}function Y(t){return t.object.type===e.PdfAnnotationSubtype.FREETEXT}function V(t){return t.object.type===e.PdfAnnotationSubtype.STAMP}function X(t){return t.object.type===e.PdfAnnotationSubtype.TEXT}function q(t){return t.object.type===e.PdfAnnotationSubtype.REDACT}function Q(t){return t.object.type===e.PdfAnnotationSubtype.CARET}function Z(t){return t.object.type===e.PdfAnnotationSubtype.WIDGET}function J(t){return X(t)&&!t.object.inReplyToId||K(t)||N(t)||B(t)||O(t)||U(t)||$(t)||G(t)||Y(t)||V(t)||q(t)||Q(t)}function tt(t){return J(t)||Z(t)}function et(t){return(null==t?void 0:t.categories)??[]}function nt(t,e){switch(e.type){case w.None:return!1;case w.All:return!0;case w.Include:return 0!==t.length&&t.some(t=>e.categories.includes(t));case w.Exclude:return 0===t.length||!t.some(t=>e.categories.includes(t))}}function ot(t){var e;return(null==(e=t.flags)?void 0:e.includes("locked"))??!1}const it=(t,e)=>(t.pages[e]??[]).map(e=>t.byUid[e]),at=t=>t.selectedUids.length>0?t.byUid[t.selectedUids[0]]??null:null,st=t=>t.selectedUids.map(e=>t.byUid[e]).filter(t=>void 0!==t),rt=t=>t.selectedUids,ct=(t,e)=>t.byUid[e]??null;const dt=t=>{const n={};for(const e of Object.values(t.pages))for(const o of e){const e=t.byUid[o];if(e&&X(e)){const t=e.object.inReplyToId;t&&(n[t]||(n[t]=[])).push(e)}}const o={},i=new Set;for(const s of Object.values(t.pages))for(const n of s){const a=t.byUid[n];if(a&&a.object.inReplyToId&&a.object.replyType===e.PdfAnnotationReplyType.Group&&a.object.type!==e.PdfAnnotationSubtype.LINK&&J(a)){const t=a.object.inReplyToId;(o[t]||(o[t]=[])).push(a),i.add(a.object.id)}}const a={};for(const[e,s]of Object.entries(t.pages)){const r=Number(e),c=[];for(const e of s){const a=t.byUid[e];if(!a||!J(a))continue;if(i.has(a.object.id))continue;const s=o[a.object.id];c.push({page:r,annotation:a,replies:n[a.object.id]??[],...s&&s.length>0?{groupMembers:s}:{}})}c.length>0&&(a[r]=c)}return a},lt=(t,e)=>{const n=[];for(const o of Object.values(t.pages))for(const i of o){const o=t.byUid[i];o&&"inReplyToId"in o.object&&o.object.inReplyToId===e&&n.push({id:o.object.id,pageIndex:o.object.pageIndex})}return n},ut=(t,e,n)=>{const o=[];for(const i of Object.values(t.pages))for(const a of i){const i=t.byUid[a];i&&"inReplyToId"in i.object&&i.object.inReplyToId===e&&n.includes(i.object.type)&&o.push(i)}return o},ht=(t,n)=>ut(t,n,[e.PdfAnnotationSubtype.LINK]),pt=(t,n)=>{const o=t.byUid[n];if(o)return o.object.inReplyToId&&o.object.replyType===e.PdfAnnotationReplyType.Group?o.object.inReplyToId:n},gt=(t,n)=>{const o=pt(t,n);if(!o)return[];const i=[],a=t.byUid[o];a&&a.object.type!==e.PdfAnnotationSubtype.LINK&&i.push(a);for(const s of Object.values(t.pages))for(const n of s){const a=t.byUid[n];a&&a.object.inReplyToId===o&&a.object.replyType===e.PdfAnnotationReplyType.Group&&a.object.type!==e.PdfAnnotationSubtype.LINK&&i.push(a)}return i},ft=(t,n)=>{const o=t.byUid[n];if(!o)return!1;if(o.object.type===e.PdfAnnotationSubtype.LINK)return!1;if(o.object.inReplyToId&&o.object.replyType===e.PdfAnnotationReplyType.Group)return!0;for(const i of Object.values(t.pages))for(const o of i){const i=t.byUid[o];if(i&&i.object.inReplyToId===n&&i.object.replyType===e.PdfAnnotationReplyType.Group&&i.object.type!==e.PdfAnnotationSubtype.LINK)return!0}return!1},mt=t=>{const e=st(t);if(0===e.length)return"disabled";const n=e[0].object.id;if(ft(t,n)){const o=gt(t,n),i=new Set(o.map(t=>t.object.id));if(e.every(t=>i.has(t.object.id)))return"ungroup"}return e.length>=2?"group":"disabled"};function yt(t){let e=t;return[()=>e,t=>{e=t}]}const At={annotationType:e.PdfAnnotationSubtype.INK,create(n){const{onCommit:o,onPreview:i,getTool:a,pageSize:s}=n,[r,c]=yt([]),[d,l]=yt(!1),u={current:null},h=e=>({x:t.clamp(e.x,0,s.width),y:t.clamp(e.y,0,s.height)}),p=()=>{const t=a();return t?{...t.defaults,strokeWidth:t.defaults.strokeWidth??1,strokeColor:t.defaults.strokeColor??t.defaults.color??"#000000",opacity:t.defaults.opacity??1,flags:t.defaults.flags??["print"]}:null},g=()=>{const t=r();if(0===t.length||0===t[0].points.length)return null;const n=p();if(!n)return null;const o=t.flatMap(t=>t.points),i=e.expandRect(e.rectFromPoints(o),n.strokeWidth/2);return{type:e.PdfAnnotationSubtype.INK,bounds:i,data:{...n,rect:i,inkList:t,blendMode:n.blendMode}}};return{onPointerDown:(t,e)=>{var n;const o=h(t);l(!0),u.current&&clearTimeout(u.current);const a=[...r(),{points:[o]}];c(a),i(g()),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{if(!d())return;const e=r();if(0===e.length)return;const n=h(t);e[e.length-1].points.push(n),c(e),i(g())},onPointerUp:(t,s)=>{var d;l(!1),null==(d=s.releasePointerCapture)||d.call(s);const h=a(),f=null==h?void 0:h.behavior;if(null==f?void 0:f.smartLineRecognition){const t=f.smartLineThreshold??.15,e=r(),n=e[e.length-1];if(n&&n.points.length>1&&function(t,e){if(t.length<3)return!0;const n=t[0],o=t[t.length-1],i=Math.hypot(o.x-n.x,o.y-n.y);return!(i<5)&&t.reduce((t,e)=>{const a=Math.abs((o.x-n.x)*(n.y-e.y)-(n.x-e.x)*(o.y-n.y))/i;return Math.max(t,a)},0)/i<e}(n.points,t)){const t=n.points[0],o=n.points[n.points.length-1],a=o.x-t.x,s=o.y-t.y,r=Math.atan2(Math.abs(s),Math.abs(a))*(180/Math.PI),d=f.snapAngleDeg??15;if(r<=d){const e=n.points.reduce((t,e)=>t+e.y,0)/n.points.length;n.points=[{x:t.x,y:e},{x:o.x,y:e}]}else if(r>=90-d){const e=n.points.reduce((t,e)=>t+e.x,0)/n.points.length;n.points=[{x:e,y:t.y},{x:e,y:o.y}]}c([...e]),i(g())}}const m=(null==f?void 0:f.commitDelay)??800;u.current&&clearTimeout(u.current),u.current=setTimeout(()=>{const t=r();if(t.length>0&&t[0].points.length>1){const i=p();if(!i)return;const a=t.flatMap(t=>t.points),s=e.expandRect(e.rectFromPoints(a),i.strokeWidth/2);o({...i,inkList:t,rect:s,type:e.PdfAnnotationSubtype.INK,pageIndex:n.pageIndex,id:e.uuidV4(),created:new Date})}c([]),i(null)},m)},onPointerCancel:(t,e)=>{var n;c([]),l(!1),i(null),u.current&&clearTimeout(u.current),null==(n=e.releasePointerCapture)||n.call(e)}}}};function xt(t){const e=t=>{const e=9*t,n=Math.PI/6;return{x:-e*Math.cos(n),y:e*Math.sin(n)}};return{getSvgPath:n=>{const{x:o,y:i}=e(n);return t?`M 0 0 L ${o} ${i} L ${o} ${-i} Z`:`M ${o} ${i} L 0 0 L ${o} ${-i}`},getLocalPoints:t=>{const{x:n,y:o}=e(t);return[{x:0,y:0},{x:n,y:o},{x:n,y:-o}]},getRotation:t=>t,filled:t}}function bt(t,e){const n=e=>e*t/2;return{getSvgPath:t=>{const e=n(t);return`M ${-e} 0 L ${e} 0`},getLocalPoints:t=>{const e=n(t);return[{x:-e,y:0},{x:e,y:0}]},getRotation:e,filled:!1}}const vt=xt(!1),It=xt(!0),Pt={[e.PdfAnnotationLineEnding.OpenArrow]:vt,[e.PdfAnnotationLineEnding.ClosedArrow]:It,[e.PdfAnnotationLineEnding.ROpenArrow]:{...vt,getRotation:t=>t+Math.PI},[e.PdfAnnotationLineEnding.RClosedArrow]:{...It,getRotation:t=>t+Math.PI},[e.PdfAnnotationLineEnding.Circle]:{getSvgPath:t=>{const e=5*t/2;return`M ${e} 0 A ${e} ${e} 0 1 1 ${-e} 0 A ${e} ${e} 0 1 1 ${e} 0`},getLocalPoints:t=>{const e=5*t/2;return[{x:-e,y:-e},{x:e,y:e}]},getRotation:()=>0,filled:!0},[e.PdfAnnotationLineEnding.Square]:{getSvgPath:t=>{const e=6*t/2;return`M ${-e} ${-e} L ${e} ${-e} L ${e} ${e} L ${-e} ${e} Z`},getLocalPoints:t=>{const e=6*t/2;return[{x:-e,y:-e},{x:e,y:-e},{x:e,y:e},{x:-e,y:e}]},getRotation:t=>t,filled:!0},[e.PdfAnnotationLineEnding.Diamond]:{getSvgPath:t=>{const e=6*t/2;return`M 0 ${-e} L ${e} 0 L 0 ${e} L ${-e} 0 Z`},getLocalPoints:t=>{const e=6*t/2;return[{x:0,y:-e},{x:e,y:0},{x:0,y:e},{x:-e,y:0}]},getRotation:t=>t,filled:!0},[e.PdfAnnotationLineEnding.Butt]:bt(6,t=>t+Math.PI/2),[e.PdfAnnotationLineEnding.Slash]:bt(18,t=>t+Math.PI/1.5)};function St(t,n,o){if(!t||0===t.length)return{origin:{x:0,y:0},size:{width:0,height:0}};const i=[...t],a=(t,e)=>Math.atan2(e.y-t.y,e.x-t.x),s=(t,o,a)=>{if(!t)return;const s=Pt[t];if(!s)return;const r=s.getLocalPoints(n),c=s.getRotation(a),d=r.map(t=>e.rotateAndTranslatePoint(t,c,o));i.push(...d)};if(t.length>=2){const e=a(t[1],t[0]);s(null==o?void 0:o.start,t[0],e);const n=t.length-1,i=a(t[n-1],t[n]);s(null==o?void 0:o.end,t[n],i)}if(i.length<=1){const e=t[0]||{x:0,y:0},o=n;return{origin:{x:e.x-o,y:e.y-o},size:{width:2*o,height:2*o}}}const r=e.rectFromPoints(i),c=n/2+1.2*n;return e.expandRect(r,c)}function Tt(t,n){if(!t.unrotatedRect)return{rect:n};const o=e.getRectCenter(n);return{rect:e.calculateRotatedRectAABBAroundPoint(n,t.rotation??0,o),unrotatedRect:n}}function Rt(t){return t.unrotatedRect?e.inferRotationCenterFromRects(t.unrotatedRect,t.rect,t.rotation??0):e.getRectCenter(t.rect)}function Mt(t,n,o){const i=Rt(t),a=t.unrotatedRect??t.rect,s=n.origin.x-a.origin.x,r=n.origin.y-a.origin.y,c={x:i.x+s,y:i.y+r};return{rect:e.calculateRotatedRectAABBAroundPoint(n,o,c),unrotatedRect:n}}function kt(t,n,o){if(!t.unrotatedRect)return n;const i=t.rotation??0;if(Math.abs(i%360)<1e-8)return n;const a=Rt(t),s=e.getRectCenter(o),r=i*Math.PI/180,c=Math.cos(r),d=Math.sin(r),l=a.x-s.x,u=a.y-s.y,h=(1-c)*l+d*u,p=-d*l+(1-c)*u;return Math.abs(h)<1e-8&&Math.abs(p)<1e-8?n:n.map(t=>({x:t.x+h,y:t.y+p}))}class Ct{constructor(){this.patches=new Map}register(t,e){this.patches.set(t,e)}transform(t,e){const n=this.patches.get(t.type);return n?n(t,e):e.changes}}const Dt=new Ct;function Lt(t,e){var n;if(void 0===(null==(n=e.metadata)?void 0:n.rotationAngle))return null;const o=e.metadata.rotationAngle,i=e.changes.unrotatedRect??t.unrotatedRect??t.rect;return{...Mt(t,{origin:{...i.origin},size:{...i.size}},o),rotation:o}}function Et(t,e){return{rotation:e,...Mt(t,t.unrotatedRect??t.rect,e)}}function jt(t,e){const n=e.origin.x-t.rect.origin.x,o=e.origin.y-t.rect.origin.y,i={rect:e};return t.unrotatedRect&&(i.unrotatedRect={origin:{x:t.unrotatedRect.origin.x+n,y:t.unrotatedRect.origin.y+o},size:{...t.unrotatedRect.size}}),{dx:n,dy:o,rects:i}}function wt(t,n,o){const i=t.unrotatedRect??t.rect;let a=n.size.width/i.size.width,s=n.size.height/i.size.height;if((n.size.width<10||n.size.height<10)&&(a=Math.max(a,10/i.size.width),s=Math.max(s,10/i.size.height),n={origin:n.origin,size:{width:i.size.width*a,height:i.size.height*s}}),null==o?void 0:o.maintainAspectRatio){const t=Math.min(a,s);a=t,s=t,n={origin:n.origin,size:{width:i.size.width*t,height:i.size.height*t}}}return{scaleX:a,scaleY:s,oldRect:i,resolvedRect:n,rects:t.unrotatedRect?{unrotatedRect:n,rect:e.calculateRotatedRectAABB(n,t.rotation??0)}:{rect:n}}}function zt(t,e){const n=t.unrotatedRect??t.rect,o=e.unrotatedRect??n;return{dx:o.origin.x-n.origin.x,dy:o.origin.y-n.origin.y}}function Nt(t,e,n){if(0===e||void 0!==t.rotation)return t;const o=(4-e)%4*90;let i=t;if(n&&(1===e||3===e)){const e=t.rect,n=e.origin.x+e.size.width/2,o=e.origin.y+e.size.height/2;i={...t,rect:{origin:{x:n-e.size.height/2,y:o-e.size.width/2},size:{width:e.size.height,height:e.size.width}}}}const{rotation:a,rect:s,unrotatedRect:r}=Et(i,o);return{...i,rotation:a,rect:s,unrotatedRect:r}}function Ot(e,n){const o=t.clamp(e.rect.origin.x,0,n.width-e.rect.size.width),i=t.clamp(e.rect.origin.y,0,n.height-e.rect.size.height),a=o-e.rect.origin.x,s=i-e.rect.origin.y;return 0===a&&0===s?e:{...e,rect:{origin:{x:o,y:i},size:e.rect.size},...e.unrotatedRect?{unrotatedRect:{origin:{x:e.unrotatedRect.origin.x+a,y:e.unrotatedRect.origin.y+s},size:e.unrotatedRect.size}}:{}}}const Ut=(t,n)=>{switch(n.type){case"vertex-edit":default:return n.changes;case"move":{if(!n.changes.rect)return n.changes;const{dx:e,dy:o,rects:i}=jt(t,n.changes.rect);return{...i,inkList:t.inkList.map(t=>({points:t.points.map(t=>({x:t.x+e,y:t.y+o}))}))}}case"resize":{if(!n.changes.rect)return n.changes;const{scaleX:e,scaleY:o,oldRect:i,resolvedRect:a,rects:s}=wt(t,n.changes.rect,n.metadata),r=(t,e)=>({origin:{x:t.origin.x+e,y:t.origin.y+e},size:{width:Math.max(1,t.size.width-2*e),height:Math.max(1,t.size.height-2*e)}}),c=.001,d=Math.abs(e-1)>c,l=Math.abs(o-1)>c,u=d&&!l?e:!d&&l?o:Math.min(e,o),h=Math.max(1,t.strokeWidth*u),p=Math.max(1,Math.min(a.size.width,a.size.height)),g=Math.min(h,p),f=Number(g.toFixed(1)),m=r(i,t.strokeWidth/2),y=r(a,f/2),A=y.size.width/Math.max(m.size.width,1e-6),x=y.size.height/Math.max(m.size.height,1e-6);return{...s,inkList:t.inkList.map(t=>({points:t.points.map(t=>({x:y.origin.x+(t.x-m.origin.x)*A,y:y.origin.y+(t.y-m.origin.y)*x}))})),strokeWidth:f}}case"rotate":{const e=Lt(t,n);if(!e)return n.changes;const{dx:o,dy:i}=zt(t,e);return{...e,inkList:t.inkList.map(t=>({points:t.points.map(t=>({x:t.x+o,y:t.y+i}))}))}}case"property-update":{if(!(void 0!==n.changes.strokeWidth||void 0!==n.changes.rotation))return n.changes;const o={...t,...n.changes},i=o.inkList.flatMap(t=>t.points),a=e.expandRect(e.rectFromPoints(i),o.strokeWidth/2),s=n.changes.rotation??t.rotation??0;return t.unrotatedRect||void 0!==n.changes.rotation?{...n.changes,unrotatedRect:a,rect:e.calculateRotatedRectAABBAroundPoint(a,s,Rt(t))}:{...n.changes,rect:a}}}},Bt=(t,n)=>{switch(n.type){case"vertex-edit":if(n.changes.linePoints){const{start:e,end:o}=n.changes.linePoints,i=[e,o],a=kt(t,i,St(i,t.strokeWidth,t.lineEndings));return{...Tt(t,St(a,t.strokeWidth,t.lineEndings)),linePoints:{start:a[0],end:a[1]}}}return n.changes;case"move":{if(!n.changes.rect)return n.changes;const{dx:e,dy:o,rects:i}=jt(t,n.changes.rect);return{...i,linePoints:{start:{x:t.linePoints.start.x+e,y:t.linePoints.start.y+o},end:{x:t.linePoints.end.x+e,y:t.linePoints.end.y+o}}}}case"resize":{if(!n.changes.rect)return n.changes;const{scaleX:e,scaleY:o,oldRect:i,resolvedRect:a,rects:s}=wt(t,n.changes.rect,n.metadata);return{...s,linePoints:{start:{x:a.origin.x+(t.linePoints.start.x-i.origin.x)*e,y:a.origin.y+(t.linePoints.start.y-i.origin.y)*o},end:{x:a.origin.x+(t.linePoints.end.x-i.origin.x)*e,y:a.origin.y+(t.linePoints.end.y-i.origin.y)*o}}}}case"rotate":{const e=Lt(t,n);if(!e)return n.changes;const{dx:o,dy:i}=zt(t,e);return{...e,linePoints:{start:{x:t.linePoints.start.x+o,y:t.linePoints.start.y+i},end:{x:t.linePoints.end.x+o,y:t.linePoints.end.y+i}}}}case"property-update":{if(!(void 0!==n.changes.strokeWidth||void 0!==n.changes.lineEndings||void 0!==n.changes.rotation))return n.changes;const o={...t,...n.changes},i=St([o.linePoints.start,o.linePoints.end],o.strokeWidth,o.lineEndings),a=n.changes.rotation??t.rotation??0;return t.unrotatedRect||void 0!==n.changes.rotation?{...n.changes,unrotatedRect:i,rect:e.calculateRotatedRectAABBAroundPoint(i,a,Rt(t))}:{...n.changes,rect:i}}default:return n.changes}},$t=(t,n)=>{switch(n.type){case"vertex-edit":if(n.changes.vertices&&n.changes.vertices.length){const e=n.changes.vertices,o=kt(t,e,St(e,t.strokeWidth,t.lineEndings));return{...Tt(t,St(o,t.strokeWidth,t.lineEndings)),vertices:o}}return n.changes;case"move":{if(!n.changes.rect)return n.changes;const{dx:e,dy:o,rects:i}=jt(t,n.changes.rect);return{...i,vertices:t.vertices.map(t=>({x:t.x+e,y:t.y+o}))}}case"resize":{if(!n.changes.rect)return n.changes;const{scaleX:e,scaleY:o,oldRect:i,resolvedRect:a,rects:s}=wt(t,n.changes.rect,n.metadata);return{...s,vertices:t.vertices.map(t=>({x:a.origin.x+(t.x-i.origin.x)*e,y:a.origin.y+(t.y-i.origin.y)*o}))}}case"rotate":{const e=Lt(t,n);if(!e)return n.changes;const{dx:o,dy:i}=zt(t,e);return{...e,vertices:t.vertices.map(t=>({x:t.x+o,y:t.y+i}))}}case"property-update":{if(!(void 0!==n.changes.strokeWidth||void 0!==n.changes.lineEndings||void 0!==n.changes.rotation))return n.changes;const o={...t,...n.changes},i=St(o.vertices,o.strokeWidth,o.lineEndings),a=n.changes.rotation??t.rotation??0;return t.unrotatedRect||void 0!==n.changes.rotation?{...n.changes,unrotatedRect:i,rect:e.calculateRotatedRectAABBAroundPoint(i,a,Rt(t))}:{...n.changes,rect:i}}default:return n.changes}};function Gt(t,e,n,o){const i=o*Math.PI/180,a=Math.abs(Math.cos(i)),s=Math.abs(Math.sin(i)),r=a*a-s*s,c=t.size.width,d=t.size.height;let l,u;if(Math.abs(r)>1e-6)l=(a*c-s*d)/r,u=(a*d-s*c)/r,l=Math.max(l,1),u=Math.max(u,1);else{const t=e.size.width*e.size.height,o=c*d,i=t>0?Math.sqrt(o/t):1;l=n.size.width*i,u=n.size.height*i}return{origin:{x:t.origin.x+c/2-l/2,y:t.origin.y+d/2-u/2},size:{width:l,height:u}}}const Ft=Math.PI,Wt=Math.PI/2,Ht=34*Math.PI/180,_t=30*Math.PI/180,Kt=12*Math.PI/180;class Yt{constructor(){this.parts=[],this.bbox={minX:1/0,minY:1/0,maxX:-1/0,maxY:-1/0},this.started=!1}moveTo(t,e){const n=-e;this.updateBBox(t,n),this.parts.push(`M ${Vt(t)} ${Vt(n)}`),this.started=!0}curveTo(t,e,n,o,i,a){const s=-e,r=-o,c=-a;this.updateBBox(t,s),this.updateBBox(n,r),this.updateBBox(i,c),this.parts.push(`C ${Vt(t)} ${Vt(s)}, ${Vt(n)} ${Vt(r)}, ${Vt(i)} ${Vt(c)}`)}close(){this.started&&this.parts.push("Z")}build(t){const e=t>0?t/2:0;return{path:this.parts.join(" "),bbox:{minX:this.bbox.minX-e,minY:this.bbox.minY-e,maxX:this.bbox.maxX+e,maxY:this.bbox.maxY+e}}}updateBBox(t,e){t<this.bbox.minX&&(this.bbox.minX=t),e<this.bbox.minY&&(this.bbox.minY=e),t>this.bbox.maxX&&(this.bbox.maxX=t),e>this.bbox.maxY&&(this.bbox.maxY=e)}}function Vt(t){return Number(t.toFixed(4)).toString()}function Xt(t,e){const n=e.x-t.x,o=e.y-t.y;return Math.sqrt(n*n+o*o)}function qt(t,e){return 0===e?0:t/e}function Qt(t,e){return 0===e?0:t/e}function Zt(t,e,n,o,i,a,s,r){const c=Math.cos(t),d=Math.sin(t),l=Math.cos(e),u=Math.sin(e),h=Math.sin((e-t)/2);if(0===h)return void(r&&s.moveTo(n+i*c,o+a*d));const p=4/3*(1-Math.cos((e-t)/2))/h,g=n+i*(c-p*d),f=o+a*(d+p*c),m=n+i*(l+p*u),y=o+a*(u-p*l),A=n+i*l,x=o+a*u;r&&s.moveTo(n+i*c,o+a*d),s.curveTo(g,f,m,y,A,x)}function Jt(t,e,n,o,i,a){const s=Math.cos(t),r=Math.sin(t),c=Math.cos(e),d=Math.sin(e),l=Math.sin((e-t)/2);if(0===l)return[];const u=4/3*(1-Math.cos((e-t)/2))/l;return[{x:n+i*(s-u*r),y:o+a*(r+u*s)},{x:n+i*(c+u*d),y:o+a*(d-u*c)},{x:n+i*c,y:o+a*d}]}function te(t,e,n,o,i,a,s,r){const c=Wt;let d=e-t;for(;d<0;)d+=2*Math.PI;const l=d;let u=0;for(r&&s.moveTo(i+n*Math.cos(t),a+o*Math.sin(t));d>c;)Zt(t+u,t+u+c,i,a,n,o,s,!1),u+=c,d-=c;d>0&&Zt(t+u,t+l,i,a,n,o,s,!1)}function ee(t,e,n,o,i,a,s,r,c){let d=t+Ft+s;const l=t+Ft+s-22*Math.PI/180;Zt(d,l,o,i,n,n,r,c),d=l;te(d,e-a,n,n,o,i,r,!1)}function ne(t,e,n,o,i,a){const s=t+Ft;Zt(s+n,s+n-_t,o,i,e,e,a,!1),Zt(s+n-_t,s+Wt,o,i,e,e,a,!1),Zt(s+Wt,s+Ft-Ht,o,i,e,e,a,!1)}function oe(t,e){const n=[],o=t+Ft;return n.push(...Jt(o+Ht,o+Kt,0,0,e,e)),n.push(...Jt(o+Kt,o+Wt,0,0,e,e)),n.push(...Jt(o+Wt,o+Ft-Ht,0,0,e,e)),n}function ie(t,e,n,o){for(let i=0;i+2<t.length;i+=3){const a=t[i],s=t[i+1],r=t[i+2];o.curveTo(a.x+e,a.y+n,s.x+e,s.y+n,r.x+e,r.y+n)}}function ae(t,e,n){if(0===n)return{n:-1,adjustedRadius:t};const o=n-2*e*t;if(o<=0)return{n:0,adjustedRadius:t};const i=2*e*t,a=Math.max(1,Math.ceil(o/i));return{n:a,adjustedRadius:o/(2*a*e)}}function se(t,e,n,o,i){let a=function(t){if(t.length<=2)return t;const e=[t[0]];for(let n=1;n<t.length;n++){const o=e[e.length-1],i=t[n];(Math.abs(i.x-o.x)>=.5||Math.abs(i.y-o.y)>=.5)&&e.push(i)}return e}(t);var s;(function(t){let e=0;const n=t.length;for(let o=0;o<n;o++){const i=(o+1)%n;e+=t[o].x*t[i].y-t[o].y*t[i].x}return e})(s=a)<0&&s.reverse();const r=a.length;if(r<2)return;if(n<=0){i.moveTo(a[0].x,a[0].y);for(let t=1;t<r;t++)i.curveTo(a[t].x,a[t].y,a[t].x,a[t].y,a[t].x,a[t].y);return}let c=e?re(n,o):ce(n,o);c<.5&&(c=.5);const d=Math.cos(Ht),l=[];for(let p=0;p+1<r;p++){const t=Xt(a[p],a[p+1]);t<=0||t>=2*d*c?l.push(Ht):l.push(Math.acos(Math.min(1,t/(2*c))))}let u=0,h=!1;for(let p=0;p+1<r;p++){const t=a[p],e=a[p+1],n=Xt(t,e);if(0===n)continue;const o=ae(c,d,n);if(o.n<0){h||(i.moveTo(t.x,t.y),h=!0);continue}const s=Math.max(.5,o.adjustedRadius),g=2*d*s,f=d*c+d*s;let m=Math.atan2(e.y-t.y,e.x-t.x);if(0===p){const e=a[r-2];u=Math.atan2(t.y-e.y,t.x-e.x)}const y=qt(e.x-t.x,n),A=Qt(e.y-t.y,n);let x=t.x,b=t.y;const v=l[p],I=l[0===p?r-2:p-1]??Ht;if(ee(u,m,c,t.x,t.y,v,I,i,!h),h=!0,0===o.n)x+=n*y,b+=n*A;else{x+=f*y,b+=f*A;let t=o.n;o.n>=1&&(ne(m,s,Ht,x,b,i),x+=g*y,b+=g*A,t=o.n-1);const e=oe(m,s);for(let n=0;n<t;n++)ie(e,x,b,i),x+=g*y,b+=g*A}u=m}}function re(t,e){return 4.75*t+.5*e}function ce(t,e){return 4*t+.5*e}function de(t,e,n,o,i,a,s){if(i<=0){const i=Math.abs(n-t)/2,a=Math.abs(o-e)/2,r=(t+n)/2,c=(e+o)/2;return void te(0,2*Math.PI,i,a,r,c,s,!0)}const r=n-t,c=o-e;let d=re(i,a);const l=.5*d;if(r<l&&c<l){const i=Math.abs(n-t)/2,a=Math.abs(o-e)/2,r=(t+n)/2,c=(e+o)/2;return void te(0,2*Math.PI,i,a,r,c,s,!0)}if(r<5&&c>20||r>20&&c<5)return void se([{x:t,y:e},{x:n,y:e},{x:n,y:o},{x:t,y:o},{x:t,y:e}],!0,i,a,s);const u=Math.sin(Kt)*d-1.5;let h=t,p=n,g=e,f=o;if(r>2*u)h+=u,p-=u;else{const e=(t+n)/2;h=e-.1,p=e+.1}if(c>2*u)g+=u,f-=u;else{const t=(o+e)/2;f=t+.1,g=t-.1}const m=function(t,e,n,o){const i=(t+n)/2,a=(e+o)/2,s=(n-t)/2,r=(o-e)/2;if(s<=0||r<=0)return[];const c=Math.max(32,Math.ceil(2*Math.max(s,r))),d=[];for(let l=0;l<=c;l++){const t=2*Math.PI*l/c;d.push({x:i+s*Math.cos(t),y:a+r*Math.sin(t)})}return d}(h,g,p,f),y=m.length;if(y<2)return;let A=0;for(let k=1;k<y;k++)A+=Xt(m[k-1],m[k]);const x=Math.cos(Ht);let b=2*x*d,v=Math.ceil(A/b);if(v<2){const i=Math.abs(n-t)/2,a=Math.abs(o-e)/2,r=(t+n)/2,c=(e+o)/2;return void te(0,2*Math.PI,i,a,r,c,s,!0)}if(b=A/v,d=b/(2*x),d<.5)d=.5,b=2*x*d;else if(d<3){const i=Math.abs(n-t)/2,a=Math.abs(o-e)/2,r=(t+n)/2,c=(e+o)/2;return void te(0,2*Math.PI,i,a,r,c,s,!0)}const I=[];let P=0;const S=.1*a;for(let k=0;k+1<y;k++){const t=m[k],e=m[k+1],n=e.x-t.x,o=e.y-t.y,i=Xt(t,e);if(0===i)continue;let a=i+P;if(a>=b-S||k===y-2){const e=qt(n,i),s=Qt(o,i);let r=b-P;for(;a>=b-S;)I.push({x:t.x+r*e,y:t.y+r*s}),a-=b,r+=b;P=Math.max(0,a)}else P+=i}const T=I.length;let R=0,M=0;for(let k=0;k<T;k++){const t=(k+1)%T,e=I[k],n=I[t];if(0===k){const t=I[T-1];R=Math.atan2(e.y-t.y,e.x-t.x),M=le(t,e,d,b)}const o=Math.atan2(n.y-e.y,n.x-e.x),i=le(e,n,d,b);ee(R,o,d,e.x,e.y,i,M,s,0===k),R=o,M=i}}function le(t,e,n,o){const i=Xt(t,e);if(0===i)return Ht;const a=(o/2+(i-o)/2)/n;return a<-1||a>1?0:Math.acos(a)}function ue(t,e,n){return(n?re(t,e):ce(t,e))+e/2}function he(t,e){return(t??0)>0?ue(t,e,!1):e/2}const pe=(t,n)=>{switch(n.type){case"vertex-edit":if(n.changes.vertices&&n.changes.vertices.length){const o=he(t.cloudyBorderIntensity,t.strokeWidth),i=n.changes.vertices,a=kt(t,i,e.expandRect(e.rectFromPoints(i),o));return{...Tt(t,e.expandRect(e.rectFromPoints(a),o)),vertices:a}}return n.changes;case"move":{if(!n.changes.rect)return n.changes;const{dx:e,dy:o,rects:i}=jt(t,n.changes.rect);return{...i,vertices:t.vertices.map(t=>({x:t.x+e,y:t.y+o}))}}case"resize":{if(!n.changes.rect)return n.changes;const{scaleX:e,scaleY:o,oldRect:i,resolvedRect:a,rects:s}=wt(t,n.changes.rect,n.metadata);return{...s,vertices:t.vertices.map(t=>({x:a.origin.x+(t.x-i.origin.x)*e,y:a.origin.y+(t.y-i.origin.y)*o}))}}case"rotate":{const e=Lt(t,n);if(!e)return n.changes;const{dx:o,dy:i}=zt(t,e);return{...e,vertices:t.vertices.map(t=>({x:t.x+o,y:t.y+i}))}}case"property-update":{const o=void 0!==n.changes.cloudyBorderIntensity;if(!(void 0!==n.changes.strokeWidth||void 0!==n.changes.rotation||o))return n.changes;const i={...t,...n.changes},a=he(i.cloudyBorderIntensity,i.strokeWidth),s=e.expandRect(e.rectFromPoints(i.vertices),a);let r=n.changes;const c=(t.cloudyBorderIntensity??0)>0;if(o||void 0!==n.changes.strokeWidth&&c){const t=i.cloudyBorderIntensity??0;if(t>0){const e=ue(t,i.strokeWidth,!1);r={...r,rectangleDifferences:{left:e,top:e,right:e,bottom:e}}}else r={...r,rectangleDifferences:void 0}}const d=n.changes.rotation??t.rotation??0;return t.unrotatedRect||void 0!==n.changes.rotation?{...r,unrotatedRect:s,rect:e.calculateRotatedRectAABBAroundPoint(s,d,Rt(t))}:{...r,rect:s}}default:return n.changes}},ge=(t,e)=>{switch(e.type){case"move":return e.changes.rect?jt(t,e.changes.rect).rects:e.changes;case"resize":return e.changes.rect?wt(t,e.changes.rect,e.metadata).rects:e.changes;case"rotate":return Lt(t,e)??e.changes;case"property-update":{let n=e.changes;const o=void 0!==e.changes.cloudyBorderIntensity,i=void 0!==e.changes.strokeWidth,a=(t.cloudyBorderIntensity??0)>0;if(o||i&&a){const o={...t,...e.changes},i=o.cloudyBorderIntensity??0;if(i>0){const t=ue(i,o.strokeWidth,!0);n={...n,rectangleDifferences:{left:t,top:t,right:t,bottom:t}}}else n={...n,rectangleDifferences:void 0}}return void 0!==e.changes.rotation&&(n={...n,...Et(t,e.changes.rotation)}),n}default:return e.changes}},fe=(t,e)=>{switch(e.type){case"move":return e.changes.rect?jt(t,e.changes.rect).rects:e.changes;case"resize":return e.changes.rect?wt(t,e.changes.rect,e.metadata).rects:e.changes;case"rotate":return Lt(t,e)??e.changes;case"property-update":{let n=e.changes;const o=void 0!==e.changes.cloudyBorderIntensity,i=void 0!==e.changes.strokeWidth,a=(t.cloudyBorderIntensity??0)>0;if(o||i&&a){const o={...t,...e.changes},i=o.cloudyBorderIntensity??0;if(i>0){const t=ue(i,o.strokeWidth,!1);n={...n,rectangleDifferences:{left:t,top:t,right:t,bottom:t}}}else n={...n,rectangleDifferences:void 0}}return void 0!==e.changes.rotation&&(n={...n,...Et(t,e.changes.rotation)}),n}default:return e.changes}},me=(t,e)=>{switch(e.type){case"move":return e.changes.rect?jt(t,e.changes.rect).rects:e.changes;case"resize":return e.changes.rect?wt(t,e.changes.rect,e.metadata).rects:e.changes;case"rotate":return Lt(t,e)??e.changes;case"property-update":return void 0!==e.changes.rotation?{...e.changes,...Et(t,e.changes.rotation)}:e.changes;default:return e.changes}},ye=(t,e)=>{switch(e.type){case"move":return e.changes.rect?jt(t,e.changes.rect).rects:e.changes;case"resize":return e.changes.rect?wt(t,e.changes.rect,e.metadata).rects:e.changes;case"rotate":return Lt(t,e)??e.changes;case"property-update":return void 0!==e.changes.rotation?{...e.changes,...Et(t,e.changes.rotation)}:e.changes;default:return e.changes}},Ae=Object.freeze(Object.defineProperty({__proto__:null,LINE_ENDING_HANDLERS:Pt,PatchRegistry:Ct,applyInsertUpright:Nt,calculateAABBFromVertices:function(t,n=0){if(0===t.length)return{origin:{x:0,y:0},size:{width:0,height:0}};const o=e.rectFromPoints(t);return n>0?e.expandRect(o,n):o},calculateRotatedRectAABB:e.calculateRotatedRectAABB,calculateRotatedRectAABBAroundPoint:e.calculateRotatedRectAABBAroundPoint,clampAnnotationToPage:Ot,compensateRotatedVertexEdit:kt,createEnding:function(t,e,n,o,i){if(!t)return null;const a=Pt[t];if(!a)return null;const s=a.getRotation(n);return{d:a.getSvgPath(e),transform:`translate(${o} ${i}) rotate(${r=s,180*r/Math.PI})`,filled:a.filled};var r},getRectCenter:e.getRectCenter,lineRectWithEndings:St,patchCircle:ge,patchFreeText:me,patchInk:Ut,patchLine:Bt,patchPolygon:pe,patchPolyline:$t,patchRegistry:Dt,patchSquare:fe,patchStamp:ye,resolveAnnotationRotationCenter:Rt,resolveRotateRects:Mt,resolveVertexEditRects:Tt,rotatePointAroundCenter:e.rotatePointAround,rotateVertices:e.rotateVertices},Symbol.toStringTag,{value:"Module"})),xe={annotationType:e.PdfAnnotationSubtype.TEXT,create(t){const{onCommit:n,getTool:o,pageSize:i}=t;return{onPointerDown:a=>{const s=o();if(!s)return;const r={origin:{x:a.x-12,y:a.y-12},size:{width:24,height:24}};let c={...s.defaults,rect:r,type:e.PdfAnnotationSubtype.TEXT,name:s.defaults.name??e.PdfAnnotationName.Comment,contents:s.defaults.contents??"",flags:s.defaults.flags??["print","noRotate","noZoom"],pageIndex:t.pageIndex,id:e.uuidV4(),created:new Date};c=Ot(c,i),n(c)}}}};function be({threshold:t=5,getTool:e,onClickDetected:n}){const[o,i]=yt(null),[a,s]=yt(!1);return{onStart:t=>{i(t),s(!1)},onMove:e=>{const n=o();if(!n||a())return;Math.sqrt(Math.pow(e.x-n.x,2)+Math.pow(e.y-n.y,2))>t&&s(!0)},onEnd:t=>{var r;if(o()&&!a()){const o=e();o&&"clickBehavior"in o&&(null==(r=o.clickBehavior)?void 0:r.enabled)&&n(t,o)}i(null),s(!1)},hasMoved:a,reset:()=>{i(null),s(!1)}}}const ve={annotationType:e.PdfAnnotationSubtype.FREETEXT,create(n){const{onCommit:o,onPreview:i,getTool:a,pageSize:s,pageIndex:r,pageRotation:c}=n,[d,l]=yt(null),u=e=>({x:t.clamp(e.x,0,s.width),y:t.clamp(e.y,0,s.height)}),h=()=>{const t=a();return t?{...t.defaults,fontColor:t.defaults.fontColor??"#000000",opacity:t.defaults.opacity??1,fontSize:t.defaults.fontSize??12,fontFamily:t.defaults.fontFamily??e.PdfStandardFont.Helvetica,color:t.defaults.color??t.defaults.backgroundColor??"transparent",textAlign:t.defaults.textAlign??e.PdfTextAlignment.Left,verticalAlign:t.defaults.verticalAlign??e.PdfVerticalAlignment.Top,contents:t.defaults.contents??"Insert text here",flags:t.defaults.flags??["print"]}:null},p=be({threshold:5,getTool:a,onClickDetected:(t,n)=>{var i;const a=h();if(!a)return;const d=n.clickBehavior;if(!(null==d?void 0:d.enabled))return;const{width:l,height:u}=d.defaultSize,p={origin:{x:t.x-l/2,y:t.y-u/2},size:{width:l,height:u}},g=d.defaultContent??a.contents;let f={...a,contents:g,type:e.PdfAnnotationSubtype.FREETEXT,rect:p,pageIndex:r,id:e.uuidV4(),created:new Date};(null==(i=n.behavior)?void 0:i.insertUpright)&&(f=Nt(f,c,!1)),f=Ot(f,s),o(f)}}),g=t=>{const n=d();if(!n)return null;const o=h();if(!o)return null;const i={origin:{x:Math.min(n.x,t.x),y:Math.min(n.y,t.y)},size:{width:Math.abs(n.x-t.x),height:Math.abs(n.y-t.y)}};return{type:e.PdfAnnotationSubtype.FREETEXT,bounds:i,data:{...o,rect:i}}};return{onPointerDown:(t,e)=>{var n;const o=u(t);l(o),p.onStart(o),i(g(o)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=u(t);p.onMove(e),d()&&p.hasMoved()&&i(g(e))},onPointerUp:(t,s)=>{var r,g;const f=d();if(!f)return;const m=h();if(!m)return;const y=u(t);if(p.hasMoved()){const t={origin:{x:Math.min(f.x,y.x),y:Math.min(f.y,y.y)},size:{width:Math.abs(f.x-y.x),height:Math.abs(f.y-y.y)}},i=a();let s={...m,type:e.PdfAnnotationSubtype.FREETEXT,rect:t,pageIndex:n.pageIndex,id:e.uuidV4(),created:new Date};(null==(r=null==i?void 0:i.behavior)?void 0:r.insertUpright)&&(s=Nt(s,c,!0)),o(s)}else p.onEnd(y);l(null),i(null),p.reset(),null==(g=s.releasePointerCapture)||g.call(s)},onPointerLeave:(t,e)=>{var n;l(null),i(null),p.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;l(null),i(null),p.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},Ie={annotationType:e.PdfAnnotationSubtype.LINE,create(n){const{pageIndex:o,onCommit:i,onPreview:a,getTool:s,pageSize:r}=n,[c,d]=yt(null),l=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.height)}),u=()=>{const t=s();return t?{...t.defaults,strokeWidth:t.defaults.strokeWidth??1,lineEndings:t.defaults.lineEndings??{start:e.PdfAnnotationLineEnding.None,end:e.PdfAnnotationLineEnding.None},color:t.defaults.color??"#000000",opacity:t.defaults.opacity??1,strokeStyle:t.defaults.strokeStyle??e.PdfAnnotationBorderStyle.SOLID,strokeDashArray:t.defaults.strokeDashArray??[],strokeColor:t.defaults.strokeColor??"#000000",flags:t.defaults.flags??["print"]}:null},h=be({threshold:5,getTool:s,onClickDetected:(t,n)=>{const a=u();if(!a)return;const s=n.clickBehavior;if(!(null==s?void 0:s.enabled))return;const r=s.defaultAngle??0,c=s.defaultLength/2,d=t.x-c*Math.cos(r),h=t.y-c*Math.sin(r),p=t.x+c*Math.cos(r),g=t.y+c*Math.sin(r),f=l({x:d,y:h}),m=l({x:p,y:g}),y=St([f,m],a.strokeWidth,a.lineEndings);i({...a,rect:y,linePoints:{start:f,end:m},pageIndex:o,id:e.uuidV4(),created:new Date,type:e.PdfAnnotationSubtype.LINE})}}),p=t=>{const n=c();if(!n)return null;const o=u();if(!o)return null;const i=St([n,t],o.strokeWidth,o.lineEndings);return{type:e.PdfAnnotationSubtype.LINE,bounds:i,data:{...o,rect:i,linePoints:{start:n,end:t}}}};return{onPointerDown:(t,e)=>{var n;const o=l(t);d(o),h.onStart(o),a(p(o)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=l(t);h.onMove(e),c()&&h.hasMoved()&&a(p(e))},onPointerUp:(t,n)=>{var s;const r=c();if(!r)return;const p=l(t);if(h.hasMoved()){const t=u();if(!t)return;if(Math.abs(p.x-r.x)>2||Math.abs(p.y-r.y)>2){const n=St([r,p],t.strokeWidth,t.lineEndings);i({...t,rect:n,linePoints:{start:r,end:p},pageIndex:o,id:e.uuidV4(),flags:["print"],created:new Date,type:e.PdfAnnotationSubtype.LINE})}}else h.onEnd(p);d(null),a(null),h.reset(),null==(s=n.releasePointerCapture)||s.call(n)},onPointerLeave:(t,e)=>{var n;d(null),a(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;d(null),a(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},Pe={annotationType:e.PdfAnnotationSubtype.POLYLINE,create(n){const{onCommit:o,onPreview:i,getTool:a,pageSize:s}=n,[r,c]=yt([]),[d,l]=yt(null),u=e=>({x:t.clamp(e.x,0,s.width),y:t.clamp(e.y,0,s.height)}),h=()=>{const t=a();return t?{...t.defaults,strokeWidth:t.defaults.strokeWidth??1,lineEndings:t.defaults.lineEndings??{start:e.PdfAnnotationLineEnding.None,end:e.PdfAnnotationLineEnding.None},color:t.defaults.color??"#000000",opacity:t.defaults.opacity??1,strokeColor:t.defaults.strokeColor??"#000000",strokeStyle:t.defaults.strokeStyle??e.PdfAnnotationBorderStyle.SOLID,strokeDashArray:t.defaults.strokeDashArray??[],flags:t.defaults.flags??["print"]}:null},p=()=>{const t=r(),n=d();if(0===t.length||!n)return null;const o=h();if(!o)return null;const i=[...t,n],a=St(i,o.strokeWidth,o.lineEndings);return{type:e.PdfAnnotationSubtype.POLYLINE,bounds:a,data:{...o,rect:a,vertices:i,currentVertex:n}}};return{onClick:(t,e)=>{if(e.metaKey||e.ctrlKey)return;const n=u(t),o=r(),a=o[o.length-1];a&&Math.abs(a.x-n.x)<1&&Math.abs(a.y-n.y)<1||(c([...o,n]),l(n),i(p()))},onDoubleClick:()=>{(()=>{const t=r();if(t.length<2)return;const a=h();if(!a)return;const s=St(t,a.strokeWidth,a.lineEndings),d={...a,vertices:t,rect:s,type:e.PdfAnnotationSubtype.POLYLINE,pageIndex:n.pageIndex,id:e.uuidV4(),created:new Date};o(d),c([]),l(null),i(null)})()},onPointerMove:t=>{if(r().length>0){const e=u(t);l(e),i(p())}},onPointerCancel:()=>{c([]),l(null),i(null)}}}},Se={annotationType:e.PdfAnnotationSubtype.POLYGON,create(n){const{onCommit:o,onPreview:i,getTool:a,scale:s,pageSize:r}=n,[c,d]=yt([]),[l,u]=yt(null),h=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.height)}),p=()=>{const t=a();return t?{...t.defaults,color:t.defaults.color??"#000000",opacity:t.defaults.opacity??1,strokeWidth:t.defaults.strokeWidth??1,strokeColor:t.defaults.strokeColor??"#000000",strokeStyle:t.defaults.strokeStyle??e.PdfAnnotationBorderStyle.SOLID,strokeDashArray:t.defaults.strokeDashArray??[],flags:t.defaults.flags??["print"]}:null},g=()=>{const t=c();if(t.length<3)return;const a=p();if(!a)return;const s=a.cloudyBorderIntensity??0,r=s>0?ue(s,a.strokeWidth,!1):a.strokeWidth/2,l=e.expandRect(e.rectFromPoints(t),r),h={...a,vertices:t,rect:l,type:e.PdfAnnotationSubtype.POLYGON,pageIndex:n.pageIndex,id:e.uuidV4(),created:new Date,...s>0&&{rectangleDifferences:{left:r,top:r,right:r,bottom:r}}};o(h),d([]),u(null),i(null)},f=()=>{const t=c(),n=l();if(0===t.length||!n)return null;const o=p();if(!o)return null;const i=o.cloudyBorderIntensity??0,a=i>0?ue(i,o.strokeWidth,!1):o.strokeWidth/2,s=[...t,n],r=e.expandRect(e.rectFromPoints(s),a);return{type:e.PdfAnnotationSubtype.POLYGON,bounds:r,data:{...o,rect:r,vertices:t,currentVertex:n}}};return{onClick:(t,e)=>{if(e.metaKey||e.ctrlKey)return;const n=h(t);if((t=>{const e=c();if(e.length<2)return!1;const n=14/s/2,o=e[0];return t.x>=o.x-n&&t.x<=o.x+n&&t.y>=o.y-n&&t.y<=o.y+n})(n)&&c().length>=3)return void g();const o=c(),a=o[o.length-1];a&&Math.abs(a.x-n.x)<1&&Math.abs(a.y-n.y)<1||(d([...o,n]),u(n),i(f()))},onDoubleClick:t=>{g()},onPointerMove:t=>{if(c().length>0){const e=h(t);u(e),i(f())}},onPointerCancel:t=>{d([]),u(null),i(null)}}}},Te={annotationType:e.PdfAnnotationSubtype.SQUARE,create(n){const{pageIndex:o,onCommit:i,onPreview:a,getTool:s,pageSize:r}=n,[c,d]=yt(null),l=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.height)}),u=()=>{const t=s();return t?{...t.defaults,flags:t.defaults.flags??["print"],strokeWidth:t.defaults.strokeWidth??2,strokeColor:t.defaults.strokeColor??"#000000",strokeStyle:t.defaults.strokeStyle??e.PdfAnnotationBorderStyle.SOLID,strokeDashArray:t.defaults.strokeDashArray??[],color:t.defaults.color??"#000000",opacity:t.defaults.opacity??1}:null},h=be({threshold:5,getTool:s,onClickDetected:(n,a)=>{const s=u();if(!s)return;const c=a.clickBehavior;if(!(null==c?void 0:c.enabled))return;const{width:d,height:l}=c.defaultSize,h=d/2,p=l/2,g=t.clamp(n.x-h,0,r.width-d),f=t.clamp(n.y-p,0,r.height-l),m=s.strokeWidth,y=s.cloudyBorderIntensity??0,A=y>0?ue(y,m,!1):m/2,x={origin:{x:g-A,y:f-A},size:{width:d+2*A,height:l+2*A}},b={...s,type:e.PdfAnnotationSubtype.SQUARE,created:new Date,id:e.uuidV4(),pageIndex:o,rect:x,...y>0&&{rectangleDifferences:{left:A,top:A,right:A,bottom:A}}};i(b)}}),p=t=>{const n=c();if(!n)return null;const o=Math.min(n.x,t.x),i=Math.min(n.y,t.y),a=Math.abs(n.x-t.x),s=Math.abs(n.y-t.y),r=u();if(!r)return null;const d=r.strokeWidth,l=r.cloudyBorderIntensity??0,h=l>0?ue(l,d,!1):d/2,p={origin:{x:o-h,y:i-h},size:{width:a+2*h,height:s+2*h}};return{type:e.PdfAnnotationSubtype.SQUARE,bounds:p,data:{rect:p,...r,...l>0&&{rectangleDifferences:{left:h,top:h,right:h,bottom:h}}}}};return{onPointerDown:(t,e)=>{var n;const o=l(t);d(o),h.onStart(o),a(p(o)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=l(t);h.onMove(e),c()&&h.hasMoved()&&a(p(e))},onPointerUp:(t,n)=>{var s;if(!c())return;if(!u())return;const r=l(t);if(h.hasMoved()){const t=u();if(!t)return;const n=p(r);if(n){const a=t.cloudyBorderIntensity??0,s=a>0?ue(a,t.strokeWidth,!1):void 0,r={...t,type:e.PdfAnnotationSubtype.SQUARE,created:new Date,id:e.uuidV4(),pageIndex:o,rect:n.data.rect,...void 0!==s&&{rectangleDifferences:{left:s,top:s,right:s,bottom:s}}};i(r)}}else h.onEnd(r);d(null),a(null),h.reset(),null==(s=n.releasePointerCapture)||s.call(n)},onPointerLeave:(t,e)=>{var n;d(null),a(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;d(null),a(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},Re=new Map,Me={annotationType:e.PdfAnnotationSubtype.STAMP,create(t){const{services:n,onCommit:o,onPreview:i,getTool:a,pageSize:s,pageRotation:r}=t;let c=null,d=null;const l=(n,i,c)=>{const d=e.getImageMetadata(i);if(!d||"application/pdf"===d.mimeType)return!1;const l=e.fitSizeWithin(d,s);return((n,i,c,d)=>{var l;const u=a();if(!u)return;const h={origin:{x:n.x-i/2,y:n.y-c/2},size:{width:i,height:c}};let p={...u.defaults,rect:h,type:e.PdfAnnotationSubtype.STAMP,name:u.defaults.name??e.PdfAnnotationName.Image,subject:u.defaults.subject??"Stamp",flags:u.defaults.flags??["print"],pageIndex:t.pageIndex,id:e.uuidV4(),created:new Date};(null==(l=u.behavior)?void 0:l.insertUpright)&&(p=Nt(p,r,!1)),p=Ot(p,s),o(p,d)})(n,(null==c?void 0:c.width)??l.width,(null==c?void 0:c.height)??l.height,{data:i}),!0};return{onHandlerActiveStart:()=>{const t=a(),n=null==t?void 0:t.defaults.imageSrc;if(!n)return;let o=Re.get(n);if(o)o.refs++;else{o={promise:fetch(n).then(t=>t.arrayBuffer()).then(n=>{const o=e.getImageMetadata(n);if(!o||"application/pdf"===o.mimeType)return null;const i=e.fitSizeWithin(o,s),a=t.defaults.imageSize;return{buffer:n,width:(null==a?void 0:a.width)??i.width,height:(null==a?void 0:a.height)??i.height}}).catch(()=>null),refs:1},Re.set(n,o)}o.promise.then(t=>{t&&(c=t.buffer,d={width:t.width,height:t.height})})},onHandlerActiveEnd:()=>{const t=a(),e=null==t?void 0:t.defaults.imageSrc;if(e){const t=Re.get(e);t&&--t.refs<=0&&Re.delete(e)}c=null,d=null,i(null)},onPointerMove:t=>{var n;const o=a();if(!(null==(n=null==o?void 0:o.behavior)?void 0:n.showGhost)||!d||!o.defaults.imageSrc)return;const s={origin:{x:t.x-d.width/2,y:t.y-d.height/2},size:d};i({type:e.PdfAnnotationSubtype.STAMP,bounds:s,data:{rect:s,ghostUrl:o.defaults.imageSrc,pageRotation:r}})},onPointerDown:t=>{const e=a();if(!e)return;const{imageSrc:o,imageSize:s}=e.defaults;o?(i(null),c?l(t,c,s):fetch(o).then(t=>t.arrayBuffer()).then(e=>l(t,e,s))):n.requestFile({accept:"image/png,image/jpeg",onFile:e=>{e.arrayBuffer().then(e=>l(t,e))}})},onPointerLeave:()=>{i(null)}}}},ke={annotationType:e.PdfAnnotationSubtype.CIRCLE,create(n){const{pageIndex:o,onCommit:i,onPreview:a,getTool:s,pageSize:r}=n,[c,d]=yt(null),l=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.height)}),u=()=>{const t=s();return t?{...t.defaults,strokeWidth:t.defaults.strokeWidth??2,strokeColor:t.defaults.strokeColor??"#000000",strokeStyle:t.defaults.strokeStyle??e.PdfAnnotationBorderStyle.SOLID,strokeDashArray:t.defaults.strokeDashArray??[],color:t.defaults.color??"#000000",opacity:t.defaults.opacity??1,flags:t.defaults.flags??["print"]}:null},h=be({threshold:5,getTool:s,onClickDetected:(n,a)=>{const s=u();if(!s)return;const c=a.clickBehavior;if(!(null==c?void 0:c.enabled))return;const{width:d,height:l}=c.defaultSize,h=d/2,p=l/2,g=t.clamp(n.x-h,0,r.width-d),f=t.clamp(n.y-p,0,r.height-l),m=s.strokeWidth,y=s.cloudyBorderIntensity??0,A=y>0?ue(y,m,!0):m/2,x={origin:{x:g-A,y:f-A},size:{width:d+2*A,height:l+2*A}},b={...s,type:e.PdfAnnotationSubtype.CIRCLE,created:new Date,id:e.uuidV4(),pageIndex:o,rect:x,...y>0&&{rectangleDifferences:{left:A,top:A,right:A,bottom:A}}};i(b)}}),p=t=>{const n=c();if(!n)return null;const o=Math.min(n.x,t.x),i=Math.min(n.y,t.y),a=Math.abs(n.x-t.x),s=Math.abs(n.y-t.y),r=u();if(!r)return null;const d=r.strokeWidth,l=r.cloudyBorderIntensity??0,h=l>0?ue(l,d,!0):d/2,p={origin:{x:o-h,y:i-h},size:{width:a+2*h,height:s+2*h}};return{type:e.PdfAnnotationSubtype.CIRCLE,bounds:p,data:{rect:p,...r,...l>0&&{rectangleDifferences:{left:h,top:h,right:h,bottom:h}}}}};return{onPointerDown:(t,e)=>{var n;const o=l(t);d(o),h.onStart(o),a(p(o)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=l(t);h.onMove(e),c()&&h.hasMoved()&&a(p(e))},onPointerUp:(t,n)=>{var s;if(!c())return;if(!u())return;const r=l(t);if(h.hasMoved()){const t=u();if(!t)return;const n=p(r);if(n){const a=t.cloudyBorderIntensity??0,s=a>0?ue(a,t.strokeWidth,!0):void 0,r={...t,type:e.PdfAnnotationSubtype.CIRCLE,flags:["print"],created:new Date,id:e.uuidV4(),pageIndex:o,rect:n.data.rect,...void 0!==s&&{rectangleDifferences:{left:s,top:s,right:s,bottom:s}}};i(r)}}else h.onEnd(r);d(null),a(null),h.reset(),null==(s=n.releasePointerCapture)||s.call(n)},onPointerLeave:(t,e)=>{var n;d(null),a(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;d(null),a(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},Ce={annotationType:e.PdfAnnotationSubtype.LINK,create(n){const{pageIndex:o,onCommit:i,onPreview:a,getTool:s,pageSize:r}=n,[c,d]=yt(null),l=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.height)}),u=()=>{const t=s();return t?{...t.defaults,flags:t.defaults.flags??["print"],strokeWidth:t.defaults.strokeWidth??2,strokeColor:t.defaults.strokeColor??"#0000FF",strokeStyle:t.defaults.strokeStyle??e.PdfAnnotationBorderStyle.UNDERLINE,strokeDashArray:t.defaults.strokeDashArray??[]}:null},h=be({threshold:5,getTool:s,onClickDetected:(n,a)=>{const s=u();if(!s)return;const c=a.clickBehavior;if(!(null==c?void 0:c.enabled))return;const{width:d,height:l}=c.defaultSize,h=d/2,p=l/2,g={origin:{x:t.clamp(n.x-h,0,r.width-d),y:t.clamp(n.y-p,0,r.height-l)},size:{width:d,height:l}},f={...s,type:e.PdfAnnotationSubtype.LINK,target:void 0,created:new Date,id:e.uuidV4(),pageIndex:o,rect:g};i(f)}}),p=t=>{const n=c();if(!n)return null;const o=Math.min(n.x,t.x),i=Math.min(n.y,t.y),a=Math.abs(n.x-t.x),s=Math.abs(n.y-t.y),r=u();if(!r)return null;const d={origin:{x:o,y:i},size:{width:a,height:s}};return{type:e.PdfAnnotationSubtype.LINK,bounds:d,data:{rect:d,strokeColor:r.strokeColor,strokeWidth:r.strokeWidth,strokeStyle:r.strokeStyle,strokeDashArray:r.strokeDashArray}}};return{onPointerDown:(t,e)=>{var n;const o=l(t);d(o),h.onStart(o),a(p(o)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=l(t);h.onMove(e),c()&&h.hasMoved()&&a(p(e))},onPointerUp:(t,n)=>{var s;if(!c())return;const r=u();if(!r)return;const g=l(t);if(h.hasMoved()){const t=p(g);if(t){const n={...r,type:e.PdfAnnotationSubtype.LINK,target:void 0,created:new Date,id:e.uuidV4(),pageIndex:o,rect:t.data.rect};i(n)}}else h.onEnd(g);d(null),a(null),h.reset(),null==(s=n.releasePointerCapture)||s.call(n)},onPointerLeave:(t,e)=>{var n;d(null),a(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;d(null),a(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},De={toolId:"__textMarkup__",handle(t,n,o){const i=t.getTool();if(i)for(const a of n){const n=e.uuidV4();o().then(e=>{var o;t.createAnnotation(a.pageIndex,{...i.defaults,rect:a.rect,segmentRects:a.segmentRects,pageIndex:a.pageIndex,created:new Date,id:n,...null!=e&&{custom:{text:e}}}),(null==(o=i.behavior)?void 0:o.selectAfterCreate)&&t.selectAnnotation(a.pageIndex,n)})}}};function Le(t){const e=t.size.height,n=e/2,o=n;return{origin:{x:t.origin.x+t.size.width-o/2,y:t.origin.y+e/2},size:{width:o,height:n}}}const Ee={toolId:"insertText",handle(t,n,o){const i=t.getTool();if(!i)return;const a=()=>({strokeColor:i.defaults.strokeColor??"#E44234",opacity:i.defaults.opacity??1,flags:i.defaults.flags??["print"]});for(const s of n){const n=s.segmentRects[s.segmentRects.length-1];if(!n)continue;const r=Le(n),c=e.uuidV4(),d=a();o().then(n=>{var o;t.createAnnotation(s.pageIndex,{type:e.PdfAnnotationSubtype.CARET,id:c,pageIndex:s.pageIndex,rect:r,strokeColor:d.strokeColor,opacity:d.opacity,intent:"Insert",rectangleDifferences:{left:.5,top:.5,right:.5,bottom:.5},created:new Date,flags:d.flags,...null!=n&&{custom:{text:n}}}),(null==(o=i.behavior)?void 0:o.selectAfterCreate)&&t.selectAnnotation(s.pageIndex,c)})}}},je={toolId:"replaceText",handle(t,n,o){const i=t.getTool();if(!i)return;const a=()=>({strokeColor:i.defaults.strokeColor??"#E44234",opacity:i.defaults.opacity??1,flags:i.defaults.flags??["print"]});for(const s of n){const n=s.segmentRects[s.segmentRects.length-1];if(!n)continue;const r=Le(n),c=e.uuidV4(),d=e.uuidV4(),l=a();o().then(n=>{var o;t.createAnnotation(s.pageIndex,{type:e.PdfAnnotationSubtype.CARET,id:c,pageIndex:s.pageIndex,rect:r,strokeColor:l.strokeColor,opacity:l.opacity,intent:"Replace",rectangleDifferences:{left:.5,top:.5,right:.5,bottom:.5},created:new Date,flags:l.flags}),t.createAnnotation(s.pageIndex,{type:e.PdfAnnotationSubtype.STRIKEOUT,id:d,pageIndex:s.pageIndex,rect:s.rect,segmentRects:s.segmentRects,strokeColor:l.strokeColor,opacity:l.opacity,intent:"StrikeOutTextEdit",inReplyToId:c,replyType:e.PdfAnnotationReplyType.Group,created:new Date,flags:l.flags,...null!=n&&{custom:{text:n}}}),(null==(o=i.behavior)?void 0:o.selectAfterCreate)&&t.selectAnnotation(s.pageIndex,c)})}}},we=[...[{id:"highlight",name:"Highlight",labelKey:"annotation.highlight",categories:["annotation","markup"],matchScore:t=>t.type===e.PdfAnnotationSubtype.HIGHLIGHT?1:0,interaction:{exclusive:!1,textSelection:!0,isDraggable:!1,isResizable:!1,isRotatable:!1,isGroupDraggable:!1,isGroupResizable:!1},defaults:{type:e.PdfAnnotationSubtype.HIGHLIGHT,strokeColor:"#FFCD45",color:"#FFCD45",opacity:1,blendMode:e.PdfBlendMode.Multiply}},{id:"underline",name:"Underline",labelKey:"annotation.underline",categories:["annotation","markup"],matchScore:t=>t.type===e.PdfAnnotationSubtype.UNDERLINE?1:0,interaction:{exclusive:!1,textSelection:!0,isDraggable:!1,isResizable:!1,isRotatable:!1,isGroupDraggable:!1,isGroupResizable:!1},defaults:{type:e.PdfAnnotationSubtype.UNDERLINE,strokeColor:"#E44234",color:"#E44234",opacity:1}},{id:"strikeout",name:"Strikeout",labelKey:"annotation.strikeout",categories:["annotation","markup"],matchScore:t=>t.type===e.PdfAnnotationSubtype.STRIKEOUT?1:0,interaction:{exclusive:!1,textSelection:!0,isDraggable:!1,isResizable:!1,isRotatable:!1,isGroupDraggable:!1,isGroupResizable:!1},defaults:{type:e.PdfAnnotationSubtype.STRIKEOUT,strokeColor:"#E44234",color:"#E44234",opacity:1}},{id:"squiggly",name:"Squiggly",labelKey:"annotation.squiggly",categories:["annotation","markup"],matchScore:t=>t.type===e.PdfAnnotationSubtype.SQUIGGLY?1:0,interaction:{exclusive:!1,textSelection:!0,isDraggable:!1,isResizable:!1,isRotatable:!1,isGroupDraggable:!1,isGroupResizable:!1},defaults:{type:e.PdfAnnotationSubtype.SQUIGGLY,strokeColor:"#E44234",color:"#E44234",opacity:1}}],...[{id:"insertText",name:"Insert Text",labelKey:"annotation.insertText",categories:["annotation","markup"],matchScore:t=>{var n;return t.type!==e.PdfAnnotationSubtype.CARET?0:(null==(n=t.intent)?void 0:n.includes("Insert"))?2:1},interaction:{exclusive:!1,textSelection:!0,showSelectionRects:!0,isDraggable:!1,isResizable:!1,isRotatable:!1,isGroupDraggable:!1,isGroupResizable:!1},defaults:{type:e.PdfAnnotationSubtype.CARET,strokeColor:"#E44234",opacity:1,intent:"Insert"},selectionHandler:Ee}],...[{id:"replaceText",name:"Replace Text",labelKey:"annotation.replaceText",categories:["annotation","markup"],matchScore:t=>{var n,o;return t.type===e.PdfAnnotationSubtype.STRIKEOUT&&(null==(n=t.intent)?void 0:n.includes("StrikeOutTextEdit"))||t.type===e.PdfAnnotationSubtype.CARET&&(null==(o=t.intent)?void 0:o.includes("Replace"))?2:0},interaction:{exclusive:!1,textSelection:!0,isDraggable:!1,isResizable:!1,isRotatable:!1,isGroupDraggable:!1,isGroupResizable:!1},defaults:{type:e.PdfAnnotationSubtype.STRIKEOUT,strokeColor:"#E44234",opacity:1,intent:"StrikeOutTextEdit"},selectionHandler:je}],...[{id:"ink",name:"Pen",labelKey:"annotation.ink",categories:["annotation","markup"],matchScore:t=>t.type===e.PdfAnnotationSubtype.INK&&"InkHighlight"!==t.intent?5:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!0,lockAspectRatio:!1},defaults:{type:e.PdfAnnotationSubtype.INK,strokeColor:"#E44234",color:"#E44234",opacity:1,strokeWidth:6},behavior:{commitDelay:800},transform:Ut,pointerHandler:At},{id:"inkHighlighter",name:"Ink Highlighter",labelKey:"annotation.inkHighlighter",categories:["annotation","markup"],matchScore:t=>t.type===e.PdfAnnotationSubtype.INK&&"InkHighlight"===t.intent?10:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!0,lockAspectRatio:!1,lockGroupAspectRatio:t=>{const e=((t.rotation??0)%90+90)%90;return e>=6&&e<=84}},defaults:{type:e.PdfAnnotationSubtype.INK,intent:"InkHighlight",strokeColor:"#FFCD45",color:"#FFCD45",opacity:1,strokeWidth:14,blendMode:e.PdfBlendMode.Multiply},behavior:{commitDelay:800,smartLineRecognition:!0,smartLineThreshold:.15},transform:Ut,pointerHandler:At}],...[{id:"circle",name:"Circle",labelKey:"annotation.circle",categories:["annotation","shape"],matchScore:t=>t.type===e.PdfAnnotationSubtype.CIRCLE?1:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!0,lockAspectRatio:!1,lockGroupAspectRatio:t=>{const e=((t.rotation??0)%90+90)%90;return e>=6&&e<=84}},defaults:{type:e.PdfAnnotationSubtype.CIRCLE,color:"transparent",opacity:1,strokeWidth:6,strokeColor:"#E44234",strokeStyle:e.PdfAnnotationBorderStyle.SOLID},clickBehavior:{enabled:!0,defaultSize:{width:100,height:100}},transform:ge,pointerHandler:ke}],...[{id:"square",name:"Square",labelKey:"annotation.square",categories:["annotation","shape"],matchScore:t=>t.type===e.PdfAnnotationSubtype.SQUARE?1:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!0,lockAspectRatio:!1,lockGroupAspectRatio:t=>{const e=((t.rotation??0)%90+90)%90;return e>=6&&e<=84}},defaults:{type:e.PdfAnnotationSubtype.SQUARE,color:"transparent",opacity:1,strokeWidth:6,strokeColor:"#E44234",strokeStyle:e.PdfAnnotationBorderStyle.SOLID},clickBehavior:{enabled:!0,defaultSize:{width:100,height:100}},transform:fe,pointerHandler:Te}],...[{id:"line",name:"Line",labelKey:"annotation.line",categories:["annotation","shape"],matchScore:t=>t.type===e.PdfAnnotationSubtype.LINE&&"LineArrow"!==t.intent?5:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!1,lockAspectRatio:!1,isGroupResizable:!0,lockGroupAspectRatio:t=>{const e=((t.rotation??0)%90+90)%90;return e>=6&&e<=84}},defaults:{type:e.PdfAnnotationSubtype.LINE,color:"transparent",opacity:1,strokeWidth:6,strokeColor:"#E44234"},clickBehavior:{enabled:!0,defaultLength:100,defaultAngle:0},transform:Bt,pointerHandler:Ie},{id:"lineArrow",name:"Arrow",labelKey:"annotation.arrow",categories:["annotation","shape"],matchScore:t=>t.type===e.PdfAnnotationSubtype.LINE&&"LineArrow"===t.intent?10:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!1,lockAspectRatio:!1,isGroupResizable:!0,lockGroupAspectRatio:t=>{const e=((t.rotation??0)%90+90)%90;return e>=6&&e<=84}},defaults:{type:e.PdfAnnotationSubtype.LINE,intent:"LineArrow",color:"transparent",opacity:1,strokeWidth:6,strokeColor:"#E44234",lineEndings:{start:e.PdfAnnotationLineEnding.None,end:e.PdfAnnotationLineEnding.OpenArrow}},clickBehavior:{enabled:!0,defaultLength:100,defaultAngle:0},transform:Bt,pointerHandler:Ie}],...[{id:"polyline",name:"Polyline",labelKey:"annotation.polyline",categories:["annotation","shape"],matchScore:t=>t.type===e.PdfAnnotationSubtype.POLYLINE?1:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!1,lockAspectRatio:!1,isGroupResizable:!0,lockGroupAspectRatio:t=>{const e=((t.rotation??0)%90+90)%90;return e>=6&&e<=84}},defaults:{type:e.PdfAnnotationSubtype.POLYLINE,color:"transparent",opacity:1,strokeWidth:6,strokeColor:"#E44234"},transform:$t,pointerHandler:Pe}],...[{id:"polygon",name:"Polygon",labelKey:"annotation.polygon",categories:["annotation","shape"],matchScore:t=>t.type===e.PdfAnnotationSubtype.POLYGON?1:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!1,lockAspectRatio:!1,isGroupResizable:!0,lockGroupAspectRatio:t=>{const e=((t.rotation??0)%90+90)%90;return e>=6&&e<=84}},defaults:{type:e.PdfAnnotationSubtype.POLYGON,color:"transparent",opacity:1,strokeWidth:6,strokeColor:"#E44234"},transform:pe,pointerHandler:Se}],...[{id:"textComment",name:"Comment",labelKey:"annotation.text",categories:["annotation","markup"],matchScore:t=>t.type!==e.PdfAnnotationSubtype.TEXT||t.inReplyToId?0:1,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!1,isRotatable:!1},defaults:{type:e.PdfAnnotationSubtype.TEXT,strokeColor:"#FFCD45",opacity:1},behavior:{selectAfterCreate:!0},pointerHandler:xe}],...[{id:"freeText",name:"Free Text",labelKey:"annotation.freeText",categories:["annotation","markup"],matchScore:t=>t.type===e.PdfAnnotationSubtype.FREETEXT?1:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!0,lockAspectRatio:!1,lockGroupAspectRatio:t=>{const e=((t.rotation??0)%90+90)%90;return e>=6&&e<=84}},defaults:{type:e.PdfAnnotationSubtype.FREETEXT,contents:"Insert text",fontSize:14,fontColor:"#E44234",fontFamily:e.PdfStandardFont.Helvetica,textAlign:e.PdfTextAlignment.Left,verticalAlign:e.PdfVerticalAlignment.Top,color:"transparent",backgroundColor:"transparent",opacity:1},clickBehavior:{enabled:!0,defaultSize:{width:100,height:20},defaultContent:"Insert text"},behavior:{insertUpright:!0,editAfterCreate:!0,selectAfterCreate:!0},transform:me,pointerHandler:ve}],...[{id:"stamp",name:"Image",labelKey:"annotation.stamp",categories:["annotation","markup"],matchScore:t=>t.type===e.PdfAnnotationSubtype.STAMP?1:0,interaction:{exclusive:!1,cursor:"copy",isDraggable:!0,isResizable:!0,lockAspectRatio:!0,lockGroupAspectRatio:!0},defaults:{type:e.PdfAnnotationSubtype.STAMP},behavior:{insertUpright:!0,useAppearanceStream:!1},transform:ye,pointerHandler:Me}],...[{id:"link",name:"Link",labelKey:"annotation.link",categories:["annotation","markup"],matchScore:t=>t.type===e.PdfAnnotationSubtype.LINK?1:0,interaction:{exclusive:!1,cursor:"crosshair",isDraggable:!0,isResizable:!0,isRotatable:!1},defaults:{type:e.PdfAnnotationSubtype.LINK,strokeColor:"#0000FF",strokeWidth:2,strokeStyle:e.PdfAnnotationBorderStyle.UNDERLINE},clickBehavior:{enabled:!0,defaultSize:{width:100,height:20}},pointerHandler:Ce}]],ze=["#E44234","#FF8D00","#FFCD45","#5CC96E","#25D2D1","#597CE2","#C544CE","#7D2E25","#000000","#FFFFFF"],Ne=t=>1===t.length?t[0]:null,Oe=t=>({pages:{},byUid:{},selectedUids:[],selectedUid:null,activeToolId:null,hasPendingChanges:!1,locked:(null==t?void 0:t.locked)??{type:w.None}}),Ue=(t,e,n)=>{const o=t.byUid[e];return o?{...t,byUid:{...t.byUid,[e]:{...o,commitState:"synced"===o.commitState||"moved"===o.commitState?"dirty":o.commitState,object:{...o.object,...n},dictMode:!0}},hasPendingChanges:!0}:t},Be=(t,e,n)=>{const o=t.byUid[e];return o?{...t,byUid:{...t.byUid,[e]:{...o,commitState:"synced"===o.commitState?"moved":o.commitState,object:{...o.object,...n}}},hasPendingChanges:!0}:t},$e=t=>{const e=new Map;we.forEach(t=>e.set(t.id,t));const n=new Map(e);(t.tools||[]).forEach(t=>{const o=e.get(t.id);o?n.set(t.id,{...o,...t,defaults:{...o.defaults,...t.defaults},interaction:{...o.interaction,...t.interaction},behavior:{...o.behavior,...t.behavior},...o.clickBehavior||t.clickBehavior?{clickBehavior:{...o.clickBehavior,...t.clickBehavior}}:{}}):n.set(t.id,t)});return{documents:{},activeDocumentId:null,tools:Array.from(n.values()).map(e=>{var n,o,i;return{...e,behavior:{...e.behavior,deactivateToolAfterCreate:(null==(n=e.behavior)?void 0:n.deactivateToolAfterCreate)??t.deactivateToolAfterCreate??!1,selectAfterCreate:(null==(o=e.behavior)?void 0:o.selectAfterCreate)??t.selectAfterCreate??!0,editAfterCreate:(null==(i=e.behavior)?void 0:i.editAfterCreate)??t.editAfterCreate??!1}}}),colorPresets:t.colorPresets??ze}},Ge=class n extends t.BasePlugin{constructor(e,n,o){var i,a,s,r;super(e,n),this.ANNOTATION_HISTORY_TOPIC="annotations",this.state$=t.createScopedEmitter((t,e)=>({documentId:t,state:e})),this.pendingContexts=new Map,this.isInitialLoadComplete=new Map,this.importQueue=new Map,this.commitInProgress=new Map,this.activeTool$=t.createScopedEmitter((t,e)=>({documentId:t,tool:e})),this.events$=t.createScopedEmitter((t,e)=>e,{cache:!1}),this.toolsChange$=t.createBehaviorEmitter(),this.appearanceCache=new Map,this.unifiedDragStates=new Map,this.unifiedDrag$=t.createBehaviorEmitter(),this.unifiedResizeStates=new Map,this.unifiedResize$=t.createBehaviorEmitter(),this.unifiedRotateStates=new Map,this.unifiedRotate$=t.createBehaviorEmitter(),this.navigate$=t.createScopedEmitter((t,e)=>e,{cache:!1}),this.config=o,this.selection=(null==(i=n.getPlugin("selection"))?void 0:i.provides())??null,this.history=(null==(a=n.getPlugin("history"))?void 0:a.provides())??null,this.scroll=(null==(s=n.getPlugin("scroll"))?void 0:s.provides())??null,this.interactionManager=(null==(r=n.getPlugin("interaction-manager"))?void 0:r.provides())??null}onDocumentLoadingStarted(t){this.dispatch(function(t,e){return{type:i,payload:{documentId:t,state:e}}}(t,Oe(this.config))),this.pendingContexts.set(t,new Map),this.isInitialLoadComplete.set(t,!1),this.importQueue.set(t,[]),this.logger.debug("AnnotationPlugin","DocumentOpened",`Initialized annotation state for document: ${t}`)}onDocumentLoaded(t){const e=this.getCoreDocument(t);if((null==e?void 0:e.document)&&this.getAllAnnotations(t,e.document),this.selection)for(const n of this.state.tools)n.interaction.textSelection&&this.selection.enableForMode(n.interaction.mode??n.id,{showSelectionRects:n.interaction.showSelectionRects??!1,enableSelection:!0,enableMarquee:!1})}onDocumentClosed(t){this.dispatch(function(t){return{type:a,payload:t}}(t)),this.pendingContexts.delete(t),this.isInitialLoadComplete.delete(t),this.importQueue.delete(t),this.appearanceCache.delete(t),this.state$.clearScope(t),this.activeTool$.clearScope(t),this.events$.clearScope(t),this.navigate$.clearScope(t),this.logger.debug("AnnotationPlugin","DocumentClosed",`Cleaned up annotation state for document: ${t}`)}async initialize(){var t,n,o;this.state.tools.forEach(t=>this.registerInteractionForTool(t)),this.history&&this.history.onHistoryChange(t=>{t.topic===this.ANNOTATION_HISTORY_TOPIC&&!1!==this.config.autoCommit&&this.commit(t.documentId)}),null==(t=this.interactionManager)||t.onModeChange(t=>{var e,n;const o=(null==(e=this.state.tools.find(e=>(e.interaction.mode??e.id)===t.activeMode))?void 0:e.id)??null;o!==((null==(n=this.state.documents[t.documentId])?void 0:n.activeToolId)??null)&&t.documentId&&this.dispatch(k(t.documentId,o))}),null==(n=this.selection)||n.onMarqueeEnd(({documentId:t,pageIndex:e,rect:n,modeId:o})=>{if("pointerMode"!==o)return;const i=this.state.documents[t];if(!i)return;const a=(i.pages[e]??[]).map(t=>i.byUid[t]).filter(t=>void 0!==t).filter(t=>tt(t)).filter(t=>z(n,t.object.rect)).filter(e=>!this.isAnnotationLocked(e.object,t)).map(t=>t.object.id);if(a.length>0){const e=new Set;for(const n of a)if(this.isInGroupMethod(n,t)){const o=this.getGroupMembersMethod(n,t);for(const t of o)e.add(t.object.id)}else e.add(n);this.setSelectionMethod([...e],t)}}),null==(o=this.selection)||o.onEndSelection(({documentId:t})=>{var n,o,i,a;if(!this.checkPermission(t,e.PdfPermissionFlag.ModifyAnnotations))return;const s=this.getActiveTool(t);if(!s||!s.interaction.textSelection)return;const r=null==(n=this.selection)?void 0:n.getFormattedSelection(),c=null==(o=this.selection)?void 0:o.getSelectedText();if(!r||!c)return;const d={toolId:s.id,documentId:t,getTool:()=>this.getActiveTool(t)??null,createAnnotation:(e,n)=>this.createAnnotation(e,n,void 0,t),selectAnnotation:(e,n)=>this.selectAnnotation(e,n,t)};(s.selectionHandler??De).handle(d,r,()=>new Promise(t=>{c.wait(e=>t(e.join("\n")),()=>t(void 0))})),(null==(i=s.behavior)?void 0:i.deactivateToolAfterCreate)&&this.setActiveTool(null,t),null==(a=this.selection)||a.clear()})}registerInteractionForTool(t){var e;null==(e=this.interactionManager)||e.registerMode({id:t.interaction.mode??t.id,scope:"page",exclusive:t.interaction.exclusive,cursor:t.interaction.cursor})}buildCapability(){return{getActiveTool:()=>this.getActiveTool(),setActiveTool:(t,e)=>this.setActiveTool(t,void 0,e),getState:()=>this.getDocumentState(),getPageAnnotations:t=>this.getPageAnnotations(t),getSelectedAnnotation:()=>this.getSelectedAnnotation(),getSelectedAnnotations:()=>this.getSelectedAnnotationsMethod(),getSelectedAnnotationIds:()=>this.getSelectedAnnotationIdsMethod(),getAnnotationById:t=>this.getAnnotationById(t),getAnnotations:t=>this.getAnnotationsMethod(t),selectAnnotation:(t,e)=>this.selectAnnotation(t,e),toggleSelection:(t,e)=>this.toggleSelectionMethod(t,e),addToSelection:(t,e)=>this.addToSelectionMethod(t,e),removeFromSelection:t=>this.removeFromSelectionMethod(t),setSelection:t=>this.setSelectionMethod(t),deselectAnnotation:()=>this.deselectAnnotation(),importAnnotations:t=>this.importAnnotations(t),exportAnnotations:(t,e)=>this.exportAnnotationsMethod(t,e),createAnnotation:(t,e,n)=>this.createAnnotation(t,e,n),updateAnnotation:(t,e,n)=>this.updateAnnotation(t,e,n),updateAnnotations:t=>this.updateAnnotationsMethod(t),moveAnnotation:(t,e,n,o,i)=>this.moveAnnotationMethod(t,e,n,o,i),deleteAnnotation:(t,e)=>this.deleteAnnotation(t,e),deleteAnnotations:(t,e)=>this.deleteAnnotationsMethod(t,e),deleteAllAnnotations:t=>this.deleteAllAnnotationsMethod(t),purgeAnnotation:(t,e,n)=>this.purgeAnnotationMethod(t,e,n),renderAnnotation:t=>this.renderAnnotation(t),getPageAppearances:(t,e,n)=>this.getPageAppearances(t,e,n),invalidatePageAppearances:(t,e)=>this.invalidatePageAppearances(t,e),commit:()=>this.commit(),getAttachedLinks:(t,e)=>this.getAttachedLinksMethod(t,e),hasAttachedLinks:(t,e)=>this.hasAttachedLinksMethod(t,e),deleteAttachedLinks:(t,e)=>this.deleteAttachedLinksMethod(t,e),groupAnnotations:t=>this.groupAnnotationsMethod(t),ungroupAnnotations:(t,e)=>this.ungroupAnnotationsMethod(t,e),getGroupMembers:(t,e)=>this.getGroupMembersMethod(t,e),isInGroup:(t,e)=>this.isInGroupMethod(t,e),setLocked:(t,e)=>this.setLocked(t,e),getLocked:t=>this.getLocked(t),isAnnotationLocked:(t,e)=>this.isAnnotationLocked(t,e),isCategoryLocked:(t,e)=>this.isCategoryLockedMethod(t,e),isToolLocked:(t,e)=>this.isToolLockedMethod(t,e),syncAnnotationObject:(t,e,n)=>this.syncAnnotationObject(t,e,n),navigateTarget:(t,e)=>this.navigateTargetMethod(t,e),forDocument:t=>this.createAnnotationScope(t),getTools:()=>this.state.tools,getTool:t=>this.getTool(t),addTool:t=>{this.dispatch((t=>({type:P,payload:t}))(t)),this.registerInteractionForTool(t)},findToolForAnnotation:t=>this.findToolForAnnotation(t),setToolDefaults:(t,e)=>this.dispatch(((t,e)=>({type:I,payload:{toolId:t,patch:e}}))(t,e)),getColorPresets:()=>[...this.state.colorPresets],addColorPreset:t=>this.dispatch({type:v,payload:t}),transformAnnotation:(t,e)=>this.transformAnnotation(t,e),onStateChange:this.state$.onGlobal,onActiveToolChange:this.activeTool$.onGlobal,onAnnotationEvent:this.events$.onGlobal,onToolsChange:this.toolsChange$.on,onNavigate:this.navigate$.onGlobal}}createAnnotationScope(t){return{getState:()=>this.getDocumentState(t),getPageAnnotations:e=>this.getPageAnnotations(e,t),getSelectedAnnotation:()=>this.getSelectedAnnotation(t),getSelectedAnnotations:()=>this.getSelectedAnnotationsMethod(t),getSelectedAnnotationIds:()=>this.getSelectedAnnotationIdsMethod(t),getAnnotationById:e=>this.getAnnotationById(e,t),getAnnotations:e=>this.getAnnotationsMethod(e,t),selectAnnotation:(e,n)=>this.selectAnnotation(e,n,t),toggleSelection:(e,n)=>this.toggleSelectionMethod(e,n,t),addToSelection:(e,n)=>this.addToSelectionMethod(e,n,t),removeFromSelection:e=>this.removeFromSelectionMethod(e,t),setSelection:e=>this.setSelectionMethod(e,t),deselectAnnotation:()=>this.deselectAnnotation(t),getActiveTool:()=>this.getActiveTool(t),setActiveTool:(e,n)=>this.setActiveTool(e,t,n),findToolForAnnotation:t=>this.findToolForAnnotation(t),importAnnotations:e=>this.importAnnotations(e,t),exportAnnotations:e=>this.exportAnnotationsMethod(e,t),createAnnotation:(e,n,o)=>this.createAnnotation(e,n,o,t),updateAnnotation:(e,n,o)=>this.updateAnnotation(e,n,o,t),updateAnnotations:e=>this.updateAnnotationsMethod(e,t),moveAnnotation:(e,n,o,i)=>this.moveAnnotationMethod(e,n,o,i,t),deleteAnnotation:(e,n)=>this.deleteAnnotation(e,n,t),deleteAnnotations:e=>this.deleteAnnotationsMethod(e,t),deleteAllAnnotations:()=>this.deleteAllAnnotationsMethod(t),purgeAnnotation:(e,n)=>this.purgeAnnotationMethod(e,n,t),renderAnnotation:e=>this.renderAnnotation(e,t),getPageAppearances:(e,n)=>this.getPageAppearances(e,n,t),invalidatePageAppearances:e=>this.invalidatePageAppearances(e,t),commit:()=>this.commit(t),getAttachedLinks:e=>this.getAttachedLinksMethod(e,t),hasAttachedLinks:e=>this.hasAttachedLinksMethod(e,t),deleteAttachedLinks:e=>this.deleteAttachedLinksMethod(e,t),groupAnnotations:()=>this.groupAnnotationsMethod(t),ungroupAnnotations:e=>this.ungroupAnnotationsMethod(e,t),getGroupMembers:e=>this.getGroupMembersMethod(e,t),isInGroup:e=>this.isInGroupMethod(e,t),getGroupingAction:()=>this.getGroupingActionMethod(t),setLocked:e=>this.setLocked(e,t),getLocked:()=>this.getLocked(t),isAnnotationLocked:e=>this.isAnnotationLocked(e,t),isCategoryLocked:e=>this.isCategoryLockedMethod(e,t),isToolLocked:e=>this.isToolLockedMethod(e,t),syncAnnotationObject:(e,n)=>this.syncAnnotationObject(e,n,t),navigateTarget:e=>this.navigateTargetMethod(e,t),onStateChange:this.state$.forScope(t),onAnnotationEvent:this.events$.forScope(t),onActiveToolChange:this.activeTool$.forScope(t),onNavigate:this.navigate$.forScope(t)}}onStoreUpdated(t,e){for(const n in e.documents){const o=t.documents[n],i=e.documents[n];o!==i&&(this.state$.emit(n,i),o&&o.activeToolId!==i.activeToolId&&this.activeTool$.emit(n,this.getActiveTool(n)),(null==o?void 0:o.selectedUids)!==i.selectedUids&&this.updateAnnotationSelectionActivity(n,i))}if(t.tools!==e.tools){for(const t in e.documents)this.activeTool$.emit(t,this.getActiveTool(t));this.toolsChange$.emit({tools:e.tools})}}transformAnnotation(t,e){const n={type:e.type,changes:e.changes,metadata:e.metadata},o=this.findToolForAnnotation(t);return(null==o?void 0:o.transform)?o.transform(t,n):n.changes}registerPageHandlers(t,e,o,i){var a,s;const r=this.getCoreDocument(t),c=null==(a=null==r?void 0:r.document)?void 0:a.pages[e];if(!c)return()=>{};if(!this.interactionManager)return()=>{};const d=[],l=((c.rotation??0)+((null==r?void 0:r.rotation)??0))%4;for(const u of this.state.tools){const a=u.pointerHandler??((null==(s=u.defaults)?void 0:s.type)?n.defaultHandlerFactories.get(u.defaults.type):void 0);if(!a)continue;const r={pageIndex:e,pageSize:c.size,pageRotation:l,scale:o,services:i.services,onPreview:t=>i.onPreview(u.id,t),onCommit:(n,o)=>{var i,a,s;const r=(null==(i=u.behavior)?void 0:i.editAfterCreate)??!1;this.createAnnotation(e,n,o,t,{editAfterCreate:r}),(null==(a=u.behavior)?void 0:a.deactivateToolAfterCreate)&&this.setActiveTool(null,t),((null==(s=u.behavior)?void 0:s.selectAfterCreate)||r)&&this.selectAnnotation(e,n.id,t)},getTool:()=>this.state.tools.find(t=>t.id===u.id),getToolContext:()=>{var e;return null==(e=this.state.documents[t])?void 0:e.activeToolContext}},h=this.interactionManager.registerHandlers({documentId:t,modeId:u.interaction.mode??u.id,handlers:a.create(r),pageIndex:e});d.push(h)}return()=>d.forEach(t=>t())}getDocumentState(t){const e=t??this.getActiveDocumentId(),n=this.state.documents[e];if(!n)throw new Error(`Annotation state not found for document: ${e}`);return n}getAllAnnotations(t,n){this.engine.getAllAnnotations(n).wait(e=>{this.dispatch(((t,e)=>({type:s,payload:{documentId:t,annotations:e}}))(t,e)),this.isInitialLoadComplete.set(t,!0);const n=this.importQueue.get(t);n&&n.length>0&&this.processImportQueue(t),this.events$.emit(t,{type:"loaded",documentId:t,total:Object.values(e).reduce((t,e)=>t+e.length,0)})},e.ignore)}getPageAnnotations(t,n){const{pageIndex:o}=t,i=n??this.getActiveDocumentId(),a=this.getCoreDocument(i),s=null==a?void 0:a.document;if(!s)return e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Document not found"});const r=s.pages.find(t=>t.index===o);return r?this.engine.getPageAnnotations(s,r):e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Page not found"})}getSelectedAnnotation(t){return at(this.getDocumentState(t))}getAnnotationById(t,e){const n=this.getDocumentState(e);return ct(n,t)}getAnnotationsMethod(t,e){const n=this.getDocumentState(e);return void 0!==(null==t?void 0:t.pageIndex)?it(n,t.pageIndex).filter(t=>void 0!==t):Object.values(n.byUid)}renderAnnotation({pageIndex:t,annotation:n,options:o},i){const a=i??this.getActiveDocumentId(),s=this.getCoreDocument(a),r=null==s?void 0:s.document;if(!r)return e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Document not found"});const c=r.pages.find(e=>e.index===t);return c?this.engine.renderPageAnnotation(r,c,n,o):e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Page not found"})}getPageAppearances(t,n,o){const i=o??this.getActiveDocumentId(),a=this.getCoreDocument(i),s=null==a?void 0:a.document;if(!s)return e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Document not found"});const r=s.pages.find(e=>e.index===t);if(!r)return e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Page not found"});let c=this.appearanceCache.get(i);c||(c=new Map,this.appearanceCache.set(i,c));const d=c.get(t);if(d&&!n){const t=new e.Task;return t.resolve(d),t}const l=this.engine.renderPageAnnotations(s,r,n),u=new e.Task;return l.wait(e=>{c.set(t,e),u.resolve(e)},t=>{u.fail(t)}),u}invalidatePageAppearances(t,e){const n=e??this.getActiveDocumentId(),o=this.appearanceCache.get(n);o&&o.delete(t)}invalidateAnnotationAppearance(t,e,n){const o=n??this.getActiveDocumentId(),i=this.appearanceCache.get(o);if(!i)return;const a=i.get(e);a&&delete a[t]}exportAnnotationsMethod(t,n){const o=n??this.getActiveDocumentId(),i=this.getDocumentState(o),a=this.getCoreDocument(o),s=null==a?void 0:a.document;if(!s)return e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Document not found"});const r=void 0!==(null==t?void 0:t.pageIndex)?[t.pageIndex]:Object.keys(i.pages).map(Number),c=[];for(const g of r){const t=i.pages[g]??[];for(const n of t){const t=i.byUid[n];t&&"deleted"!==t.commitState&&c.push({annotation:t.object,pageIndex:g,isStamp:t.object.type===e.PdfAnnotationSubtype.STAMP})}}const d=c.filter(t=>t.isStamp);if(0===d.length)return e.PdfTaskHelper.resolve(c.map(t=>({annotation:t.annotation})));const l=e.PdfTaskHelper.create(),u=new Map;let h=d.length,p=!1;for(const e of d){const t=s.pages.find(t=>t.index===e.pageIndex);if(!t){h--;continue}this.engine.exportAnnotationAppearanceAsPdf(s,t,e.annotation).wait(t=>{p||(u.set(e.annotation.id,t),h--,0===h&&l.resolve(c.map(t=>({annotation:t.annotation,...u.has(t.annotation.id)?{ctx:{data:u.get(t.annotation.id),mimeType:"application/pdf"}}:{}}))))},t=>{p||(p=!0,l.reject(t.reason))})}return 0===h&&l.resolve(c.map(t=>({annotation:t.annotation}))),l}importAnnotations(t,e){const n=e??this.getActiveDocumentId();if(!this.isInitialLoadComplete.get(n)){const e=this.importQueue.get(n)||[];return e.push(...t),void this.importQueue.set(n,e)}this.processImportItems(n,t)}processImportQueue(t){const e=this.importQueue.get(t);if(!e||0===e.length)return;const n=[...e];this.importQueue.set(t,[]),this.processImportItems(t,n)}processImportItems(t,e){const n=this.pendingContexts.get(t);if(n){for(const o of e){const{annotation:e,ctx:i}=o,a=e.pageIndex,s=e.id;this.dispatch(C(t,a,e)),i&&n.set(s,i)}!1!==this.config.autoCommit&&this.commit(t)}}createAnnotation(t,n,o,i,a){const s=i??this.getActiveDocumentId();if(!this.checkPermission(s,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","CreateAnnotation",`Cannot create annotation: document ${s} lacks ModifyAnnotations permission`);const r=n.id,c=this.pendingContexts.get(s);if(!c)return;const d={...n,author:n.author??this.config.annotationAuthor},l=null==a?void 0:a.editAfterCreate,u=()=>{this.dispatch(C(s,t,d)),o&&c.set(r,o),this.events$.emit(s,{type:"create",documentId:s,annotation:d,pageIndex:t,ctx:o,committed:!1,editAfterCreate:l})};if(!this.history)return u(),void(this.config.autoCommit&&this.commit(s));const h={execute:u,undo:()=>{c.delete(r),this.dispatch(S(s)),this.dispatch(E(s,t,r)),this.events$.emit(s,{type:"delete",documentId:s,annotation:d,pageIndex:t,committed:!1})},metadata:{annotationIds:[r]}};this.history.forDocument(s).register(h,this.ANNOTATION_HISTORY_TOPIC)}buildPatch(t,e){return"rect"in e?e:this.transformAnnotation(t,{type:"property-update",changes:e})}updateAnnotation(t,n,o,i){const a=i??this.getActiveDocumentId();if(!this.checkPermission(a,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","UpdateAnnotation",`Cannot update annotation: document ${a} lacks ModifyAnnotations permission`);const s=this.getDocumentState(a).byUid[n].object,r=this.buildPatch(s,{...o,author:o.author??this.config.annotationAuthor}),c=()=>{this.dispatch(D(a,t,n,r)),this.events$.emit(a,{type:"update",documentId:a,annotation:s,pageIndex:t,patch:r,committed:!1})};if(!this.history)return c(),void(!1!==this.config.autoCommit&&this.commit(a));const d=Object.fromEntries(Object.keys(o).map(t=>[t,s[t]])),l={execute:c,undo:()=>{this.dispatch(D(a,t,n,d)),this.events$.emit(a,{type:"update",documentId:a,annotation:s,pageIndex:t,patch:d,committed:!1})},metadata:{annotationIds:[n]}};this.history.forDocument(a).register(l,this.ANNOTATION_HISTORY_TOPIC)}deleteAnnotation(t,n,o){var i;const a=o??this.getActiveDocumentId();if(!this.checkPermission(a,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","DeleteAnnotation",`Cannot delete annotation: document ${a} lacks ModifyAnnotations permission`);const s=this.getDocumentState(a),r=null==(i=s.byUid[n])?void 0:i.object;if(!r)return;const c=lt(s,n),d=c.map(t=>{var e;return null==(e=s.byUid[t.id])?void 0:e.object}).filter(t=>void 0!==t),l=()=>{var e;for(const t of c){const n=null==(e=s.byUid[t.id])?void 0:e.object;n&&(this.dispatch(E(a,t.pageIndex,t.id)),this.events$.emit(a,{type:"delete",documentId:a,annotation:n,pageIndex:t.pageIndex,committed:!1}))}this.dispatch(S(a)),this.dispatch(E(a,t,n)),this.events$.emit(a,{type:"delete",documentId:a,annotation:r,pageIndex:t,committed:!1})};if(!this.history)return l(),void(!1!==this.config.autoCommit&&this.commit(a));const u={execute:l,undo:()=>{this.dispatch(C(a,t,r)),this.events$.emit(a,{type:"create",documentId:a,annotation:r,pageIndex:t,committed:!1});for(const t of d)this.dispatch(C(a,t.pageIndex,t)),this.events$.emit(a,{type:"create",documentId:a,annotation:t,pageIndex:t.pageIndex,committed:!1})},metadata:{annotationIds:[n,...c.map(t=>t.id)]}};this.history.forDocument(a).register(u,this.ANNOTATION_HISTORY_TOPIC)}deleteAnnotationsMethod(t,e){for(const{pageIndex:n,id:o}of t)this.deleteAnnotation(n,o,e)}deleteAllAnnotationsMethod(t){const e=t??this.getActiveDocumentId(),n=this.getDocumentState(e),o=[];for(const[i,a]of Object.entries(n.pages))for(const t of a){const e=n.byUid[t];e&&o.push({pageIndex:Number(i),id:e.object.id})}o.length>0&&this.deleteAnnotationsMethod(o,e)}purgeAnnotationMethod(t,e,n){const o=n??this.getActiveDocumentId();this.dispatch(j(o,t,e))}selectAnnotation(t,e,n){const o=n??this.getActiveDocumentId(),i=this.getAnnotationById(e,o);if(!i||!this.isAnnotationLocked(i.object,o))if(this.isInGroupMethod(e,o)){const t=this.getGroupMembersMethod(e,o).map(t=>t.object.id);this.dispatch(M(o,t))}else this.dispatch(((t,e,n)=>({type:r,payload:{documentId:t,pageIndex:e,id:n}}))(o,t,e))}deselectAnnotation(t){const e=t??this.getActiveDocumentId();this.dispatch(S(e))}updateAnnotationSelectionActivity(t,e){var n,o;if(0===e.selectedUids.length)return void(null==(n=this.interactionManager)||n.releasePageActivity(t,"annotation-selection"));const i=e.selectedUids[0],a=e.byUid[i];a&&(null==(o=this.interactionManager)||o.claimPageActivity(t,"annotation-selection",a.object.pageIndex))}getSelectedAnnotationsMethod(t){return st(this.getDocumentState(t))}getSelectedAnnotationIdsMethod(t){return rt(this.getDocumentState(t))}toggleSelectionMethod(t,e,n){const o=n??this.getActiveDocumentId(),i=this.getAnnotationById(e,o);if(i&&this.isAnnotationLocked(i.object,o))return;const a=this.getDocumentState(o);if(a.selectedUids.includes(e))this.dispatch(R(o,e));else if(this.isInGroupMethod(e,o)){const t=this.getGroupMembersMethod(e,o);for(const e of t)a.selectedUids.includes(e.object.id)||this.dispatch(T(o,e.object.pageIndex,e.object.id))}else this.dispatch(T(o,t,e))}addToSelectionMethod(t,e,n){const o=n??this.getActiveDocumentId(),i=this.getAnnotationById(e,o);i&&this.isAnnotationLocked(i.object,o)||this.dispatch(T(o,t,e))}removeFromSelectionMethod(t,e){const n=e??this.getActiveDocumentId();this.dispatch(R(n,t))}setSelectionMethod(t,e){const n=e??this.getActiveDocumentId(),o=this.getDocumentState(n),i=t.filter(t=>{const e=o.byUid[t];return!e||!this.isAnnotationLocked(e.object,n)});this.dispatch(M(n,i))}getAttachedLinksMethod(t,e){return ht(this.getDocumentState(e),t)}hasAttachedLinksMethod(t,e){return this.getAttachedLinksMethod(t,e).length>0}deleteAttachedLinksMethod(t,e){const n=this.getAttachedLinksMethod(t,e);for(const o of n)this.deleteAnnotation(o.object.pageIndex,o.object.id,e)}groupAnnotationsMethod(t){const n=t??this.getActiveDocumentId();if(!this.checkPermission(n,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","GroupAnnotations",`Cannot group annotations: document ${n} lacks ModifyAnnotations permission`);const o=this.getSelectedAnnotationsMethod(n);if(o.length<2)return void this.logger.debug("AnnotationPlugin","GroupAnnotations","Need at least 2 annotations to group");const i=o[0],a=o.slice(1).map(t=>({pageIndex:t.object.pageIndex,id:t.object.id,patch:{inReplyToId:i.object.id,replyType:e.PdfAnnotationReplyType.Group}}));this.updateAnnotationsMethod(a,n)}ungroupAnnotationsMethod(t,n){const o=n??this.getActiveDocumentId();if(!this.checkPermission(o,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","UngroupAnnotations",`Cannot ungroup annotations: document ${o} lacks ModifyAnnotations permission`);const i=this.getGroupMembersMethod(t,o).filter(t=>t.object.inReplyToId&&t.object.replyType===e.PdfAnnotationReplyType.Group).map(t=>({pageIndex:t.object.pageIndex,id:t.object.id,patch:{inReplyToId:void 0,replyType:void 0}}));i.length>0&&this.updateAnnotationsMethod(i,o)}getGroupMembersMethod(t,e){return gt(this.getDocumentState(e),t)}isInGroupMethod(t,e){return ft(this.getDocumentState(e),t)}getGroupingActionMethod(t){return mt(this.getDocumentState(t))}computeCombinedConstraints(t){let e=1/0,n=1/0,o=1/0,i=1/0;for(const a of t){const t=a.rect.origin.y,s=a.pageSize.height-(a.rect.origin.y+a.rect.size.height),r=a.rect.origin.x,c=a.pageSize.width-(a.rect.origin.x+a.rect.size.width);e=Math.min(e,t),n=Math.min(n,s),o=Math.min(o,r),i=Math.min(i,c)}return isFinite(e)||(e=0),isFinite(n)||(n=0),isFinite(o)||(o=0),isFinite(i)||(i=0),{maxUp:e,maxDown:n,maxLeft:o,maxRight:i}}clampDelta(t,e){return{x:Math.max(-e.maxLeft,Math.min(e.maxRight,t.x)),y:Math.max(-e.maxUp,Math.min(e.maxDown,t.y))}}startDrag(t,e){const{annotationIds:n,pageSize:o}=e,i=[];for(const d of n){const e=this.getAttachedLinksMethod(d,t);for(const t of e)i.includes(t.object.id)||i.push(t.object.id)}const a=[...n,...i],s=new Map,r=[];for(const d of a){const e=this.getAnnotationById(d,t);e&&(s.set(d,{...e.object.rect}),r.push({id:d,rect:e.object.rect,pageIndex:e.object.pageIndex,pageSize:o}))}const c={documentId:t,isDragging:!0,primaryIds:n,attachedLinkIds:i,allParticipantIds:a,originalRects:s,delta:{x:0,y:0},combinedConstraints:this.computeCombinedConstraints(r)};this.unifiedDragStates.set(t,c),this.unifiedDrag$.emit({documentId:t,type:"start",state:c,previewPatches:{}})}computeDragPreviewPatches(t,e){const n={};for(const o of t.allParticipantIds){const i=this.getAnnotationById(o,e);if(!i)continue;const a=t.originalRects.get(o);if(!a)continue;const s={...a,origin:{x:a.origin.x+t.delta.x,y:a.origin.y+t.delta.y}};n[o]=this.transformAnnotation(i.object,{type:"move",changes:{rect:s}})}return n}updateDrag(t,e){const n=this.unifiedDragStates.get(t);if(!(null==n?void 0:n.isDragging))return{x:0,y:0};const o=this.clampDelta(e,n.combinedConstraints),i={...n,delta:o};this.unifiedDragStates.set(t,i);const a=this.computeDragPreviewPatches(i,t);return this.unifiedDrag$.emit({documentId:t,type:"update",state:i,previewPatches:a}),o}commitDrag(t){const e=this.unifiedDragStates.get(t);if(!e)return;const n=e.delta;if(0!==n.x||0!==n.y){const o=[];for(const i of e.allParticipantIds){const a=this.getAnnotationById(i,t);if(!a)continue;const s=e.originalRects.get(i)??a.object.rect,r={...s,origin:{x:s.origin.x+n.x,y:s.origin.y+n.y}},c=this.transformAnnotation(a.object,{type:"move",changes:{rect:r}});o.push({pageIndex:a.object.pageIndex,id:i,patch:c})}o.length>0&&this.moveAnnotationsMethod(o,t)}const o=this.computeDragPreviewPatches(e,t);this.unifiedDrag$.emit({documentId:t,type:"end",state:{...e,isDragging:!1},previewPatches:o}),this.unifiedDragStates.delete(t)}cancelDrag(t){const e=this.unifiedDragStates.get(t);e&&(this.unifiedDrag$.emit({documentId:t,type:"cancel",state:{...e,isDragging:!1,delta:{x:0,y:0}},previewPatches:{}}),this.unifiedDragStates.delete(t))}getDragState(t){return this.unifiedDragStates.get(t)??null}get onDragChange(){return this.unifiedDrag$.on}computeUnifiedGroupBoundingBox(t){if(0===t.length)return{origin:{x:0,y:0},size:{width:0,height:0}};let e=1/0,n=1/0,o=-1/0,i=-1/0;for(const a of t)e=Math.min(e,a.origin.x),n=Math.min(n,a.origin.y),o=Math.max(o,a.origin.x+a.size.width),i=Math.max(i,a.origin.y+a.size.height);return{origin:{x:e,y:n},size:{width:o-e,height:i-n}}}computeUnifiedRelativePositions(t,e){return t.map(t=>({id:t.id,originalRect:t.rect,originalUnrotatedRect:t.unrotatedRect,pageIndex:t.pageIndex,isAttachedLink:t.isAttachedLink,parentId:t.parentId,relativeX:e.size.width>0?(t.rect.origin.x-e.origin.x)/e.size.width:0,relativeY:e.size.height>0?(t.rect.origin.y-e.origin.y)/e.size.height:0,relativeWidth:e.size.width>0?t.rect.size.width/e.size.width:1,relativeHeight:e.size.height>0?t.rect.size.height/e.size.height:1}))}computeUnifiedResizedRects(t,e,n=10){const o=new Map;for(const i of t){const t=Math.max(n,i.relativeWidth*e.size.width),a=Math.max(n,i.relativeHeight*e.size.height);o.set(i.id,{origin:{x:e.origin.x+i.relativeX*e.size.width,y:e.origin.y+i.relativeY*e.size.height},size:{width:t,height:a}})}return o}computeResizePreviewPatches(t,e){const n={},o=this.unifiedResizeStates.get(e),i=o?new Map(o.participatingAnnotations.map(t=>[t.id,t])):void 0;for(const[a,s]of t){const t=this.getAnnotationById(a,e);if(!t)continue;let r=s;const c=null==i?void 0:i.get(a);(null==o?void 0:o.isGroupResize)&&(null==c?void 0:c.originalUnrotatedRect)&&(r=Gt(s,c.originalRect,c.originalUnrotatedRect,t.object.rotation??0)),n[a]=this.transformAnnotation(t.object,{type:"resize",changes:{rect:r}})}return n}startResize(t,e){const{annotationIds:n,pageSize:o,resizeHandle:i}=e,a=[],s=[];for(const g of n){const e=this.getAnnotationById(g,t);if(e){s.push({id:g,rect:e.object.rect,unrotatedRect:e.object.unrotatedRect?this.cloneRect(e.object.unrotatedRect):void 0,pageIndex:e.object.pageIndex,isAttachedLink:!1});const n=this.getAttachedLinksMethod(g,t);for(const t of n)a.includes(t.object.id)||(a.push(t.object.id),s.push({id:t.object.id,rect:t.object.rect,unrotatedRect:t.object.unrotatedRect?this.cloneRect(t.object.unrotatedRect):void 0,pageIndex:t.object.pageIndex,isAttachedLink:!0,parentId:g}))}}const r=[...n,...a],c=s.map(t=>t.rect),d=this.computeUnifiedGroupBoundingBox(c),l=this.computeUnifiedRelativePositions(s,d),u=this.computeUnifiedResizedRects(l,d),h={documentId:t,isResizing:!0,isGroupResize:n.length>1,primaryIds:n,attachedLinkIds:a,allParticipantIds:r,originalGroupBox:d,currentGroupBox:d,participatingAnnotations:l,resizeHandle:i,computedRects:u};this.unifiedResizeStates.set(t,h);const p=this.computeResizePreviewPatches(u,t);this.unifiedResize$.emit({documentId:t,type:"start",state:h,computedRects:Object.fromEntries(u),previewPatches:p})}updateResize(t,e){const n=this.unifiedResizeStates.get(t);if(!(null==n?void 0:n.isResizing))return{};const o=this.computeUnifiedResizedRects(n.participatingAnnotations,e),i={...n,currentGroupBox:e,computedRects:o};this.unifiedResizeStates.set(t,i);const a=Object.fromEntries(o),s=this.computeResizePreviewPatches(o,t);return this.unifiedResize$.emit({documentId:t,type:"update",state:i,computedRects:a,previewPatches:s}),a}commitResize(t){const e=this.unifiedResizeStates.get(t);if(!e)return;const n=this.computeUnifiedResizedRects(e.participatingAnnotations,e.currentGroupBox),o=[],i=new Map(e.participatingAnnotations.map(t=>[t.id,t]));for(const[s,r]of n){const n=this.getAnnotationById(s,t);if(!n)continue;let a=r;const c=i.get(s);e.isGroupResize&&(null==c?void 0:c.originalUnrotatedRect)&&(a=Gt(r,c.originalRect,c.originalUnrotatedRect,n.object.rotation??0));const d=this.transformAnnotation(n.object,{type:"resize",changes:{rect:a}});o.push({pageIndex:n.object.pageIndex,id:s,patch:d})}o.length>0&&this.updateAnnotationsMethod(o,t);const a=this.computeResizePreviewPatches(n,t);this.unifiedResize$.emit({documentId:t,type:"end",state:{...e,isResizing:!1},computedRects:Object.fromEntries(n),previewPatches:a}),this.unifiedResizeStates.delete(t)}cancelResize(t){const e=this.unifiedResizeStates.get(t);if(!e)return;const n=this.computeUnifiedResizedRects(e.participatingAnnotations,e.originalGroupBox);this.unifiedResize$.emit({documentId:t,type:"cancel",state:{...e,isResizing:!1,currentGroupBox:e.originalGroupBox},computedRects:Object.fromEntries(n),previewPatches:{}}),this.unifiedResizeStates.delete(t)}getResizeState(t){return this.unifiedResizeStates.get(t)??null}get onResizeChange(){return this.unifiedResize$.on}cloneRect(t){return{origin:{x:t.origin.x,y:t.origin.y},size:{width:t.size.width,height:t.size.height}}}translateRect(t,e){return{origin:{x:t.origin.x+e.x,y:t.origin.y+e.y},size:{...t.size}}}normalizeAngle(t){const e=t%360;return e<0?e+360:e}normalizeDelta(t){return(t+540)%360-180}buildRotationParticipants(t,e){const n=[],o=[];for(const i of t){const t=this.getAnnotationById(i,e);if(!t)continue;n.push({id:i,rect:this.cloneRect(t.object.rect),pageIndex:t.object.pageIndex,rotation:t.object.rotation??0,unrotatedRect:t.object.unrotatedRect?this.cloneRect(t.object.unrotatedRect):void 0,isAttachedLink:!1});const a=this.getAttachedLinksMethod(i,e);for(const e of a)o.includes(e.object.id)||(o.push(e.object.id),n.push({id:e.object.id,rect:this.cloneRect(e.object.rect),pageIndex:e.object.pageIndex,rotation:e.object.rotation??0,unrotatedRect:e.object.unrotatedRect?this.cloneRect(e.object.unrotatedRect):void 0,isAttachedLink:!0,parentId:i}))}return{participants:n,attachedLinkIds:o}}computeRotatePreviewPatches(t,n){const o={};for(const i of t.participants){const a=this.getAnnotationById(i.id,n);if(!a)continue;const s=Rt({rect:i.rect,unrotatedRect:i.unrotatedRect,rotation:i.rotation}),r=e.rotatePointAround(s,t.rotationCenter,t.delta),c={x:r.x-s.x,y:r.y-s.y},d=this.normalizeAngle(i.rotation+t.delta),l=this.transformAnnotation(a.object,{type:"rotate",changes:{rotation:d,unrotatedRect:this.translateRect(i.unrotatedRect??i.rect,c)},metadata:{rotationAngle:d,rotationDelta:t.delta,rotationCenter:t.rotationCenter}});l.rect||0===c.x&&0===c.y||(l.rect={origin:{x:a.object.rect.origin.x+c.x,y:a.object.rect.origin.y+c.y},size:{...a.object.rect.size}}),o[i.id]=l}return o}startRotation(t,e){const{annotationIds:n,cursorAngle:o,rotationCenter:i}=e,{participants:a,attachedLinkIds:s}=this.buildRotationParticipants(n,t);if(0===a.length)return;const r=a.map(t=>t.rect),c=this.computeUnifiedGroupBoundingBox(r),d=i??{x:c.origin.x+c.size.width/2,y:c.origin.y+c.size.height/2},l={documentId:t,isRotating:!0,primaryIds:n,attachedLinkIds:s,allParticipantIds:a.map(t=>t.id),rotationCenter:d,cursorStartAngle:o,currentAngle:o,delta:0,participants:a};this.unifiedRotateStates.set(t,l);const u=this.computeRotatePreviewPatches(l,t);this.unifiedRotate$.emit({documentId:t,type:"start",state:l,previewPatches:u})}updateRotation(t,e,n){const o=this.unifiedRotateStates.get(t);if(!(null==o?void 0:o.isRotating))return;const i=void 0!==n?n:this.normalizeDelta(e-o.cursorStartAngle),a={...o,currentAngle:e,delta:i};this.unifiedRotateStates.set(t,a);const s=this.computeRotatePreviewPatches(a,t);this.unifiedRotate$.emit({documentId:t,type:"update",state:a,previewPatches:s})}commitRotation(t){const e=this.unifiedRotateStates.get(t);if(!e)return;const n=this.computeRotatePreviewPatches(e,t),o=[];for(const[i,a]of Object.entries(n)){const e=this.getAnnotationById(i,t);e&&o.push({pageIndex:e.object.pageIndex,id:i,patch:a})}o.length>0&&this.updateAnnotationsMethod(o,t),this.unifiedRotate$.emit({documentId:t,type:"end",state:{...e,isRotating:!1},previewPatches:n}),this.unifiedRotateStates.delete(t)}cancelRotation(t){const e=this.unifiedRotateStates.get(t);e&&(this.unifiedRotate$.emit({documentId:t,type:"cancel",state:{...e,isRotating:!1,delta:0,currentAngle:e.cursorStartAngle},previewPatches:{}}),this.unifiedRotateStates.delete(t))}getRotateState(t){return this.unifiedRotateStates.get(t)??null}get onRotateChange(){return this.unifiedRotate$.on}updateAnnotationsMethod(t,n){const o=n??this.getActiveDocumentId();if(!this.checkPermission(o,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","UpdateAnnotations",`Cannot update annotations: document ${o} lacks ModifyAnnotations permission`);const i=this.getDocumentState(o),a=t.map(({pageIndex:t,id:e,patch:n})=>{var o;const a=null==(o=i.byUid[e])?void 0:o.object;if(!a)return null;return{pageIndex:t,id:e,patch:this.buildPatch(a,{...n,author:n.author??this.config.annotationAuthor}),originalObject:a}}).filter(t=>null!==t);if(0===a.length)return;const s=()=>{for(const{pageIndex:t,id:e,patch:n,originalObject:i}of a)this.dispatch(D(o,t,e,n)),this.events$.emit(o,{type:"update",documentId:o,annotation:i,pageIndex:t,patch:n,committed:!1})};if(!this.history)return s(),void(!1!==this.config.autoCommit&&this.commit(o));const r=a.map(({pageIndex:t,id:e,patch:n,originalObject:o})=>({pageIndex:t,id:e,originalPatch:Object.fromEntries(Object.keys(n).map(t=>[t,o[t]])),originalObject:o})),c={execute:s,undo:()=>{for(const{pageIndex:t,id:e,originalPatch:n,originalObject:i}of r)this.dispatch(D(o,t,e,n)),this.events$.emit(o,{type:"update",documentId:o,annotation:i,pageIndex:t,patch:n,committed:!1})},metadata:{annotationIds:a.map(t=>t.id)}};this.history.forDocument(o).register(c,this.ANNOTATION_HISTORY_TOPIC)}moveAnnotationsMethod(t,n){const o=n??this.getActiveDocumentId();if(!this.checkPermission(o,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","MoveAnnotations",`Cannot move annotations: document ${o} lacks ModifyAnnotations permission`);const i=this.getDocumentState(o),a=t.map(({pageIndex:t,id:e,patch:n})=>{var o;const a=null==(o=i.byUid[e])?void 0:o.object;return a?{pageIndex:t,id:e,patch:n,originalObject:a}:null}).filter(t=>null!==t);if(0===a.length)return;const s=()=>{for(const{pageIndex:t,id:e,patch:n,originalObject:i}of a)this.dispatch(L(o,t,e,n)),this.events$.emit(o,{type:"update",documentId:o,annotation:i,pageIndex:t,patch:n,committed:!1})};if(!this.history)return s(),void(!1!==this.config.autoCommit&&this.commit(o));const r=a.map(({pageIndex:t,id:e,patch:n,originalObject:o})=>({pageIndex:t,id:e,originalPatch:Object.fromEntries(Object.keys(n).map(t=>[t,o[t]])),originalObject:o})),c={execute:s,undo:()=>{for(const{pageIndex:t,id:e,originalPatch:n,originalObject:i}of r)this.dispatch(L(o,t,e,n)),this.events$.emit(o,{type:"update",documentId:o,annotation:i,pageIndex:t,patch:n,committed:!1})},metadata:{annotationIds:a.map(t=>t.id)}};this.history.forDocument(o).register(c,this.ANNOTATION_HISTORY_TOPIC)}moveAnnotationMethod(t,e,n,o="delta",i){const a=i??this.getActiveDocumentId(),s=this.getAnnotationById(e,a);if(!s)return;const r=s.object.rect,c={...r,origin:"absolute"===o?{x:n.x,y:n.y}:{x:r.origin.x+n.x,y:r.origin.y+n.y}},d=this.transformAnnotation(s.object,{type:"move",changes:{rect:c}});this.moveAnnotationsMethod([{pageIndex:t,id:e,patch:d}],a)}getActiveTool(t){const e=this.getDocumentState(t);return e.activeToolId?this.state.tools.find(t=>t.id===e.activeToolId)??null:null}setActiveTool(t,n,o){var i,a;const s=n??this.getActiveDocumentId();if(null!==t&&!this.checkPermission(s,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","SetActiveTool",`Cannot activate tool: document ${s} lacks ModifyAnnotations permission`);const r=this.getDocumentState(s);if(t===r.activeToolId&&!o)return;this.dispatch(k(s,t,o));const c=this.state.tools.find(e=>e.id===t);c?null==(i=this.interactionManager)||i.forDocument(s).activate(c.interaction.mode??c.id):null==(a=this.interactionManager)||a.forDocument(s).activateDefaultMode()}getTool(t){return this.state.tools.find(e=>e.id===t)}findToolForAnnotation(t){let e=null,n=0;for(const o of this.state.tools){const i=o.matchScore(t);i>n&&(n=i,e=o)}return e}setLocked(t,e){const n=e??this.getActiveDocumentId();this.dispatch(((t,e)=>({type:x,payload:{documentId:t,mode:e}}))(n,t));const o=this.getDocumentState(n),i=o.selectedUids.filter(t=>{const e=o.byUid[t];return!!e&&this.isAnnotationLocked(e.object,n)});if(i.length>0){const t=o.selectedUids.filter(t=>!i.includes(t));this.dispatch(M(n,t))}}getLocked(t){return this.getDocumentState(t).locked}isAnnotationLocked(t,e){if(ot(t))return!0;return nt(et(this.findToolForAnnotation(t)),this.getLocked(e))}isCategoryLockedMethod(t,e){return nt([t],this.getLocked(e))}isToolLockedMethod(t,e){const n=this.getTool(t);if(!n)return!1;return nt(et(n),this.getLocked(e))}syncAnnotationObject(t,e,n){const o=n??this.getActiveDocumentId();this.dispatch(((t,e,n)=>({type:b,payload:{documentId:t,id:e,patch:n}}))(o,t,e))}navigateTargetMethod(t,n){var o;const i=n??this.getActiveDocumentId(),a=n=>(this.navigate$.emit(i,{documentId:i,result:n,target:t}),e.PdfTaskHelper.resolve(n));let s;if("destination"===t.type)s=t.destination;else if("action"===t.type){const n=t.action;if(n.type!==e.PdfActionType.Goto&&n.type!==e.PdfActionType.RemoteGoto)return n.type===e.PdfActionType.URI?a({outcome:"uri",uri:n.uri}):a({outcome:"unsupported"});s=n.destination}if(!s)return a({outcome:"unsupported"});if(!this.scroll)return a({outcome:"destination",destination:s});const r=this.scroll.forDocument(i);if(s.zoom.mode===e.PdfZoomMode.XYZ){const t=this.getCoreDocument(i),e=null==(o=null==t?void 0:t.document)?void 0:o.pages.find(t=>t.index===s.pageIndex);r.scrollToPage({pageNumber:s.pageIndex+1,pageCoordinates:e&&s.zoom.params?{x:s.zoom.params.x,y:e.size.height-s.zoom.params.y}:void 0,behavior:"smooth"})}else r.scrollToPage({pageNumber:s.pageIndex+1,behavior:"smooth"});return a({outcome:"navigated"})}collectPendingChanges(t,e){const n=this.getDocumentState(t),o=this.pendingContexts.get(t),i={creations:[],updates:[],deletions:[],committedUids:[],isEmpty:!0};for(const[a,s]of Object.entries(n.byUid)){if("synced"===s.commitState)continue;if(e.pages.find(t=>t.index===s.object.pageIndex))switch(i.committedUids.push(a),i.isEmpty=!1,s.commitState){case"new":i.creations.push({uid:a,ta:s,ctx:null==o?void 0:o.get(s.object.id)});break;case"moved":i.updates.push({uid:a,ta:s,moved:!0});break;case"dirty":i.updates.push({uid:a,ta:s});break;case"deleted":i.deletions.push({uid:a,ta:s})}}return i}executeCommitBatch(t,n,o){const i=new e.Task,a=this.pendingContexts.get(t),s=[];for(const{uid:e,ta:c,ctx:d}of o.creations){const t=n.pages.find(t=>t.index===c.object.pageIndex);if(!t)continue;const o=this.engine.createPageAnnotation(n,t,c.object,d);s.push({type:"create",task:o,ta:c,uid:e,ctx:d})}for(const{uid:e,ta:c,moved:d}of o.updates){const t=n.pages.find(t=>t.index===c.object.pageIndex);if(!t)continue;const o=d?this.engine.updatePageAnnotation(n,t,c.object,{regenerateAppearance:!1}):this.engine.updatePageAnnotation(n,t,c.object);s.push({type:"update",task:o,ta:c,uid:e,moved:d})}for(const{uid:c,ta:d}of o.deletions){const o=n.pages.find(t=>t.index===d.object.pageIndex);if(o)if(d.object.id){const t=new e.Task;this.engine.removePageAnnotation(n,o,d.object).wait(()=>t.resolve(!0),t.fail),s.push({type:"delete",task:t,ta:d,uid:c})}else this.dispatch(j(t,d.object.pageIndex,c))}const r=s.map(t=>t.task);return e.Task.allSettled(r).wait(()=>{this.emitCommitEvents(t,s,a);for(const o of s)"update"===o.type&&o.moved||"create"!==o.type&&"update"!==o.type&&"delete"!==o.type||this.invalidateAnnotationAppearance(o.ta.object.id,o.ta.object.pageIndex,t);var e,n;this.dispatch((e=t,n=o.committedUids,{type:y,payload:{documentId:e,committedUids:n}})),i.resolve(!0)},t=>i.fail(t)),i}emitCommitEvents(t,n,o){for(const i of n)if(i.task.state.stage===e.TaskStage.Resolved)switch(i.type){case"create":this.events$.emit(t,{type:"create",documentId:t,annotation:i.ta.object,pageIndex:i.ta.object.pageIndex,ctx:i.ctx,committed:!0}),null==o||o.delete(i.ta.object.id);break;case"update":this.events$.emit(t,{type:"update",documentId:t,annotation:i.ta.object,pageIndex:i.ta.object.pageIndex,patch:i.ta.object,committed:!0});break;case"delete":this.dispatch(j(t,i.ta.object.pageIndex,i.uid)),this.events$.emit(t,{type:"delete",documentId:t,annotation:i.ta.object,pageIndex:i.ta.object.pageIndex,committed:!0})}}acquireCommitLock(t){return!this.commitInProgress.get(t)&&(this.commitInProgress.set(t,!0),!0)}releaseCommitLock(t){this.commitInProgress.set(t,!1)}commit(t){const n=t??this.getActiveDocumentId();if(!this.getDocumentState(n).hasPendingChanges)return e.PdfTaskHelper.resolve(!0);if(!this.acquireCommitLock(n))return e.PdfTaskHelper.resolve(!0);const o=this.getCoreDocument(n),i=null==o?void 0:o.document;if(!i)return this.releaseCommitLock(n),e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Document not found"});const a=this.collectPendingChanges(n,i);if(a.isEmpty)return this.releaseCommitLock(n),e.PdfTaskHelper.resolve(!0);const s=new e.Task;return this.executeCommitBatch(n,i,a).wait(()=>{this.releaseCommitLock(n);this.getDocumentState(n).hasPendingChanges?this.commit(n).wait(t=>s.resolve(t),t=>s.fail(t)):s.resolve(!0)},t=>{this.releaseCommitLock(n),s.fail(t)}),s}};Ge.id="annotation",Ge.defaultHandlerFactories=new Map([[e.PdfAnnotationSubtype.CIRCLE,ke],[e.PdfAnnotationSubtype.SQUARE,Te],[e.PdfAnnotationSubtype.STAMP,Me],[e.PdfAnnotationSubtype.POLYGON,Se],[e.PdfAnnotationSubtype.POLYLINE,Pe],[e.PdfAnnotationSubtype.LINE,Ie],[e.PdfAnnotationSubtype.INK,At],[e.PdfAnnotationSubtype.FREETEXT,ve],[e.PdfAnnotationSubtype.TEXT,xe],[e.PdfAnnotationSubtype.LINK,Ce]]);let Fe=Ge;function We(t){return e=>(null==e?void 0:e.id)===t}const He=We("highlight"),_e=We("squiggly"),Ke=We("underline"),Ye=We("strikeout"),Ve=We("ink"),Xe=We("inkHighlighter"),qe=We("square"),Qe=We("circle"),Ze=We("line"),Je=We("polyline"),tn=We("polygon"),en=We("freeText"),nn=We("stamp"),on=We("insertText"),an=We("replaceText"),sn={manifest:o,create:(t,e)=>new Fe(n,t,e),reducer:(t,e)=>{switch(e.type){case i:{const{documentId:n,state:o}=e.payload;return{...t,documents:{...t.documents,[n]:o},activeDocumentId:t.activeDocumentId??n}}case a:{const n=e.payload,{[n]:o,...i}=t.documents;return{...t,documents:i,activeDocumentId:t.activeDocumentId===n?null:t.activeDocumentId}}case"ANNOTATION/SET_ACTIVE_DOCUMENT":return{...t,activeDocumentId:e.payload};case s:{const{documentId:n,annotations:o}=e.payload,i=t.documents[n];if(!i)return t;const a={},s={};for(const[t,e]of Object.entries(o)){const n=Number(t),o=i.pages[n]||[];for(const t of o)delete s[t];const r=e.map(t=>{const e=t.id;return s[e]={commitState:"synced",object:t},e});a[n]=r}return{...t,documents:{...t.documents,[n]:{...i,pages:a,byUid:s}}}}case r:{const{documentId:n,id:o}=e.payload,i=t.documents[n];return i?{...t,documents:{...t.documents,[n]:{...i,selectedUids:[o],selectedUid:o}}}:t}case c:{const{documentId:n}=e.payload,o=t.documents[n];return o?{...t,documents:{...t.documents,[n]:{...o,selectedUids:[],selectedUid:null}}}:t}case d:{const{documentId:n,id:o}=e.payload,i=t.documents[n];if(!i)return t;if(i.selectedUids.includes(o))return t;const a=[...i.selectedUids,o];return{...t,documents:{...t.documents,[n]:{...i,selectedUids:a,selectedUid:Ne(a)}}}}case l:{const{documentId:n,id:o}=e.payload,i=t.documents[n];if(!i)return t;const a=i.selectedUids.filter(t=>t!==o);return{...t,documents:{...t.documents,[n]:{...i,selectedUids:a,selectedUid:Ne(a)}}}}case u:{const{documentId:n,ids:o}=e.payload,i=t.documents[n];return i?{...t,documents:{...t.documents,[n]:{...i,selectedUids:o,selectedUid:Ne(o)}}}:t}case h:{const{documentId:n,toolId:o,context:i}=e.payload,a=t.documents[n];return a?{...t,documents:{...t.documents,[n]:{...a,activeToolId:o,activeToolContext:i}}}:t}case p:{const{documentId:n,pageIndex:o,annotation:i}=e.payload,a=t.documents[n];if(!a)return t;const s=i.id;return{...t,documents:{...t.documents,[n]:{...a,pages:{...a.pages,[o]:[...a.pages[o]??[],s]},byUid:{...a.byUid,[s]:{commitState:"new",object:i,dictMode:!0}},hasPendingChanges:!0}}}}case m:{const{documentId:n,pageIndex:o,id:i}=e.payload,a=t.documents[n];return a&&a.byUid[i]?{...t,documents:{...t.documents,[n]:{...a,pages:{...a.pages,[o]:(a.pages[o]??[]).filter(t=>t!==i)},byUid:{...a.byUid,[i]:{...a.byUid[i],commitState:"deleted"}},hasPendingChanges:!0}}}:t}case g:{const{documentId:n,id:o,patch:i}=e.payload,a=t.documents[n];return a?{...t,documents:{...t.documents,[n]:Ue(a,o,i)}}:t}case f:{const{documentId:n,id:o,patch:i}=e.payload,a=t.documents[n];return a?{...t,documents:{...t.documents,[n]:Be(a,o,i)}}:t}case y:{const{documentId:n,committedUids:o}=e.payload,i=t.documents[n];if(!i)return t;const a=new Set(o),s={};let r=!1;for(const[t,e]of Object.entries(i.byUid))a.has(t)?s[t]={...e,commitState:"dirty"===e.commitState||"new"===e.commitState||"moved"===e.commitState?"synced":e.commitState}:(s[t]=e,"new"!==e.commitState&&"dirty"!==e.commitState&&"moved"!==e.commitState&&"deleted"!==e.commitState||(r=!0));return{...t,documents:{...t.documents,[n]:{...i,byUid:s,hasPendingChanges:r}}}}case A:{const{documentId:n,pageIndex:o,uid:i}=e.payload,a=t.documents[n];if(!a||!a.byUid[i])return t;const{[i]:s,...r}=a.byUid;return{...t,documents:{...t.documents,[n]:{...a,pages:{...a.pages,[o]:(a.pages[o]??[]).filter(t=>t!==i)},byUid:r}}}}case x:{const{documentId:n,mode:o}=e.payload,i=t.documents[n];return i?{...t,documents:{...t.documents,[n]:{...i,locked:o}}}:t}case b:{const{documentId:n,id:o,patch:i}=e.payload,a=t.documents[n];if(!a)return t;const s=a.byUid[o];return s?{...t,documents:{...t.documents,[n]:{...a,byUid:{...a.byUid,[o]:{...s,object:{...s.object,...i}}}}}}:t}case P:{const n=new Map(t.tools.map(t=>[t.id,t]));return n.set(e.payload.id,e.payload),{...t,tools:Array.from(n.values())}}case I:{const{toolId:n,patch:o}=e.payload;return{...t,tools:t.tools.map(t=>t.id===n?{...t,defaults:{...t.defaults,...o}}:t)}}case v:return t.colorPresets.includes(e.payload)?t:{...t,colorPresets:[...t.colorPresets,e.payload]};default:return t}},initialState:(t,e)=>$e(e)};Object.defineProperty(exports,"calculateRotatedRectAABB",{enumerable:!0,get:()=>e.calculateRotatedRectAABB}),Object.defineProperty(exports,"calculateRotatedRectAABBAroundPoint",{enumerable:!0,get:()=>e.calculateRotatedRectAABBAroundPoint}),Object.defineProperty(exports,"getRectCenter",{enumerable:!0,get:()=>e.getRectCenter}),Object.defineProperty(exports,"inferRotationCenterFromRects",{enumerable:!0,get:()=>e.inferRotationCenterFromRects}),Object.defineProperty(exports,"rotatePointAroundCenter",{enumerable:!0,get:()=>e.rotatePointAround}),Object.defineProperty(exports,"rotateVertices",{enumerable:!0,get:()=>e.rotateVertices}),exports.ANNOTATION_PLUGIN_ID=n,exports.AnnotationPlugin=Fe,exports.AnnotationPluginPackage=sn,exports.LockModeType=w,exports.applyInsertUpright=Nt,exports.clampAnnotationToPage=Ot,exports.convertAABBRectToUnrotatedSpace=Gt,exports.createToolPredicate=We,exports.defineAnnotationTool=t=>t,exports.generateCloudyEllipsePath=function(t,e,n,o){const i=new Yt;let a=0,s=0,r=t.width,c=t.height;return e&&(a+=e.left,s+=e.top,r-=e.right,c-=e.bottom),de(a,-c,r,-s,n,o,i),i.close(),i.build(o)},exports.generateCloudyPolygonPath=function(t,e,n,o){const i=new Yt;if(t.length<3)return i.build(o);const a=t.map(t=>({x:t.x-e.x,y:-(t.y-e.y)})),s=a[0],r=a[a.length-1];return s.x===r.x&&s.y===r.y||a.push({x:s.x,y:s.y}),se(a,!1,n,o,i),i.close(),i.build(o)},exports.generateCloudyRectanglePath=function(t,e,n,o){const i=new Yt;let a=0,s=0,r=t.width,c=t.height;return e?(a+=e.left,s+=e.top,r-=e.right,c-=e.bottom):(a+=o/2,s+=o/2,r-=o/2,c-=o/2),se([{x:a,y:-s},{x:r,y:-s},{x:r,y:-c},{x:a,y:-c},{x:a,y:-s}],!1,n,o,i),i.close(),i.build(o)},exports.getAnnotationByUid=ct,exports.getAnnotationCategories=et,exports.getAnnotations=t=>{const e={};for(const n of Object.keys(t.pages).map(Number))e[n]=it(t,n);return e},exports.getAnnotationsByPageIndex=it,exports.getAttachedLinks=ht,exports.getCloudyBorderExtent=ue,exports.getGroupLeaderId=pt,exports.getGroupMembers=gt,exports.getIRTChildIds=lt,exports.getIRTChildrenByType=ut,exports.getSelectedAnnotation=at,exports.getSelectedAnnotationByPageIndex=(t,e)=>{const n=t.pages[e]??[];for(const o of t.selectedUids)if(n.includes(o))return t.byUid[o]??null;return null},exports.getSelectedAnnotationIds=rt,exports.getSelectedAnnotations=st,exports.getSelectedAnnotationsByPageIndex=(t,e)=>{const n=new Set(t.pages[e]??[]);return t.selectedUids.filter(t=>n.has(t)).map(e=>t.byUid[e]).filter(t=>void 0!==t)},exports.getSelectionGroupingAction=mt,exports.getSidebarAnnotationsWithReplies=t=>{const e=dt(t),n=[],o=Object.keys(e).map(Number).sort((t,e)=>t-e);for(const i of o)n.push(...e[i]);return n},exports.getSidebarAnnotationsWithRepliesGroupedByPage=dt,exports.getToolDefaultsById=function(t,e){const n=t.tools.find(t=>t.id===e);return null==n?void 0:n.defaults},exports.hasAttachedLinks=(t,e)=>ht(t,e).length>0,exports.hasIRTChildren=(t,e)=>{for(const n of Object.values(t.pages))for(const o of n){const n=t.byUid[o];if(n&&"inReplyToId"in n.object&&n.object.inReplyToId===e)return!0}return!1},exports.hasLockedFlag=ot,exports.hasMultipleSelected=t=>t.selectedUids.length>1,exports.initialDocumentState=Oe,exports.initialState=$e,exports.isAnnotationSelected=(t,e)=>t.selectedUids.includes(e),exports.isCaret=Q,exports.isCategoryLocked=nt,exports.isCircle=O,exports.isCircleTool=Qe,exports.isFreeText=Y,exports.isFreeTextTool=en,exports.isHighlight=F,exports.isHighlightTool=He,exports.isInGroup=ft,exports.isInk=N,exports.isInkHighlighterTool=Xe,exports.isInkTool=Ve,exports.isInsertTextTool=on,exports.isLine=$,exports.isLineTool=Ze,exports.isLink=function(t){return t.object.type===e.PdfAnnotationSubtype.LINK},exports.isPolygon=U,exports.isPolygonTool=tn,exports.isPolyline=G,exports.isPolylineTool=Je,exports.isRedact=q,exports.isReplaceTextTool=an,exports.isSelectableAnnotation=tt,exports.isSidebarAnnotation=J,exports.isSquare=B,exports.isSquareTool=qe,exports.isSquiggly=_,exports.isSquigglyTool=_e,exports.isStamp=V,exports.isStampTool=nn,exports.isStrikeout=H,exports.isStrikeoutTool=Ye,exports.isText=X,exports.isTextMarkup=K,exports.isUnderline=W,exports.isUnderlineTool=Ke,exports.isWidget=Z,exports.manifest=o,exports.patching=Ae,exports.rectsIntersect=z,exports.resolveInteractionProp=function(t,e,n){return void 0===t?n:"function"==typeof t?t(e):t},exports.useClickDetector=be,exports.useState=yt;
2
2
  //# sourceMappingURL=index.cjs.map