@embedpdf/plugin-annotation 2.8.0 → 2.9.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 +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +814 -139
- package/dist/index.js.map +1 -1
- package/dist/lib/geometry/cloudy-border.d.ts +90 -0
- package/dist/lib/geometry/index.d.ts +1 -0
- package/dist/lib/handlers/types.d.ts +2 -1
- package/dist/lib/tools/default-tools.d.ts +38 -87
- package/dist/lib/tools/types.d.ts +32 -1
- package/dist/preact/index.cjs +1 -1
- package/dist/preact/index.cjs.map +1 -1
- package/dist/preact/index.js +352 -239
- package/dist/preact/index.js.map +1 -1
- package/dist/react/index.cjs +1 -1
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.js +352 -239
- package/dist/react/index.js.map +1 -1
- package/dist/shared/components/annotation-container.d.ts +4 -2
- package/dist/shared/components/annotations/circle.d.ts +6 -2
- package/dist/shared/components/annotations/polygon.d.ts +3 -1
- package/dist/shared/components/annotations/square.d.ts +6 -2
- package/dist/shared/components/types.d.ts +6 -2
- package/dist/shared-preact/components/annotation-container.d.ts +4 -2
- package/dist/shared-preact/components/annotations/circle.d.ts +6 -2
- package/dist/shared-preact/components/annotations/polygon.d.ts +3 -1
- package/dist/shared-preact/components/annotations/square.d.ts +6 -2
- package/dist/shared-preact/components/types.d.ts +6 -2
- package/dist/shared-react/components/annotation-container.d.ts +4 -2
- package/dist/shared-react/components/annotations/circle.d.ts +6 -2
- package/dist/shared-react/components/annotations/polygon.d.ts +3 -1
- package/dist/shared-react/components/annotations/square.d.ts +6 -2
- package/dist/shared-react/components/types.d.ts +6 -2
- package/dist/svelte/components/annotations/Circle.svelte.d.ts +3 -1
- package/dist/svelte/components/annotations/Polygon.svelte.d.ts +1 -0
- package/dist/svelte/components/annotations/Square.svelte.d.ts +3 -1
- package/dist/svelte/components/types.d.ts +2 -1
- package/dist/svelte/context/types.d.ts +6 -2
- package/dist/svelte/index.cjs +1 -1
- package/dist/svelte/index.cjs.map +1 -1
- package/dist/svelte/index.js +505 -293
- package/dist/svelte/index.js.map +1 -1
- package/dist/vue/components/annotation-container.vue.d.ts +7 -6
- package/dist/vue/components/annotations/circle.vue.d.ts +5 -1
- package/dist/vue/components/annotations/polygon.vue.d.ts +2 -0
- package/dist/vue/components/annotations/square.vue.d.ts +5 -1
- package/dist/vue/components/annotations.vue.d.ts +8 -9
- package/dist/vue/context/types.d.ts +6 -2
- package/dist/vue/index.cjs +1 -1
- package/dist/vue/index.cjs.map +1 -1
- package/dist/vue/index.js +259 -121
- package/dist/vue/index.js.map +1 -1
- package/package.json +10 -10
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"],defaultConfig:{autoCommit:!0,annotationAuthor:"Guest",deactivateToolAfterCreate:!1,selectAfterCreate:!0}},i="ANNOTATION/INIT_STATE",s="ANNOTATION/CLEANUP_STATE",a="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",g="ANNOTATION/CREATE_ANNOTATION",p="ANNOTATION/PATCH_ANNOTATION",f="ANNOTATION/MOVE_ANNOTATION",m="ANNOTATION/DELETE_ANNOTATION",y="ANNOTATION/COMMIT",A="ANNOTATION/PURGE_ANNOTATION",x="ANNOTATION/ADD_COLOR_PRESET",b="ANNOTATION/SET_TOOL_DEFAULTS",I="ANNOTATION/ADD_TOOL";const v=t=>({type:c,payload:{documentId:t}}),P=(t,e,n)=>({type:d,payload:{documentId:t,pageIndex:e,id:n}}),S=(t,e)=>({type:l,payload:{documentId:t,id:e}}),R=(t,e)=>({type:u,payload:{documentId:t,ids:e}}),T=(t,e)=>({type:h,payload:{documentId:t,toolId:e}}),C=(t,e,n)=>({type:g,payload:{documentId:t,pageIndex:e,annotation:n}}),k=(t,e,n,o)=>({type:p,payload:{documentId:t,pageIndex:e,id:n,patch:o}}),M=(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}}),D=(t,e,n)=>({type:A,payload:{documentId:t,pageIndex:e,uid:n}});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 w(t){return t.object.type===e.PdfAnnotationSubtype.INK}function j(t){return t.object.type===e.PdfAnnotationSubtype.CIRCLE}function L(t){return t.object.type===e.PdfAnnotationSubtype.POLYGON}function O(t){return t.object.type===e.PdfAnnotationSubtype.SQUARE}function N(t){return t.object.type===e.PdfAnnotationSubtype.LINE}function U(t){return t.object.type===e.PdfAnnotationSubtype.POLYLINE}function F(t){return t.object.type===e.PdfAnnotationSubtype.HIGHLIGHT}function B(t){return t.object.type===e.PdfAnnotationSubtype.UNDERLINE}function G(t){return t.object.type===e.PdfAnnotationSubtype.STRIKEOUT}function $(t){return t.object.type===e.PdfAnnotationSubtype.SQUIGGLY}function H(t){return F(t)||B(t)||G(t)||$(t)}function W(t){return t.object.type===e.PdfAnnotationSubtype.FREETEXT}function _(t){return t.object.type===e.PdfAnnotationSubtype.STAMP}function V(t){return t.object.type===e.PdfAnnotationSubtype.TEXT}function Y(t){return t.object.type===e.PdfAnnotationSubtype.REDACT}function K(t){return t.object.type===e.PdfAnnotationSubtype.CARET}function Q(t){return V(t)&&!t.object.inReplyToId||H(t)||w(t)||O(t)||j(t)||L(t)||N(t)||U(t)||W(t)||_(t)||Y(t)||K(t)}const q=(t,e)=>(t.pages[e]??[]).map(e=>t.byUid[e]),X=t=>t.selectedUids.length>0?t.byUid[t.selectedUids[0]]??null:null,Z=t=>t.selectedUids.map(e=>t.byUid[e]).filter(t=>void 0!==t),J=t=>t.selectedUids,tt=(t,e)=>t.byUid[e]??null;const et=t=>{const n={};for(const e of Object.values(t.pages))for(const o of e){const e=t.byUid[o];if(e&&V(e)){const t=e.object.inReplyToId;t&&(n[t]||(n[t]=[])).push(e)}}const o={},i=new Set;for(const a of Object.values(t.pages))for(const n of a){const s=t.byUid[n];if(s&&s.object.inReplyToId&&s.object.replyType===e.PdfAnnotationReplyType.Group&&s.object.type!==e.PdfAnnotationSubtype.LINK&&Q(s)){const t=s.object.inReplyToId;(o[t]||(o[t]=[])).push(s),i.add(s.object.id)}}const s={};for(const[e,a]of Object.entries(t.pages)){const r=Number(e),c=[];for(const e of a){const s=t.byUid[e];if(!s||!Q(s))continue;if(i.has(s.object.id))continue;const a=o[s.object.id];c.push({page:r,annotation:s,replies:n[s.object.id]??[],...a&&a.length>0?{groupMembers:a}:{}})}c.length>0&&(s[r]=c)}return s},nt=(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},ot=(t,e,n)=>{const o=[];for(const i of Object.values(t.pages))for(const s of i){const i=t.byUid[s];i&&"inReplyToId"in i.object&&i.object.inReplyToId===e&&n.includes(i.object.type)&&o.push(i)}return o},it=(t,n)=>ot(t,n,[e.PdfAnnotationSubtype.LINK]),st=(t,n)=>{const o=t.byUid[n];if(o)return o.object.inReplyToId&&o.object.replyType===e.PdfAnnotationReplyType.Group?o.object.inReplyToId:n},at=(t,n)=>{const o=st(t,n);if(!o)return[];const i=[],s=t.byUid[o];s&&s.object.type!==e.PdfAnnotationSubtype.LINK&&i.push(s);for(const a of Object.values(t.pages))for(const n of a){const s=t.byUid[n];s&&s.object.inReplyToId===o&&s.object.replyType===e.PdfAnnotationReplyType.Group&&s.object.type!==e.PdfAnnotationSubtype.LINK&&i.push(s)}return i},rt=(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},ct=t=>{const e=Z(t);if(0===e.length)return"disabled";const n=e[0].object.id;if(rt(t,n)){const o=at(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"},dt=[{id:"highlight",name:"Highlight",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",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",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",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",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"}},{id:"replaceText",name:"Replace Text",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"}},{id:"ink",name:"Pen",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}},{id:"inkHighlighter",name:"Ink Highlighter",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}},{id:"circle",name:"Circle",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}}},{id:"square",name:"Square",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}}},{id:"line",name:"Line",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}},{id:"lineArrow",name:"Arrow",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}},{id:"polyline",name:"Polyline",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"}},{id:"polygon",name:"Polygon",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"}},{id:"textComment",name:"Comment",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}},{id:"freeText",name:"Free Text",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}},{id:"stamp",name:"Image",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}}],lt=["#E44234","#FF8D00","#FFCD45","#5CC96E","#25D2D1","#597CE2","#C544CE","#7D2E25","#000000","#FFFFFF"],ut=t=>1===t.length?t[0]:null,ht=()=>({pages:{},byUid:{},selectedUids:[],selectedUid:null,activeToolId:null,hasPendingChanges:!1}),gt=(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},pt=(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},ft=t=>{const e=new Map;dt.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;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}}}),colorPresets:t.colorPresets??lt}};function mt(t){let e=t;return[()=>e,t=>{e=t}]}const yt={annotationType:e.PdfAnnotationSubtype.INK,create(n){const{onCommit:o,onPreview:i,getTool:s,pageSize:a}=n,[r,c]=mt([]),[d,l]=mt(!1),u={current:null},h=e=>({x:t.clamp(e.x,0,a.width),y:t.clamp(e.y,0,a.height)}),g=()=>{const t=s();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},p=()=>{const t=r();if(0===t.length||0===t[0].points.length)return null;const n=g();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}}};return{onPointerDown:(t,e)=>{var n;const o=h(t);l(!0),u.current&&clearTimeout(u.current);const s=[...r(),{points:[o]}];c(s),i(p()),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(p())},onPointerUp:(t,s)=>{var a;l(!1),null==(a=s.releasePointerCapture)||a.call(s),u.current&&clearTimeout(u.current),u.current=setTimeout(()=>{const t=r();if(t.length>0&&t[0].points.length>1){const i=g();if(!i)return;const s=t.flatMap(t=>t.points),a=e.expandRect(e.rectFromPoints(s),i.strokeWidth/2);o({...i,inkList:t,rect:a,type:e.PdfAnnotationSubtype.INK,pageIndex:n.pageIndex,id:e.uuidV4(),created:new Date})}c([]),i(null)},800)},onPointerCancel:(t,e)=>{var n;c([]),l(!1),i(null),u.current&&clearTimeout(u.current),null==(n=e.releasePointerCapture)||n.call(e)}}}};function At(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 xt(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 bt=At(!1),It=At(!0),vt={[e.PdfAnnotationLineEnding.OpenArrow]:bt,[e.PdfAnnotationLineEnding.ClosedArrow]:It,[e.PdfAnnotationLineEnding.ROpenArrow]:{...bt,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]:xt(6,t=>t+Math.PI/2),[e.PdfAnnotationLineEnding.Slash]:xt(18,t=>t+Math.PI/1.5)};function Pt(t,n,o){if(!t||0===t.length)return{origin:{x:0,y:0},size:{width:0,height:0}};const i=[...t],s=(t,e)=>Math.atan2(e.y-t.y,e.x-t.x),a=(t,o,s)=>{if(!t)return;const a=vt[t];if(!a)return;const r=a.getLocalPoints(n),c=a.getRotation(s),d=r.map(t=>e.rotateAndTranslatePoint(t,c,o));i.push(...d)};if(t.length>=2){const e=s(t[1],t[0]);a(null==o?void 0:o.start,t[0],e);const n=t.length-1,i=s(t[n-1],t[n]);a(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 St(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 Tt(t,n,o){const i=Rt(t),s=t.unrotatedRect??t.rect,a=n.origin.x-s.origin.x,r=n.origin.y-s.origin.y,c={x:i.x+a,y:i.y+r};return{rect:e.calculateRotatedRectAABBAroundPoint(n,o,c),unrotatedRect:n}}function Ct(t,n,o){if(!t.unrotatedRect)return n;const i=t.rotation??0;if(Math.abs(i%360)<1e-8)return n;const s=Rt(t),a=e.getRectCenter(o),r=i*Math.PI/180,c=Math.cos(r),d=Math.sin(r),l=s.x-a.x,u=s.y-a.y,h=(1-c)*l+d*u,g=-d*l+(1-c)*u;return Math.abs(h)<1e-8&&Math.abs(g)<1e-8?n:n.map(t=>({x:t.x+h,y:t.y+g}))}class kt{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 Mt=new kt;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{...Tt(t,{origin:{...i.origin},size:{...i.size}},o),rotation:o}}function Dt(t,e){return{rotation:e,...Tt(t,t.unrotatedRect??t.rect,e)}}function zt(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 s=n.size.width/i.size.width,a=n.size.height/i.size.height;if((n.size.width<10||n.size.height<10)&&(s=Math.max(s,10/i.size.width),a=Math.max(a,10/i.size.height),n={origin:n.origin,size:{width:i.size.width*s,height:i.size.height*a}}),null==o?void 0:o.maintainAspectRatio){const t=Math.min(s,a);s=t,a=t,n={origin:n.origin,size:{width:i.size.width*t,height:i.size.height*t}}}return{scaleX:s,scaleY:a,oldRect:i,resolvedRect:n,rects:t.unrotatedRect?{unrotatedRect:n,rect:e.calculateRotatedRectAABB(n,t.rotation??0)}:{rect:n}}}function jt(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 Lt(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:s,rect:a,unrotatedRect:r}=Dt(i,o);return{...i,rotation:s,rect:a,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),s=o-e.rect.origin.x,a=i-e.rect.origin.y;return 0===s&&0===a?e:{...e,rect:{origin:{x:o,y:i},size:e.rect.size},...e.unrotatedRect?{unrotatedRect:{origin:{x:e.unrotatedRect.origin.x+s,y:e.unrotatedRect.origin.y+a},size:e.unrotatedRect.size}}:{}}}const Nt=Object.freeze(Object.defineProperty({__proto__:null,LINE_ENDING_HANDLERS:vt,PatchRegistry:kt,applyInsertUpright:Lt,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:Ct,createEnding:function(t,e,n,o,i){if(!t)return null;const s=vt[t];if(!s)return null;const a=s.getRotation(n);return{d:s.getSvgPath(e),transform:`translate(${o} ${i}) rotate(${r=a,180*r/Math.PI})`,filled:s.filled};var r},getRectCenter:e.getRectCenter,lineRectWithEndings:Pt,patchRegistry:Mt,resolveAnnotationRotationCenter:Rt,resolveRotateRects:Tt,resolveVertexEditRects:St,rotatePointAroundCenter:e.rotatePointAround,rotateVertices:e.rotateVertices},Symbol.toStringTag,{value:"Module"})),Ut={annotationType:e.PdfAnnotationSubtype.TEXT,create(t){const{onCommit:n,getTool:o,pageSize:i}=t;return{onPointerDown:s=>{const a=o();if(!a)return;const r={origin:{x:s.x-12,y:s.y-12},size:{width:24,height:24}};let c={...a.defaults,rect:r,type:e.PdfAnnotationSubtype.TEXT,icon:a.defaults.icon??e.PdfAnnotationIcon.Comment,contents:a.defaults.contents??"",flags:a.defaults.flags??["print","noRotate","noZoom"],pageIndex:t.pageIndex,id:e.uuidV4(),created:new Date};c=Ot(c,i),n(c)}}}};function Ft({threshold:t=5,getTool:e,onClickDetected:n}){const[o,i]=mt(null),[s,a]=mt(!1);return{onStart:t=>{i(t),a(!1)},onMove:e=>{const n=o();if(!n||s())return;Math.sqrt(Math.pow(e.x-n.x,2)+Math.pow(e.y-n.y,2))>t&&a(!0)},onEnd:t=>{var r;if(o()&&!s()){const o=e();o&&"clickBehavior"in o&&(null==(r=o.clickBehavior)?void 0:r.enabled)&&n(t,o)}i(null),a(!1)},hasMoved:s,reset:()=>{i(null),a(!1)}}}const Bt={annotationType:e.PdfAnnotationSubtype.FREETEXT,create(n){const{onCommit:o,onPreview:i,getTool:s,pageSize:a,pageIndex:r,pageRotation:c}=n,[d,l]=mt(null),u=e=>({x:t.clamp(e.x,0,a.width),y:t.clamp(e.y,0,a.height)}),h=()=>{const t=s();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},g=Ft({threshold:5,getTool:s,onClickDetected:(t,n)=>{var i;const s=h();if(!s)return;const d=n.clickBehavior;if(!(null==d?void 0:d.enabled))return;const{width:l,height:u}=d.defaultSize,g={origin:{x:t.x-l/2,y:t.y-u/2},size:{width:l,height:u}},p=d.defaultContent??s.contents;let f={...s,contents:p,type:e.PdfAnnotationSubtype.FREETEXT,rect:g,pageIndex:r,id:e.uuidV4(),created:new Date};(null==(i=n.behavior)?void 0:i.insertUpright)&&(f=Lt(f,c,!1)),f=Ot(f,a),o(f)}}),p=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),g.onStart(o),i(p(o)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=u(t);g.onMove(e),d()&&g.hasMoved()&&i(p(e))},onPointerUp:(t,a)=>{var r,p;const f=d();if(!f)return;const m=h();if(!m)return;const y=u(t);if(g.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=s();let a={...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)&&(a=Lt(a,c,!0)),o(a)}else g.onEnd(y);l(null),i(null),g.reset(),null==(p=a.releasePointerCapture)||p.call(a)},onPointerLeave:(t,e)=>{var n;l(null),i(null),g.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;l(null),i(null),g.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},Gt={annotationType:e.PdfAnnotationSubtype.LINE,create(n){const{pageIndex:o,onCommit:i,onPreview:s,getTool:a,pageSize:r}=n,[c,d]=mt(null),l=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.height)}),u=()=>{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,strokeStyle:t.defaults.strokeStyle??e.PdfAnnotationBorderStyle.SOLID,strokeDashArray:t.defaults.strokeDashArray??[],strokeColor:t.defaults.strokeColor??"#000000",flags:t.defaults.flags??["print"]}:null},h=Ft({threshold:5,getTool:a,onClickDetected:(t,n)=>{const s=u();if(!s)return;const a=n.clickBehavior;if(!(null==a?void 0:a.enabled))return;const r=a.defaultAngle??0,c=a.defaultLength/2,d=t.x-c*Math.cos(r),h=t.y-c*Math.sin(r),g=t.x+c*Math.cos(r),p=t.y+c*Math.sin(r),f=l({x:d,y:h}),m=l({x:g,y:p}),y=Pt([f,m],s.strokeWidth,s.lineEndings);i({...s,rect:y,linePoints:{start:f,end:m},pageIndex:o,id:e.uuidV4(),created:new Date,type:e.PdfAnnotationSubtype.LINE})}}),g=t=>{const n=c();if(!n)return null;const o=u();if(!o)return null;const i=Pt([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),s(g(o)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=l(t);h.onMove(e),c()&&h.hasMoved()&&s(g(e))},onPointerUp:(t,n)=>{var a;const r=c();if(!r)return;const g=l(t);if(h.hasMoved()){const t=u();if(!t)return;if(Math.abs(g.x-r.x)>2||Math.abs(g.y-r.y)>2){const n=Pt([r,g],t.strokeWidth,t.lineEndings);i({...t,rect:n,linePoints:{start:r,end:g},pageIndex:o,id:e.uuidV4(),flags:["print"],created:new Date,type:e.PdfAnnotationSubtype.LINE})}}else h.onEnd(g);d(null),s(null),h.reset(),null==(a=n.releasePointerCapture)||a.call(n)},onPointerLeave:(t,e)=>{var n;d(null),s(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;d(null),s(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},$t={annotationType:e.PdfAnnotationSubtype.POLYLINE,create(n){const{onCommit:o,onPreview:i,getTool:s,pageSize:a}=n,[r,c]=mt([]),[d,l]=mt(null),u=e=>({x:t.clamp(e.x,0,a.width),y:t.clamp(e.y,0,a.height)}),h=()=>{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,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=r(),n=d();if(0===t.length||!n)return null;const o=h();if(!o)return null;const i=[...t,n],s=Pt(i,o.strokeWidth,o.lineEndings);return{type:e.PdfAnnotationSubtype.POLYLINE,bounds:s,data:{...o,rect:s,vertices:i,currentVertex:n}}};return{onClick:(t,e)=>{if(e.metaKey||e.ctrlKey)return;const n=u(t),o=r(),s=o[o.length-1];s&&Math.abs(s.x-n.x)<1&&Math.abs(s.y-n.y)<1||(c([...o,n]),l(n),i(g()))},onDoubleClick:()=>{(()=>{const t=r();if(t.length<2)return;const s=h();if(!s)return;const a=Pt(t,s.strokeWidth,s.lineEndings),d={...s,vertices:t,rect:a,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(g())}},onPointerCancel:()=>{c([]),l(null),i(null)}}}},Ht={annotationType:e.PdfAnnotationSubtype.POLYGON,create(n){const{onCommit:o,onPreview:i,getTool:s,scale:a,pageSize:r}=n,[c,d]=mt([]),[l,u]=mt(null),h=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.height)}),g=()=>{const t=s();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},p=()=>{const t=c();if(t.length<3)return;const s=g();if(!s)return;const a=e.expandRect(e.rectFromPoints(t),s.strokeWidth/2),r={...s,vertices:t,rect:a,type:e.PdfAnnotationSubtype.POLYGON,pageIndex:n.pageIndex,id:e.uuidV4(),created:new Date};o(r),d([]),u(null),i(null)},f=()=>{const t=c(),n=l();if(0===t.length||!n)return null;const o=g();if(!o)return null;const i=[...t,n],s=e.expandRect(e.rectFromPoints(i),o.strokeWidth/2);return{type:e.PdfAnnotationSubtype.POLYGON,bounds:s,data:{...o,rect:s,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/a/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 p();const o=c(),s=o[o.length-1];s&&Math.abs(s.x-n.x)<1&&Math.abs(s.y-n.y)<1||(d([...o,n]),u(n),i(f()))},onDoubleClick:t=>{p()},onPointerMove:t=>{if(c().length>0){const e=h(t);u(e),i(f())}},onPointerCancel:t=>{d([]),u(null),i(null)}}}},Wt={annotationType:e.PdfAnnotationSubtype.SQUARE,create(n){const{pageIndex:o,onCommit:i,onPreview:s,getTool:a,pageSize:r}=n,[c,d]=mt(null),l=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.height)}),u=()=>{const t=a();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=Ft({threshold:5,getTool:a,onClickDetected:(n,s)=>{const a=u();if(!a)return;const c=s.clickBehavior;if(!(null==c?void 0:c.enabled))return;const{width:d,height:l}=c.defaultSize,h=d/2,g=l/2,p=t.clamp(n.x-h,0,r.width-d),f=t.clamp(n.y-g,0,r.height-l),m=a.strokeWidth,y=m/2,A={origin:{x:p-y,y:f-y},size:{width:d+m,height:l+m}},x={...a,type:e.PdfAnnotationSubtype.SQUARE,created:new Date,id:e.uuidV4(),pageIndex:o,rect:A};i(x)}}),g=t=>{const n=c();if(!n)return null;const o=Math.min(n.x,t.x),i=Math.min(n.y,t.y),s=Math.abs(n.x-t.x),a=Math.abs(n.y-t.y),r=u();if(!r)return null;const d=r.strokeWidth,l=d/2,h={origin:{x:o-l,y:i-l},size:{width:s+d,height:a+d}};return{type:e.PdfAnnotationSubtype.SQUARE,bounds:h,data:{rect:h,...r}}};return{onPointerDown:(t,e)=>{var n;const o=l(t);d(o),h.onStart(o),s(g(o)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=l(t);h.onMove(e),c()&&h.hasMoved()&&s(g(e))},onPointerUp:(t,n)=>{var a;if(!c())return;if(!u())return;const r=l(t);if(h.hasMoved()){const t=u();if(!t)return;const n=g(r);if(n){const s={...t,type:e.PdfAnnotationSubtype.SQUARE,created:new Date,id:e.uuidV4(),pageIndex:o,rect:n.data.rect};i(s)}}else h.onEnd(r);d(null),s(null),h.reset(),null==(a=n.releasePointerCapture)||a.call(n)},onPointerLeave:(t,e)=>{var n;d(null),s(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;d(null),s(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},_t={annotationType:e.PdfAnnotationSubtype.STAMP,create(t){const{services:n,onCommit:o,getTool:i,pageSize:s,pageRotation:a}=t;return{onPointerDown:r=>{const c=i();if(!c)return;const{imageSrc:d,imageSize:l}=c.defaults,u=(n,i,d)=>{var l;const u={origin:{x:r.x-i/2,y:r.y-d/2},size:{width:i,height:d}};let h={...c.defaults,rect:u,type:e.PdfAnnotationSubtype.STAMP,icon:c.defaults.icon??e.PdfAnnotationIcon.Draft,subject:c.defaults.subject??"Stamp",flags:c.defaults.flags??["print"],pageIndex:t.pageIndex,id:e.uuidV4(),created:new Date};(null==(l=c.behavior)?void 0:l.insertUpright)&&(h=Lt(h,a,!1)),h=Ot(h,s),o(h,{imageData:n})};d?n.processImage({source:d,maxWidth:s.width,maxHeight:s.height,onComplete:t=>u(t.imageData,(null==l?void 0:l.width)??t.width,(null==l?void 0:l.height)??t.height)}):n.requestFile({accept:"image/png,image/jpeg",onFile:t=>{n.processImage({source:t,maxWidth:s.width,maxHeight:s.height,onComplete:t=>u(t.imageData,t.width,t.height)})}})}}}},Vt={annotationType:e.PdfAnnotationSubtype.CIRCLE,create(n){const{pageIndex:o,onCommit:i,onPreview:s,getTool:a,pageSize:r}=n,[c,d]=mt(null),l=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.height)}),u=()=>{const t=a();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=Ft({threshold:5,getTool:a,onClickDetected:(n,s)=>{const a=u();if(!a)return;const c=s.clickBehavior;if(!(null==c?void 0:c.enabled))return;const{width:d,height:l}=c.defaultSize,h=d/2,g=l/2,p=t.clamp(n.x-h,0,r.width-d),f=t.clamp(n.y-g,0,r.height-l),m=a.strokeWidth,y=m/2,A={origin:{x:p-y,y:f-y},size:{width:d+m,height:l+m}},x={...a,type:e.PdfAnnotationSubtype.CIRCLE,created:new Date,id:e.uuidV4(),pageIndex:o,rect:A};i(x)}}),g=t=>{const n=c();if(!n)return null;const o=Math.min(n.x,t.x),i=Math.min(n.y,t.y),s=Math.abs(n.x-t.x),a=Math.abs(n.y-t.y),r=u();if(!r)return null;const d=r.strokeWidth,l=d/2,h={origin:{x:o-l,y:i-l},size:{width:s+d,height:a+d}};return{type:e.PdfAnnotationSubtype.CIRCLE,bounds:h,data:{rect:h,...r}}};return{onPointerDown:(t,e)=>{var n;const o=l(t);d(o),h.onStart(o),s(g(o)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=l(t);h.onMove(e),c()&&h.hasMoved()&&s(g(e))},onPointerUp:(t,n)=>{var a;if(!c())return;if(!u())return;const r=l(t);if(h.hasMoved()){const t=u();if(!t)return;const n=g(r);if(n){const s={...t,type:e.PdfAnnotationSubtype.CIRCLE,flags:["print"],created:new Date,id:e.uuidV4(),pageIndex:o,rect:n.data.rect};i(s)}}else h.onEnd(r);d(null),s(null),h.reset(),null==(a=n.releasePointerCapture)||a.call(n)},onPointerLeave:(t,e)=>{var n;d(null),s(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;d(null),s(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},Yt={toolId:"__textMarkup__",handle(t,n,o){const i=t.getTool();if(i)for(const s of n){const n=e.uuidV4();o().then(e=>{var o;t.createAnnotation(s.pageIndex,{...i.defaults,rect:s.rect,segmentRects:s.segmentRects,pageIndex:s.pageIndex,created:new Date,id:n,...null!=e&&{custom:{text:e}}}),(null==(o=i.behavior)?void 0:o.selectAfterCreate)&&t.selectAnnotation(s.pageIndex,n)})}}};function Kt(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 Qt={toolId:"insertText",handle(t,n,o){const i=t.getTool();if(!i)return;const s=()=>({strokeColor:i.defaults.strokeColor??"#E44234",opacity:i.defaults.opacity??1,flags:i.defaults.flags??["print"]});for(const a of n){const n=a.segmentRects[a.segmentRects.length-1];if(!n)continue;const r=Kt(n),c=e.uuidV4(),d=s();o().then(n=>{var o;t.createAnnotation(a.pageIndex,{type:e.PdfAnnotationSubtype.CARET,id:c,pageIndex:a.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(a.pageIndex,c)})}}},qt={toolId:"replaceText",handle(t,n,o){const i=t.getTool();if(!i)return;const s=()=>({strokeColor:i.defaults.strokeColor??"#E44234",opacity:i.defaults.opacity??1,flags:i.defaults.flags??["print"]});for(const a of n){const n=a.segmentRects[a.segmentRects.length-1];if(!n)continue;const r=Kt(n),c=e.uuidV4(),d=e.uuidV4(),l=s();o().then(n=>{var o;t.createAnnotation(a.pageIndex,{type:e.PdfAnnotationSubtype.CARET,id:c,pageIndex:a.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(a.pageIndex,{type:e.PdfAnnotationSubtype.STRIKEOUT,id:d,pageIndex:a.pageIndex,rect:a.rect,segmentRects:a.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(a.pageIndex,c)})}}},Xt=(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}=zt(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{oldRect:e,resolvedRect:o,rects:i}=wt(t,n.changes.rect,n.metadata),s=(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)}}),a=Math.min(o.size.width/e.size.width,o.size.height/e.size.height),r=Math.max(1,Math.round(t.strokeWidth*a)),c=s(e,t.strokeWidth/2),d=s(o,r/2),l=d.size.width/Math.max(c.size.width,1e-6),u=d.size.height/Math.max(c.size.height,1e-6);return{...i,inkList:t.inkList.map(t=>({points:t.points.map(t=>({x:d.origin.x+(t.x-c.origin.x)*l,y:d.origin.y+(t.y-c.origin.y)*u}))})),strokeWidth:r}}case"rotate":{const e=Et(t,n);if(!e)return n.changes;const{dx:o,dy:i}=jt(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),s=e.expandRect(e.rectFromPoints(i),o.strokeWidth/2),a=n.changes.rotation??t.rotation??0;return t.unrotatedRect||void 0!==n.changes.rotation?{...n.changes,unrotatedRect:s,rect:e.calculateRotatedRectAABBAroundPoint(s,a,Rt(t))}:{...n.changes,rect:s}}}},Zt=(t,n)=>{switch(n.type){case"vertex-edit":if(n.changes.linePoints){const{start:e,end:o}=n.changes.linePoints,i=[e,o],s=Ct(t,i,Pt(i,t.strokeWidth,t.lineEndings));return{...St(t,Pt(s,t.strokeWidth,t.lineEndings)),linePoints:{start:s[0],end:s[1]}}}return n.changes;case"move":{if(!n.changes.rect)return n.changes;const{dx:e,dy:o,rects:i}=zt(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:s,rects:a}=wt(t,n.changes.rect,n.metadata);return{...a,linePoints:{start:{x:s.origin.x+(t.linePoints.start.x-i.origin.x)*e,y:s.origin.y+(t.linePoints.start.y-i.origin.y)*o},end:{x:s.origin.x+(t.linePoints.end.x-i.origin.x)*e,y:s.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}=jt(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=Pt([o.linePoints.start,o.linePoints.end],o.strokeWidth,o.lineEndings),s=n.changes.rotation??t.rotation??0;return t.unrotatedRect||void 0!==n.changes.rotation?{...n.changes,unrotatedRect:i,rect:e.calculateRotatedRectAABBAroundPoint(i,s,Rt(t))}:{...n.changes,rect:i}}default:return n.changes}},Jt=(t,n)=>{switch(n.type){case"vertex-edit":if(n.changes.vertices&&n.changes.vertices.length){const e=n.changes.vertices,o=Ct(t,e,Pt(e,t.strokeWidth,t.lineEndings));return{...St(t,Pt(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}=zt(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:s,rects:a}=wt(t,n.changes.rect,n.metadata);return{...a,vertices:t.vertices.map(t=>({x:s.origin.x+(t.x-i.origin.x)*e,y:s.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}=jt(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=Pt(o.vertices,o.strokeWidth,o.lineEndings),s=n.changes.rotation??t.rotation??0;return t.unrotatedRect||void 0!==n.changes.rotation?{...n.changes,unrotatedRect:i,rect:e.calculateRotatedRectAABBAroundPoint(i,s,Rt(t))}:{...n.changes,rect:i}}default:return n.changes}},te=(t,n)=>{switch(n.type){case"vertex-edit":if(n.changes.vertices&&n.changes.vertices.length){const o=t.strokeWidth/2,i=n.changes.vertices,s=Ct(t,i,e.expandRect(e.rectFromPoints(i),o));return{...St(t,e.expandRect(e.rectFromPoints(s),o)),vertices:s}}return n.changes;case"move":{if(!n.changes.rect)return n.changes;const{dx:e,dy:o,rects:i}=zt(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:s,rects:a}=wt(t,n.changes.rect,n.metadata);return{...a,vertices:t.vertices.map(t=>({x:s.origin.x+(t.x-i.origin.x)*e,y:s.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}=jt(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.rotation))return n.changes;const o={...t,...n.changes},i=o.strokeWidth/2,s=e.expandRect(e.rectFromPoints(o.vertices),i),a=n.changes.rotation??t.rotation??0;return t.unrotatedRect||void 0!==n.changes.rotation?{...n.changes,unrotatedRect:s,rect:e.calculateRotatedRectAABBAroundPoint(s,a,Rt(t))}:{...n.changes,rect:s}}default:return n.changes}},ee=(t,e)=>{switch(e.type){case"move":return e.changes.rect?zt(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,...Dt(t,e.changes.rotation)}:e.changes;default:return e.changes}},ne=(t,e)=>{switch(e.type){case"move":return e.changes.rect?zt(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,...Dt(t,e.changes.rotation)}:e.changes;default:return e.changes}},oe=(t,e)=>{switch(e.type){case"move":return e.changes.rect?zt(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,...Dt(t,e.changes.rotation)}:e.changes;default:return e.changes}},ie=(t,e)=>{switch(e.type){case"move":return e.changes.rect?zt(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,...Dt(t,e.changes.rotation)}:e.changes;default:return e.changes}};function se(t,e,n,o){const i=o*Math.PI/180,s=Math.abs(Math.cos(i)),a=Math.abs(Math.sin(i)),r=s*s-a*a,c=t.size.width,d=t.size.height;let l,u;if(Math.abs(r)>1e-6)l=(s*c-a*d)/r,u=(s*d-a*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 ae=class extends t.BasePlugin{constructor(e,n,o){var i,s,a;super(e,n),this.ANNOTATION_HISTORY_TOPIC="annotations",this.state$=t.createBehaviorEmitter(),this.pendingContexts=new Map,this.isInitialLoadComplete=new Map,this.importQueue=new Map,this.commitInProgress=new Map,this.handlerFactories=new Map,this.selectionHandlerFactories=new Map,this.activeTool$=t.createBehaviorEmitter(),this.events$=t.createBehaviorEmitter(),this.toolsChange$=t.createBehaviorEmitter(),this.patchRegistry=new kt,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.config=o,this.selection=(null==(i=n.getPlugin("selection"))?void 0:i.provides())??null,this.history=(null==(s=n.getPlugin("history"))?void 0:s.provides())??null,this.interactionManager=(null==(a=n.getPlugin("interaction-manager"))?void 0:a.provides())??null,this.registerHandlerFactories(),this.registerSelectionHandlerFactories(),this.registerBuiltInPatches()}onDocumentLoadingStarted(t){this.dispatch(function(t,e){return{type:i,payload:{documentId:t,state:e}}}(t,{pages:{},byUid:{},selectedUids:[],selectedUid:null,activeToolId:null,hasPendingChanges:!1})),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:s,payload:t}}(t)),this.pendingContexts.delete(t),this.isInitialLoadComplete.delete(t),this.importQueue.delete(t),this.appearanceCache.delete(t),this.logger.debug("AnnotationPlugin","DocumentClosed",`Cleaned up annotation state for document: ${t}`)}registerHandlerFactories(){this.handlerFactories.set(e.PdfAnnotationSubtype.CIRCLE,Vt),this.handlerFactories.set(e.PdfAnnotationSubtype.SQUARE,Wt),this.handlerFactories.set(e.PdfAnnotationSubtype.STAMP,_t),this.handlerFactories.set(e.PdfAnnotationSubtype.POLYGON,Ht),this.handlerFactories.set(e.PdfAnnotationSubtype.POLYLINE,$t),this.handlerFactories.set(e.PdfAnnotationSubtype.LINE,Gt),this.handlerFactories.set(e.PdfAnnotationSubtype.INK,yt),this.handlerFactories.set(e.PdfAnnotationSubtype.FREETEXT,Bt),this.handlerFactories.set(e.PdfAnnotationSubtype.TEXT,Ut)}registerSelectionHandlerFactories(){this.selectionHandlerFactories.set("insertText",Qt),this.selectionHandlerFactories.set("replaceText",qt)}registerBuiltInPatches(){this.patchRegistry.register(e.PdfAnnotationSubtype.INK,Xt),this.patchRegistry.register(e.PdfAnnotationSubtype.LINE,Zt),this.patchRegistry.register(e.PdfAnnotationSubtype.POLYLINE,Jt),this.patchRegistry.register(e.PdfAnnotationSubtype.POLYGON,te),this.patchRegistry.register(e.PdfAnnotationSubtype.CIRCLE,ee),this.patchRegistry.register(e.PdfAnnotationSubtype.SQUARE,ne),this.patchRegistry.register(e.PdfAnnotationSubtype.FREETEXT,oe),this.patchRegistry.register(e.PdfAnnotationSubtype.STAMP,ie)}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(T(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 s=(i.pages[e]??[]).map(t=>i.byUid[t]).filter(t=>void 0!==t).filter(t=>Q(t)).filter(t=>z(n,t.object.rect)).map(t=>t.object.id);if(s.length>0){const e=new Set;for(const n of s)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,s;if(!this.checkPermission(t,e.PdfPermissionFlag.ModifyAnnotations))return;const a=this.getActiveTool(t);if(!a||!a.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:a.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)};(this.selectionHandlerFactories.get(a.id)??Yt).handle(d,r,()=>new Promise(t=>{c.wait(e=>t(e.join("\n")),()=>t(void 0))})),(null==(i=a.behavior)?void 0:i.deactivateToolAfterCreate)&&this.setActiveTool(null,t),null==(s=this.selection)||s.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=>this.setActiveTool(t),getState:()=>this.getDocumentState(),getPageAnnotations:t=>this.getPageAnnotations(t),getSelectedAnnotation:()=>this.getSelectedAnnotation(),getSelectedAnnotations:()=>this.getSelectedAnnotationsMethod(),getSelectedAnnotationIds:()=>this.getSelectedAnnotationIdsMethod(),getAnnotationById:t=>this.getAnnotationById(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),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),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),forDocument:t=>this.createAnnotationScope(t),getTools:()=>this.state.tools,getTool:t=>this.getTool(t),addTool:t=>{this.dispatch((t=>({type:I,payload:t}))(t)),this.registerInteractionForTool(t)},findToolForAnnotation:t=>this.findToolForAnnotation(t),setToolDefaults:(t,e)=>this.dispatch(((t,e)=>({type:b,payload:{toolId:t,patch:e}}))(t,e)),getColorPresets:()=>[...this.state.colorPresets],addColorPreset:t=>this.dispatch({type:x,payload:t}),transformAnnotation:(t,e)=>this.transformAnnotation(t,e),registerPatchFunction:(t,e)=>this.registerPatchFunction(t,e),onStateChange:this.state$.on,onActiveToolChange:this.activeTool$.on,onAnnotationEvent:this.events$.on,onToolsChange:this.toolsChange$.on}}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),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=>this.setActiveTool(e,t),findToolForAnnotation:t=>this.findToolForAnnotation(t),importAnnotations:e=>this.importAnnotations(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),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),onStateChange:e=>this.state$.on(n=>{n.documentId===t&&e(n.state)}),onAnnotationEvent:e=>this.events$.on(n=>{n.documentId===t&&e(n)}),onActiveToolChange:e=>this.activeTool$.on(n=>{n.documentId===t&&e(n.tool)})}}onStoreUpdated(t,e){for(const n in e.documents){const o=t.documents[n],i=e.documents[n];o!==i&&(this.state$.emit({documentId:n,state:i}),o&&o.activeToolId!==i.activeToolId&&this.activeTool$.emit({documentId:n,tool: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({documentId:t,tool:this.getActiveTool(t)});this.toolsChange$.emit({tools:e.tools})}}registerPatchFunction(t,e){this.patchRegistry.register(t,e)}transformAnnotation(t,e){const n={type:e.type,changes:e.changes,metadata:e.metadata};return this.patchRegistry.transform(t,n)}registerPageHandlers(t,e,n,o){var i;const s=this.getCoreDocument(t),a=null==(i=null==s?void 0:s.document)?void 0:i.pages[e];if(!a)return()=>{};if(!this.interactionManager)return()=>{};const r=[],c=((a.rotation??0)+((null==s?void 0:s.rotation)??0))%4;for(const d of this.state.tools){if(!d.defaults.type)continue;const i=this.handlerFactories.get(d.defaults.type);if(!i)continue;const s={pageIndex:e,pageSize:a.size,pageRotation:c,scale:n,services:o.services,onPreview:t=>o.onPreview(d.id,t),onCommit:(n,o)=>{var i,s;this.createAnnotation(e,n,o,t),(null==(i=d.behavior)?void 0:i.deactivateToolAfterCreate)&&this.setActiveTool(null,t),(null==(s=d.behavior)?void 0:s.selectAfterCreate)&&this.selectAnnotation(e,n.id,t)},getTool:()=>this.state.tools.find(t=>t.id===d.id)},l=this.interactionManager.registerHandlers({documentId:t,modeId:d.interaction.mode??d.id,handlers:i.create(s),pageIndex:e});r.push(l)}return()=>r.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:a,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({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(),s=this.getCoreDocument(i),a=null==s?void 0:s.document;if(!a)return e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Document not found"});const r=a.pages.find(t=>t.index===o);return r?this.engine.getPageAnnotations(a,r):e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Page not found"})}getSelectedAnnotation(t){return X(this.getDocumentState(t))}getAnnotationById(t,e){const n=this.getDocumentState(e);return tt(n,t)}renderAnnotation({pageIndex:t,annotation:n,options:o},i){const s=i??this.getActiveDocumentId(),a=this.getCoreDocument(s),r=null==a?void 0:a.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(),s=this.getCoreDocument(i),a=null==s?void 0:s.document;if(!a)return e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Document not found"});const r=a.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(a,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 s=i.get(e);s&&delete s[t]}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,s=e.pageIndex,a=e.id;this.dispatch(C(t,s,e)),i&&n.set(a,i)}!1!==this.config.autoCommit&&this.commit(t)}}createAnnotation(t,n,o,i){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 a=n.id,r=this.pendingContexts.get(s);if(!r)return;const c={...n,author:n.author??this.config.annotationAuthor},d=()=>{this.dispatch(C(s,t,c)),o&&r.set(a,o),this.events$.emit({type:"create",documentId:s,annotation:c,pageIndex:t,ctx:o,committed:!1})};if(!this.history)return d(),void(this.config.autoCommit&&this.commit(s));const l={execute:d,undo:()=>{r.delete(a),this.dispatch(v(s)),this.dispatch(E(s,t,a)),this.events$.emit({type:"delete",documentId:s,annotation:c,pageIndex:t,committed:!1})},metadata:{annotationIds:[a]}};this.history.forDocument(s).register(l,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 s=i??this.getActiveDocumentId();if(!this.checkPermission(s,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","UpdateAnnotation",`Cannot update annotation: document ${s} lacks ModifyAnnotations permission`);const a=this.getDocumentState(s).byUid[n].object,r=this.buildPatch(a,{...o,author:o.author??this.config.annotationAuthor}),c=()=>{this.dispatch(k(s,t,n,r)),this.events$.emit({type:"update",documentId:s,annotation:a,pageIndex:t,patch:r,committed:!1})};if(!this.history)return c(),void(!1!==this.config.autoCommit&&this.commit(s));const d=Object.fromEntries(Object.keys(o).map(t=>[t,a[t]])),l={execute:c,undo:()=>{this.dispatch(k(s,t,n,d)),this.events$.emit({type:"update",documentId:s,annotation:a,pageIndex:t,patch:d,committed:!1})},metadata:{annotationIds:[n]}};this.history.forDocument(s).register(l,this.ANNOTATION_HISTORY_TOPIC)}deleteAnnotation(t,n,o){var i;const s=o??this.getActiveDocumentId();if(!this.checkPermission(s,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","DeleteAnnotation",`Cannot delete annotation: document ${s} lacks ModifyAnnotations permission`);const a=this.getDocumentState(s),r=null==(i=a.byUid[n])?void 0:i.object;if(!r)return;const c=nt(a,n),d=c.map(t=>{var e;return null==(e=a.byUid[t.id])?void 0:e.object}).filter(t=>void 0!==t),l=()=>{var e;for(const t of c){const n=null==(e=a.byUid[t.id])?void 0:e.object;n&&(this.dispatch(E(s,t.pageIndex,t.id)),this.events$.emit({type:"delete",documentId:s,annotation:n,pageIndex:t.pageIndex,committed:!1}))}this.dispatch(v(s)),this.dispatch(E(s,t,n)),this.events$.emit({type:"delete",documentId:s,annotation:r,pageIndex:t,committed:!1})};if(!this.history)return l(),void(!1!==this.config.autoCommit&&this.commit(s));const u={execute:l,undo:()=>{this.dispatch(C(s,t,r)),this.events$.emit({type:"create",documentId:s,annotation:r,pageIndex:t,committed:!1});for(const t of d)this.dispatch(C(s,t.pageIndex,t)),this.events$.emit({type:"create",documentId:s,annotation:t,pageIndex:t.pageIndex,committed:!1})},metadata:{annotationIds:[n,...c.map(t=>t.id)]}};this.history.forDocument(s).register(u,this.ANNOTATION_HISTORY_TOPIC)}deleteAnnotationsMethod(t,e){for(const{pageIndex:n,id:o}of t)this.deleteAnnotation(n,o,e)}purgeAnnotationMethod(t,e,n){const o=n??this.getActiveDocumentId();this.dispatch(D(o,t,e))}selectAnnotation(t,e,n){const o=n??this.getActiveDocumentId();if(this.isInGroupMethod(e,o)){const t=this.getGroupMembersMethod(e,o).map(t=>t.object.id);this.dispatch(R(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(v(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],s=e.byUid[i];s&&(null==(o=this.interactionManager)||o.claimPageActivity(t,"annotation-selection",s.object.pageIndex))}getSelectedAnnotationsMethod(t){return Z(this.getDocumentState(t))}getSelectedAnnotationIdsMethod(t){return J(this.getDocumentState(t))}toggleSelectionMethod(t,e,n){const o=n??this.getActiveDocumentId(),i=this.getDocumentState(o);if(i.selectedUids.includes(e))this.dispatch(S(o,e));else if(this.isInGroupMethod(e,o)){const t=this.getGroupMembersMethod(e,o);for(const e of t)i.selectedUids.includes(e.object.id)||this.dispatch(P(o,e.object.pageIndex,e.object.id))}else this.dispatch(P(o,t,e))}addToSelectionMethod(t,e,n){const o=n??this.getActiveDocumentId();this.dispatch(P(o,t,e))}removeFromSelectionMethod(t,e){const n=e??this.getActiveDocumentId();this.dispatch(S(n,t))}setSelectionMethod(t,e){const n=e??this.getActiveDocumentId();this.dispatch(R(n,t))}getAttachedLinksMethod(t,e){return it(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],s=o.slice(1).map(t=>({pageIndex:t.object.pageIndex,id:t.object.id,patch:{inReplyToId:i.object.id,replyType:e.PdfAnnotationReplyType.Group}}));this.updateAnnotationsMethod(s,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 at(this.getDocumentState(e),t)}isInGroupMethod(t,e){return rt(this.getDocumentState(e),t)}getGroupingActionMethod(t){return ct(this.getDocumentState(t))}computeCombinedConstraints(t){let e=1/0,n=1/0,o=1/0,i=1/0;for(const s of t){const t=s.rect.origin.y,a=s.pageSize.height-(s.rect.origin.y+s.rect.size.height),r=s.rect.origin.x,c=s.pageSize.width-(s.rect.origin.x+s.rect.size.width);e=Math.min(e,t),n=Math.min(n,a),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 s=[...n,...i],a=new Map,r=[];for(const d of s){const e=this.getAnnotationById(d,t);e&&(a.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:s,originalRects:a,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 s=t.originalRects.get(o);if(!s)continue;const a={...s,origin:{x:s.origin.x+t.delta.x,y:s.origin.y+t.delta.y}};n[o]=this.transformAnnotation(i.object,{type:"move",changes:{rect:a}})}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 s=this.computeDragPreviewPatches(i,t);return this.unifiedDrag$.emit({documentId:t,type:"update",state:i,previewPatches:s}),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 s=this.getAnnotationById(i,t);if(!s)continue;const a=e.originalRects.get(i)??s.object.rect,r={...a,origin:{x:a.origin.x+n.x,y:a.origin.y+n.y}},c=this.transformAnnotation(s.object,{type:"move",changes:{rect:r}});o.push({pageIndex:s.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 s of t)e=Math.min(e,s.origin.x),n=Math.min(n,s.origin.y),o=Math.max(o,s.origin.x+s.size.width),i=Math.max(i,s.origin.y+s.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),s=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:s}})}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[s,a]of t){const t=this.getAnnotationById(s,e);if(!t)continue;let r=a;const c=null==i?void 0:i.get(s);(null==o?void 0:o.isGroupResize)&&(null==c?void 0:c.originalUnrotatedRect)&&(r=se(a,c.originalRect,c.originalUnrotatedRect,t.object.rotation??0)),n[s]=this.transformAnnotation(t.object,{type:"resize",changes:{rect:r}})}return n}startResize(t,e){const{annotationIds:n,pageSize:o,resizeHandle:i}=e,s=[],a=[];for(const p of n){const e=this.getAnnotationById(p,t);if(e){a.push({id:p,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(p,t);for(const t of n)s.includes(t.object.id)||(s.push(t.object.id),a.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:p}))}}const r=[...n,...s],c=a.map(t=>t.rect),d=this.computeUnifiedGroupBoundingBox(c),l=this.computeUnifiedRelativePositions(a,d),u=this.computeUnifiedResizedRects(l,d),h={documentId:t,isResizing:!0,isGroupResize:n.length>1,primaryIds:n,attachedLinkIds:s,allParticipantIds:r,originalGroupBox:d,currentGroupBox:d,participatingAnnotations:l,resizeHandle:i,computedRects:u};this.unifiedResizeStates.set(t,h);const g=this.computeResizePreviewPatches(u,t);this.unifiedResize$.emit({documentId:t,type:"start",state:h,computedRects:Object.fromEntries(u),previewPatches:g})}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 s=Object.fromEntries(o),a=this.computeResizePreviewPatches(o,t);return this.unifiedResize$.emit({documentId:t,type:"update",state:i,computedRects:s,previewPatches:a}),s}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[a,r]of n){const n=this.getAnnotationById(a,t);if(!n)continue;let s=r;const c=i.get(a);e.isGroupResize&&(null==c?void 0:c.originalUnrotatedRect)&&(s=se(r,c.originalRect,c.originalUnrotatedRect,n.object.rotation??0));const d=this.transformAnnotation(n.object,{type:"resize",changes:{rect:s}});o.push({pageIndex:n.object.pageIndex,id:a,patch:d})}o.length>0&&this.updateAnnotationsMethod(o,t);const s=this.computeResizePreviewPatches(n,t);this.unifiedResize$.emit({documentId:t,type:"end",state:{...e,isResizing:!1},computedRects:Object.fromEntries(n),previewPatches:s}),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 s=this.getAttachedLinksMethod(i,e);for(const e of s)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 s=this.getAnnotationById(i.id,n);if(!s)continue;const a=Rt({rect:i.rect,unrotatedRect:i.unrotatedRect,rotation:i.rotation}),r=e.rotatePointAround(a,t.rotationCenter,t.delta),c={x:r.x-a.x,y:r.y-a.y},d=this.normalizeAngle(i.rotation+t.delta),l=this.transformAnnotation(s.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:s.object.rect.origin.x+c.x,y:s.object.rect.origin.y+c.y},size:{...s.object.rect.size}}),o[i.id]=l}return o}startRotation(t,e){const{annotationIds:n,cursorAngle:o,rotationCenter:i}=e,{participants:s,attachedLinkIds:a}=this.buildRotationParticipants(n,t);if(0===s.length)return;const r=s.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:a,allParticipantIds:s.map(t=>t.id),rotationCenter:d,cursorStartAngle:o,currentAngle:o,delta:0,participants:s};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),s={...o,currentAngle:e,delta:i};this.unifiedRotateStates.set(t,s);const a=this.computeRotatePreviewPatches(s,t);this.unifiedRotate$.emit({documentId:t,type:"update",state:s,previewPatches:a})}commitRotation(t){const e=this.unifiedRotateStates.get(t);if(!e)return;const n=this.computeRotatePreviewPatches(e,t),o=[];for(const[i,s]of Object.entries(n)){const e=this.getAnnotationById(i,t);e&&o.push({pageIndex:e.object.pageIndex,id:i,patch:s})}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),s=t.map(({pageIndex:t,id:e,patch:n})=>{var o;const s=null==(o=i.byUid[e])?void 0:o.object;if(!s)return null;return{pageIndex:t,id:e,patch:this.buildPatch(s,{...n,author:n.author??this.config.annotationAuthor}),originalObject:s}}).filter(t=>null!==t);if(0===s.length)return;const a=()=>{for(const{pageIndex:t,id:e,patch:n,originalObject:i}of s)this.dispatch(k(o,t,e,n)),this.events$.emit({type:"update",documentId:o,annotation:i,pageIndex:t,patch:n,committed:!1})};if(!this.history)return a(),void(!1!==this.config.autoCommit&&this.commit(o));const r=s.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:a,undo:()=>{for(const{pageIndex:t,id:e,originalPatch:n,originalObject:i}of r)this.dispatch(k(o,t,e,n)),this.events$.emit({type:"update",documentId:o,annotation:i,pageIndex:t,patch:n,committed:!1})},metadata:{annotationIds:s.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),s=t.map(({pageIndex:t,id:e,patch:n})=>{var o;const s=null==(o=i.byUid[e])?void 0:o.object;return s?{pageIndex:t,id:e,patch:n,originalObject:s}:null}).filter(t=>null!==t);if(0===s.length)return;const a=()=>{for(const{pageIndex:t,id:e,patch:n,originalObject:i}of s)this.dispatch(M(o,t,e,n)),this.events$.emit({type:"update",documentId:o,annotation:i,pageIndex:t,patch:n,committed:!1})};if(!this.history)return a(),void(!1!==this.config.autoCommit&&this.commit(o));const r=s.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:a,undo:()=>{for(const{pageIndex:t,id:e,originalPatch:n,originalObject:i}of r)this.dispatch(M(o,t,e,n)),this.events$.emit({type:"update",documentId:o,annotation:i,pageIndex:t,patch:n,committed:!1})},metadata:{annotationIds:s.map(t=>t.id)}};this.history.forDocument(o).register(c,this.ANNOTATION_HISTORY_TOPIC)}moveAnnotationMethod(t,e,n,o="delta",i){const s=i??this.getActiveDocumentId(),a=this.getAnnotationById(e,s);if(!a)return;const r=a.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(a.object,{type:"move",changes:{rect:c}});this.moveAnnotationsMethod([{pageIndex:t,id:e,patch:d}],s)}getActiveTool(t){const e=this.getDocumentState(t);return e.activeToolId?this.state.tools.find(t=>t.id===e.activeToolId)??null:null}setActiveTool(t,n){var o,i;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 a=this.getDocumentState(s);if(t===a.activeToolId)return;this.dispatch(T(s,t));const r=this.state.tools.find(e=>e.id===t);r?null==(o=this.interactionManager)||o.forDocument(s).activate(r.interaction.mode??r.id):null==(i=this.interactionManager)||i.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}collectPendingChanges(t,e){const n=this.getDocumentState(t),o=this.pendingContexts.get(t),i={creations:[],updates:[],deletions:[],committedUids:[],isEmpty:!0};for(const[s,a]of Object.entries(n.byUid)){if("synced"===a.commitState)continue;if(e.pages.find(t=>t.index===a.object.pageIndex))switch(i.committedUids.push(s),i.isEmpty=!1,a.commitState){case"new":i.creations.push({uid:s,ta:a,ctx:null==o?void 0:o.get(a.object.id)});break;case"moved":i.updates.push({uid:s,ta:a,moved:!0});break;case"dirty":i.updates.push({uid:s,ta:a});break;case"deleted":i.deletions.push({uid:s,ta:a})}}return i}executeCommitBatch(t,n,o){const i=new e.Task,s=this.pendingContexts.get(t),a=[];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);a.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);a.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),a.push({type:"delete",task:t,ta:d,uid:c})}else this.dispatch(D(t,d.object.pageIndex,c))}const r=a.map(t=>t.task);return e.Task.allSettled(r).wait(()=>{this.emitCommitEvents(t,a,s);for(const o of a)"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({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({type:"update",documentId:t,annotation:i.ta.object,pageIndex:i.ta.object.pageIndex,patch:i.ta.object,committed:!0});break;case"delete":this.dispatch(D(t,i.ta.object.pageIndex,i.uid)),this.events$.emit({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 s=this.collectPendingChanges(n,i);if(s.isEmpty)return this.releaseCommitLock(n),e.PdfTaskHelper.resolve(!0);const a=new e.Task;return this.executeCommitBatch(n,i,s).wait(()=>{this.releaseCommitLock(n);this.getDocumentState(n).hasPendingChanges?this.commit(n).wait(t=>a.resolve(t),t=>a.fail(t)):a.resolve(!0)},t=>{this.releaseCommitLock(n),a.fail(t)}),a}};ae.id="annotation";let re=ae;function ce(t){return e=>(null==e?void 0:e.id)===t}const de=ce("highlight"),le=ce("squiggly"),ue=ce("underline"),he=ce("strikeout"),ge=ce("ink"),pe=ce("inkHighlighter"),fe=ce("square"),me=ce("circle"),ye=ce("line"),Ae=ce("polyline"),xe=ce("polygon"),be=ce("freeText"),Ie=ce("stamp"),ve=ce("insertText"),Pe=ce("replaceText"),Se={manifest:o,create:(t,e)=>new re(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 s:{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 a:{const{documentId:n,annotations:o}=e.payload,i=t.documents[n];if(!i)return t;const s={},a={};for(const[t,e]of Object.entries(o)){const n=Number(t),o=i.pages[n]||[];for(const t of o)delete a[t];const r=e.map(t=>{const e=t.id;return a[e]={commitState:"synced",object:t},e});s[n]=r}return{...t,documents:{...t.documents,[n]:{...i,pages:s,byUid:a}}}}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 s=[...i.selectedUids,o];return{...t,documents:{...t.documents,[n]:{...i,selectedUids:s,selectedUid:ut(s)}}}}case l:{const{documentId:n,id:o}=e.payload,i=t.documents[n];if(!i)return t;const s=i.selectedUids.filter(t=>t!==o);return{...t,documents:{...t.documents,[n]:{...i,selectedUids:s,selectedUid:ut(s)}}}}case u:{const{documentId:n,ids:o}=e.payload,i=t.documents[n];return i?{...t,documents:{...t.documents,[n]:{...i,selectedUids:o,selectedUid:ut(o)}}}:t}case h:{const{documentId:n,toolId:o}=e.payload,i=t.documents[n];return i?{...t,documents:{...t.documents,[n]:{...i,activeToolId:o}}}:t}case g:{const{documentId:n,pageIndex:o,annotation:i}=e.payload,s=t.documents[n];if(!s)return t;const a=i.id;return{...t,documents:{...t.documents,[n]:{...s,pages:{...s.pages,[o]:[...s.pages[o]??[],a]},byUid:{...s.byUid,[a]:{commitState:"new",object:i,dictMode:!0}},hasPendingChanges:!0}}}}case m:{const{documentId:n,pageIndex:o,id:i}=e.payload,s=t.documents[n];return s&&s.byUid[i]?{...t,documents:{...t.documents,[n]:{...s,pages:{...s.pages,[o]:(s.pages[o]??[]).filter(t=>t!==i)},byUid:{...s.byUid,[i]:{...s.byUid[i],commitState:"deleted"}},hasPendingChanges:!0}}}:t}case p:{const{documentId:n,id:o,patch:i}=e.payload,s=t.documents[n];return s?{...t,documents:{...t.documents,[n]:gt(s,o,i)}}:t}case f:{const{documentId:n,id:o,patch:i}=e.payload,s=t.documents[n];return s?{...t,documents:{...t.documents,[n]:pt(s,o,i)}}:t}case y:{const{documentId:n,committedUids:o}=e.payload,i=t.documents[n];if(!i)return t;const s=new Set(o),a={};let r=!1;for(const[t,e]of Object.entries(i.byUid))s.has(t)?a[t]={...e,commitState:"dirty"===e.commitState||"new"===e.commitState||"moved"===e.commitState?"synced":e.commitState}:(a[t]=e,"new"!==e.commitState&&"dirty"!==e.commitState&&"moved"!==e.commitState&&"deleted"!==e.commitState||(r=!0));return{...t,documents:{...t.documents,[n]:{...i,byUid:a,hasPendingChanges:r}}}}case A:{const{documentId:n,pageIndex:o,uid:i}=e.payload,s=t.documents[n];if(!s||!s.byUid[i])return t;const{[i]:a,...r}=s.byUid;return{...t,documents:{...t.documents,[n]:{...s,pages:{...s.pages,[o]:(s.pages[o]??[]).filter(t=>t!==i)},byUid:r}}}}case I:{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 b:{const{toolId:n,patch:o}=e.payload;return{...t,tools:t.tools.map(t=>t.id===n?{...t,defaults:{...t.defaults,...o}}:t)}}case x:return t.colorPresets.includes(e.payload)?t:{...t,colorPresets:[...t.colorPresets,e.payload]};default:return t}},initialState:(t,e)=>ft(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=re,exports.AnnotationPluginPackage=Se,exports.convertAABBRectToUnrotatedSpace=se,exports.createToolPredicate=ce,exports.getAnnotationByUid=tt,exports.getAnnotations=t=>{const e={};for(const n of Object.keys(t.pages).map(Number))e[n]=q(t,n);return e},exports.getAnnotationsByPageIndex=q,exports.getAttachedLinks=it,exports.getGroupLeaderId=st,exports.getGroupMembers=at,exports.getIRTChildIds=nt,exports.getIRTChildrenByType=ot,exports.getSelectedAnnotation=X,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=J,exports.getSelectedAnnotations=Z,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=ct,exports.getSidebarAnnotationsWithReplies=t=>{const e=et(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=et,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)=>it(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.hasMultipleSelected=t=>t.selectedUids.length>1,exports.initialDocumentState=ht,exports.initialState=ft,exports.isAnnotationSelected=(t,e)=>t.selectedUids.includes(e),exports.isCaret=K,exports.isCircle=j,exports.isCircleTool=me,exports.isFreeText=W,exports.isFreeTextTool=be,exports.isHighlight=F,exports.isHighlightTool=de,exports.isInGroup=rt,exports.isInk=w,exports.isInkHighlighterTool=pe,exports.isInkTool=ge,exports.isInsertTextTool=ve,exports.isLine=N,exports.isLineTool=ye,exports.isLink=function(t){return t.object.type===e.PdfAnnotationSubtype.LINK},exports.isPolygon=L,exports.isPolygonTool=xe,exports.isPolyline=U,exports.isPolylineTool=Ae,exports.isRedact=Y,exports.isReplaceTextTool=Pe,exports.isSidebarAnnotation=Q,exports.isSquare=O,exports.isSquareTool=fe,exports.isSquiggly=$,exports.isSquigglyTool=le,exports.isStamp=_,exports.isStampTool=Ie,exports.isStrikeout=G,exports.isStrikeoutTool=he,exports.isText=V,exports.isTextMarkup=H,exports.isUnderline=B,exports.isUnderlineTool=ue,exports.manifest=o,exports.patching=Nt,exports.rectsIntersect=z,exports.resolveInteractionProp=function(t,e,n){return void 0===t?n:"function"==typeof t?t(e):t};
|
|
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"],defaultConfig:{autoCommit:!0,annotationAuthor:"Guest",deactivateToolAfterCreate:!1,selectAfterCreate:!0}},i="ANNOTATION/INIT_STATE",s="ANNOTATION/CLEANUP_STATE",a="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",g="ANNOTATION/CREATE_ANNOTATION",p="ANNOTATION/PATCH_ANNOTATION",f="ANNOTATION/MOVE_ANNOTATION",y="ANNOTATION/DELETE_ANNOTATION",m="ANNOTATION/COMMIT",A="ANNOTATION/PURGE_ANNOTATION",x="ANNOTATION/ADD_COLOR_PRESET",b="ANNOTATION/SET_TOOL_DEFAULTS",I="ANNOTATION/ADD_TOOL";const v=t=>({type:c,payload:{documentId:t}}),P=(t,e,n)=>({type:d,payload:{documentId:t,pageIndex:e,id:n}}),S=(t,e)=>({type:l,payload:{documentId:t,id:e}}),R=(t,e)=>({type:u,payload:{documentId:t,ids:e}}),T=(t,e)=>({type:h,payload:{documentId:t,toolId:e}}),M=(t,e,n)=>({type:g,payload:{documentId:t,pageIndex:e,annotation:n}}),C=(t,e,n,o)=>({type:p,payload:{documentId:t,pageIndex:e,id:n,patch:o}}),k=(t,e,n,o)=>({type:f,payload:{documentId:t,pageIndex:e,id:n,patch:o}}),D=(t,e,n)=>({type:y,payload:{documentId:t,pageIndex:e,id:n}}),E=(t,e,n)=>({type:A,payload:{documentId:t,pageIndex:e,uid:n}});function w(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 j(t){return t.object.type===e.PdfAnnotationSubtype.INK}function z(t){return t.object.type===e.PdfAnnotationSubtype.CIRCLE}function L(t){return t.object.type===e.PdfAnnotationSubtype.POLYGON}function O(t){return t.object.type===e.PdfAnnotationSubtype.SQUARE}function N(t){return t.object.type===e.PdfAnnotationSubtype.LINE}function U(t){return t.object.type===e.PdfAnnotationSubtype.POLYLINE}function B(t){return t.object.type===e.PdfAnnotationSubtype.HIGHLIGHT}function F(t){return t.object.type===e.PdfAnnotationSubtype.UNDERLINE}function $(t){return t.object.type===e.PdfAnnotationSubtype.STRIKEOUT}function G(t){return t.object.type===e.PdfAnnotationSubtype.SQUIGGLY}function W(t){return B(t)||F(t)||$(t)||G(t)}function H(t){return t.object.type===e.PdfAnnotationSubtype.FREETEXT}function _(t){return t.object.type===e.PdfAnnotationSubtype.STAMP}function Y(t){return t.object.type===e.PdfAnnotationSubtype.TEXT}function V(t){return t.object.type===e.PdfAnnotationSubtype.REDACT}function X(t){return t.object.type===e.PdfAnnotationSubtype.CARET}function K(t){return Y(t)&&!t.object.inReplyToId||W(t)||j(t)||O(t)||z(t)||L(t)||N(t)||U(t)||H(t)||_(t)||V(t)||X(t)}const q=(t,e)=>(t.pages[e]??[]).map(e=>t.byUid[e]),Q=t=>t.selectedUids.length>0?t.byUid[t.selectedUids[0]]??null:null,Z=t=>t.selectedUids.map(e=>t.byUid[e]).filter(t=>void 0!==t),J=t=>t.selectedUids,tt=(t,e)=>t.byUid[e]??null;const et=t=>{const n={};for(const e of Object.values(t.pages))for(const o of e){const e=t.byUid[o];if(e&&Y(e)){const t=e.object.inReplyToId;t&&(n[t]||(n[t]=[])).push(e)}}const o={},i=new Set;for(const a of Object.values(t.pages))for(const n of a){const s=t.byUid[n];if(s&&s.object.inReplyToId&&s.object.replyType===e.PdfAnnotationReplyType.Group&&s.object.type!==e.PdfAnnotationSubtype.LINK&&K(s)){const t=s.object.inReplyToId;(o[t]||(o[t]=[])).push(s),i.add(s.object.id)}}const s={};for(const[e,a]of Object.entries(t.pages)){const r=Number(e),c=[];for(const e of a){const s=t.byUid[e];if(!s||!K(s))continue;if(i.has(s.object.id))continue;const a=o[s.object.id];c.push({page:r,annotation:s,replies:n[s.object.id]??[],...a&&a.length>0?{groupMembers:a}:{}})}c.length>0&&(s[r]=c)}return s},nt=(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},ot=(t,e,n)=>{const o=[];for(const i of Object.values(t.pages))for(const s of i){const i=t.byUid[s];i&&"inReplyToId"in i.object&&i.object.inReplyToId===e&&n.includes(i.object.type)&&o.push(i)}return o},it=(t,n)=>ot(t,n,[e.PdfAnnotationSubtype.LINK]),st=(t,n)=>{const o=t.byUid[n];if(o)return o.object.inReplyToId&&o.object.replyType===e.PdfAnnotationReplyType.Group?o.object.inReplyToId:n},at=(t,n)=>{const o=st(t,n);if(!o)return[];const i=[],s=t.byUid[o];s&&s.object.type!==e.PdfAnnotationSubtype.LINK&&i.push(s);for(const a of Object.values(t.pages))for(const n of a){const s=t.byUid[n];s&&s.object.inReplyToId===o&&s.object.replyType===e.PdfAnnotationReplyType.Group&&s.object.type!==e.PdfAnnotationSubtype.LINK&&i.push(s)}return i},rt=(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},ct=t=>{const e=Z(t);if(0===e.length)return"disabled";const n=e[0].object.id;if(rt(t,n)){const o=at(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"},dt=[{id:"ink",name:"Pen",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}},{id:"inkHighlighter",name:"Ink Highlighter",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}}],lt=[{id:"highlight",name:"Highlight",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",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",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",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",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"}},{id:"replaceText",name:"Replace Text",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"}},...dt,{id:"circle",name:"Circle",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}}},{id:"square",name:"Square",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}}},{id:"line",name:"Line",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}},{id:"lineArrow",name:"Arrow",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}},{id:"polyline",name:"Polyline",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"}},{id:"polygon",name:"Polygon",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"}},{id:"textComment",name:"Comment",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}},{id:"freeText",name:"Free Text",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}},{id:"stamp",name:"Image",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}}],ut=["#E44234","#FF8D00","#FFCD45","#5CC96E","#25D2D1","#597CE2","#C544CE","#7D2E25","#000000","#FFFFFF"],ht=t=>1===t.length?t[0]:null,gt=()=>({pages:{},byUid:{},selectedUids:[],selectedUid:null,activeToolId:null,hasPendingChanges:!1}),pt=(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},ft=(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},yt=t=>{const e=new Map;lt.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;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}}}),colorPresets:t.colorPresets??ut}};function mt(t){let e=t;return[()=>e,t=>{e=t}]}const At={annotationType:e.PdfAnnotationSubtype.INK,create(n){const{onCommit:o,onPreview:i,getTool:s,pageSize:a}=n,[r,c]=mt([]),[d,l]=mt(!1),u={current:null},h=e=>({x:t.clamp(e.x,0,a.width),y:t.clamp(e.y,0,a.height)}),g=()=>{const t=s();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},p=()=>{const t=r();if(0===t.length||0===t[0].points.length)return null;const n=g();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 s=[...r(),{points:[o]}];c(s),i(p()),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(p())},onPointerUp:(t,a)=>{var d;l(!1),null==(d=a.releasePointerCapture)||d.call(a);const h=s(),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 s=Math.abs((o.x-n.x)*(n.y-e.y)-(n.x-e.x)*(o.y-n.y))/i;return Math.max(t,s)},0)/i<e}(n.points,t)){const t=n.points[0],o=n.points[n.points.length-1],s=o.x-t.x,a=o.y-t.y,r=Math.atan2(Math.abs(a),Math.abs(s))*(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(p())}}const y=(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=g();if(!i)return;const s=t.flatMap(t=>t.points),a=e.expandRect(e.rectFromPoints(s),i.strokeWidth/2);o({...i,inkList:t,rect:a,type:e.PdfAnnotationSubtype.INK,pageIndex:n.pageIndex,id:e.uuidV4(),created:new Date})}c([]),i(null)},y)},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 It=xt(!1),vt=xt(!0),Pt={[e.PdfAnnotationLineEnding.OpenArrow]:It,[e.PdfAnnotationLineEnding.ClosedArrow]:vt,[e.PdfAnnotationLineEnding.ROpenArrow]:{...It,getRotation:t=>t+Math.PI},[e.PdfAnnotationLineEnding.RClosedArrow]:{...vt,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],s=(t,e)=>Math.atan2(e.y-t.y,e.x-t.x),a=(t,o,s)=>{if(!t)return;const a=Pt[t];if(!a)return;const r=a.getLocalPoints(n),c=a.getRotation(s),d=r.map(t=>e.rotateAndTranslatePoint(t,c,o));i.push(...d)};if(t.length>=2){const e=s(t[1],t[0]);a(null==o?void 0:o.start,t[0],e);const n=t.length-1,i=s(t[n-1],t[n]);a(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 Rt(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 Tt(t){return t.unrotatedRect?e.inferRotationCenterFromRects(t.unrotatedRect,t.rect,t.rotation??0):e.getRectCenter(t.rect)}function Mt(t,n,o){const i=Tt(t),s=t.unrotatedRect??t.rect,a=n.origin.x-s.origin.x,r=n.origin.y-s.origin.y,c={x:i.x+a,y:i.y+r};return{rect:e.calculateRotatedRectAABBAroundPoint(n,o,c),unrotatedRect:n}}function Ct(t,n,o){if(!t.unrotatedRect)return n;const i=t.rotation??0;if(Math.abs(i%360)<1e-8)return n;const s=Tt(t),a=e.getRectCenter(o),r=i*Math.PI/180,c=Math.cos(r),d=Math.sin(r),l=s.x-a.x,u=s.y-a.y,h=(1-c)*l+d*u,g=-d*l+(1-c)*u;return Math.abs(h)<1e-8&&Math.abs(g)<1e-8?n:n.map(t=>({x:t.x+h,y:t.y+g}))}class kt{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 kt;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 wt(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 zt(t,n,o){const i=t.unrotatedRect??t.rect;let s=n.size.width/i.size.width,a=n.size.height/i.size.height;if((n.size.width<10||n.size.height<10)&&(s=Math.max(s,10/i.size.width),a=Math.max(a,10/i.size.height),n={origin:n.origin,size:{width:i.size.width*s,height:i.size.height*a}}),null==o?void 0:o.maintainAspectRatio){const t=Math.min(s,a);s=t,a=t,n={origin:n.origin,size:{width:i.size.width*t,height:i.size.height*t}}}return{scaleX:s,scaleY:a,oldRect:i,resolvedRect:n,rects:t.unrotatedRect?{unrotatedRect:n,rect:e.calculateRotatedRectAABB(n,t.rotation??0)}:{rect:n}}}function Lt(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 Ot(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:s,rect:a,unrotatedRect:r}=wt(i,o);return{...i,rotation:s,rect:a,unrotatedRect:r}}function Nt(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),s=o-e.rect.origin.x,a=i-e.rect.origin.y;return 0===s&&0===a?e:{...e,rect:{origin:{x:o,y:i},size:e.rect.size},...e.unrotatedRect?{unrotatedRect:{origin:{x:e.unrotatedRect.origin.x+s,y:e.unrotatedRect.origin.y+a},size:e.unrotatedRect.size}}:{}}}const Ut=Object.freeze(Object.defineProperty({__proto__:null,LINE_ENDING_HANDLERS:Pt,PatchRegistry:kt,applyInsertUpright:Ot,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:Nt,compensateRotatedVertexEdit:Ct,createEnding:function(t,e,n,o,i){if(!t)return null;const s=Pt[t];if(!s)return null;const a=s.getRotation(n);return{d:s.getSvgPath(e),transform:`translate(${o} ${i}) rotate(${r=a,180*r/Math.PI})`,filled:s.filled};var r},getRectCenter:e.getRectCenter,lineRectWithEndings:St,patchRegistry:Dt,resolveAnnotationRotationCenter:Tt,resolveRotateRects:Mt,resolveVertexEditRects:Rt,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:s=>{const a=o();if(!a)return;const r={origin:{x:s.x-12,y:s.y-12},size:{width:24,height:24}};let c={...a.defaults,rect:r,type:e.PdfAnnotationSubtype.TEXT,icon:a.defaults.icon??e.PdfAnnotationIcon.Comment,contents:a.defaults.contents??"",flags:a.defaults.flags??["print","noRotate","noZoom"],pageIndex:t.pageIndex,id:e.uuidV4(),created:new Date};c=Nt(c,i),n(c)}}}};function Ft({threshold:t=5,getTool:e,onClickDetected:n}){const[o,i]=mt(null),[s,a]=mt(!1);return{onStart:t=>{i(t),a(!1)},onMove:e=>{const n=o();if(!n||s())return;Math.sqrt(Math.pow(e.x-n.x,2)+Math.pow(e.y-n.y,2))>t&&a(!0)},onEnd:t=>{var r;if(o()&&!s()){const o=e();o&&"clickBehavior"in o&&(null==(r=o.clickBehavior)?void 0:r.enabled)&&n(t,o)}i(null),a(!1)},hasMoved:s,reset:()=>{i(null),a(!1)}}}const $t={annotationType:e.PdfAnnotationSubtype.FREETEXT,create(n){const{onCommit:o,onPreview:i,getTool:s,pageSize:a,pageIndex:r,pageRotation:c}=n,[d,l]=mt(null),u=e=>({x:t.clamp(e.x,0,a.width),y:t.clamp(e.y,0,a.height)}),h=()=>{const t=s();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},g=Ft({threshold:5,getTool:s,onClickDetected:(t,n)=>{var i;const s=h();if(!s)return;const d=n.clickBehavior;if(!(null==d?void 0:d.enabled))return;const{width:l,height:u}=d.defaultSize,g={origin:{x:t.x-l/2,y:t.y-u/2},size:{width:l,height:u}},p=d.defaultContent??s.contents;let f={...s,contents:p,type:e.PdfAnnotationSubtype.FREETEXT,rect:g,pageIndex:r,id:e.uuidV4(),created:new Date};(null==(i=n.behavior)?void 0:i.insertUpright)&&(f=Ot(f,c,!1)),f=Nt(f,a),o(f)}}),p=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),g.onStart(o),i(p(o)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=u(t);g.onMove(e),d()&&g.hasMoved()&&i(p(e))},onPointerUp:(t,a)=>{var r,p;const f=d();if(!f)return;const y=h();if(!y)return;const m=u(t);if(g.hasMoved()){const t={origin:{x:Math.min(f.x,m.x),y:Math.min(f.y,m.y)},size:{width:Math.abs(f.x-m.x),height:Math.abs(f.y-m.y)}},i=s();let a={...y,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)&&(a=Ot(a,c,!0)),o(a)}else g.onEnd(m);l(null),i(null),g.reset(),null==(p=a.releasePointerCapture)||p.call(a)},onPointerLeave:(t,e)=>{var n;l(null),i(null),g.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;l(null),i(null),g.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},Gt={annotationType:e.PdfAnnotationSubtype.LINE,create(n){const{pageIndex:o,onCommit:i,onPreview:s,getTool:a,pageSize:r}=n,[c,d]=mt(null),l=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.height)}),u=()=>{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,strokeStyle:t.defaults.strokeStyle??e.PdfAnnotationBorderStyle.SOLID,strokeDashArray:t.defaults.strokeDashArray??[],strokeColor:t.defaults.strokeColor??"#000000",flags:t.defaults.flags??["print"]}:null},h=Ft({threshold:5,getTool:a,onClickDetected:(t,n)=>{const s=u();if(!s)return;const a=n.clickBehavior;if(!(null==a?void 0:a.enabled))return;const r=a.defaultAngle??0,c=a.defaultLength/2,d=t.x-c*Math.cos(r),h=t.y-c*Math.sin(r),g=t.x+c*Math.cos(r),p=t.y+c*Math.sin(r),f=l({x:d,y:h}),y=l({x:g,y:p}),m=St([f,y],s.strokeWidth,s.lineEndings);i({...s,rect:m,linePoints:{start:f,end:y},pageIndex:o,id:e.uuidV4(),created:new Date,type:e.PdfAnnotationSubtype.LINE})}}),g=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),s(g(o)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=l(t);h.onMove(e),c()&&h.hasMoved()&&s(g(e))},onPointerUp:(t,n)=>{var a;const r=c();if(!r)return;const g=l(t);if(h.hasMoved()){const t=u();if(!t)return;if(Math.abs(g.x-r.x)>2||Math.abs(g.y-r.y)>2){const n=St([r,g],t.strokeWidth,t.lineEndings);i({...t,rect:n,linePoints:{start:r,end:g},pageIndex:o,id:e.uuidV4(),flags:["print"],created:new Date,type:e.PdfAnnotationSubtype.LINE})}}else h.onEnd(g);d(null),s(null),h.reset(),null==(a=n.releasePointerCapture)||a.call(n)},onPointerLeave:(t,e)=>{var n;d(null),s(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;d(null),s(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},Wt={annotationType:e.PdfAnnotationSubtype.POLYLINE,create(n){const{onCommit:o,onPreview:i,getTool:s,pageSize:a}=n,[r,c]=mt([]),[d,l]=mt(null),u=e=>({x:t.clamp(e.x,0,a.width),y:t.clamp(e.y,0,a.height)}),h=()=>{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,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=r(),n=d();if(0===t.length||!n)return null;const o=h();if(!o)return null;const i=[...t,n],s=St(i,o.strokeWidth,o.lineEndings);return{type:e.PdfAnnotationSubtype.POLYLINE,bounds:s,data:{...o,rect:s,vertices:i,currentVertex:n}}};return{onClick:(t,e)=>{if(e.metaKey||e.ctrlKey)return;const n=u(t),o=r(),s=o[o.length-1];s&&Math.abs(s.x-n.x)<1&&Math.abs(s.y-n.y)<1||(c([...o,n]),l(n),i(g()))},onDoubleClick:()=>{(()=>{const t=r();if(t.length<2)return;const s=h();if(!s)return;const a=St(t,s.strokeWidth,s.lineEndings),d={...s,vertices:t,rect:a,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(g())}},onPointerCancel:()=>{c([]),l(null),i(null)}}}};function Ht(t,e,n,o){const i=o*Math.PI/180,s=Math.abs(Math.cos(i)),a=Math.abs(Math.sin(i)),r=s*s-a*a,c=t.size.width,d=t.size.height;let l,u;if(Math.abs(r)>1e-6)l=(s*c-a*d)/r,u=(s*d-a*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,Yt=Math.PI/2,Vt=34*Math.PI/180,Xt=30*Math.PI/180,Kt=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,s){const a=-e,r=-o,c=-s;this.updateBBox(t,a),this.updateBBox(n,r),this.updateBBox(i,c),this.parts.push(`C ${Qt(t)} ${Qt(a)}, ${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,s,a,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&&a.moveTo(n+i*c,o+s*d));const g=4/3*(1-Math.cos((e-t)/2))/h,p=n+i*(c-g*d),f=o+s*(d+g*c),y=n+i*(l+g*u),m=o+s*(u-g*l),A=n+i*l,x=o+s*u;r&&a.moveTo(n+i*c,o+s*d),a.curveTo(p,f,y,m,A,x)}function ne(t,e,n,o,i,s){const a=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*(a-u*r),y:o+s*(r+u*a)},{x:n+i*(c+u*d),y:o+s*(d-u*c)},{x:n+i*c,y:o+s*d}]}function oe(t,e,n,o,i,s,a,r){const c=Yt;let d=e-t;for(;d<0;)d+=2*Math.PI;const l=d;let u=0;for(r&&a.moveTo(i+n*Math.cos(t),s+o*Math.sin(t));d>c;)ee(t+u,t+u+c,i,s,n,o,a,!1),u+=c,d-=c;d>0&&ee(t+u,t+l,i,s,n,o,a,!1)}function ie(t,e,n,o,i,s,a,r,c){let d=t+_t+a;const l=t+_t+a-22*Math.PI/180;ee(d,l,o,i,n,n,r,c),d=l;oe(d,e-s,n,n,o,i,r,!1)}function se(t,e,n,o,i,s){const a=t+_t;ee(a+n,a+n-Xt,o,i,e,e,s,!1),ee(a+n-Xt,a+Yt,o,i,e,e,s,!1),ee(a+Yt,a+_t-Vt,o,i,e,e,s,!1)}function ae(t,e){const n=[],o=t+_t;return n.push(...ne(o+Vt,o+Kt,0,0,e,e)),n.push(...ne(o+Kt,o+Yt,0,0,e,e)),n.push(...ne(o+Yt,o+_t-Vt,0,0,e,e)),n}function re(t,e,n,o){for(let i=0;i+2<t.length;i+=3){const s=t[i],a=t[i+1],r=t[i+2];o.curveTo(s.x+e,s.y+n,a.x+e,a.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,s=Math.max(1,Math.ceil(o/i));return{n:s,adjustedRadius:o/(2*s*e)}}function de(t,e,n,o,i){let s=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 a;(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})(a=s)<0&&a.reverse();const r=s.length;if(r<2)return;if(n<=0){i.moveTo(s[0].x,s[0].y);for(let t=1;t<r;t++)i.curveTo(s[t].x,s[t].y,s[t].x,s[t].y,s[t].x,s[t].y);return}let c=e?le(n,o):ue(n,o);c<.5&&(c=.5);const d=Math.cos(Vt),l=[];for(let g=0;g+1<r;g++){const t=Zt(s[g],s[g+1]);t<=0||t>=2*d*c?l.push(Vt):l.push(Math.acos(Math.min(1,t/(2*c))))}let u=0,h=!1;for(let g=0;g+1<r;g++){const t=s[g],e=s[g+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 a=Math.max(.5,o.adjustedRadius),p=2*d*a,f=d*c+d*a;let y=Math.atan2(e.y-t.y,e.x-t.x);if(0===g){const e=s[r-2];u=Math.atan2(t.y-e.y,t.x-e.x)}const m=Jt(e.x-t.x,n),A=te(e.y-t.y,n);let x=t.x,b=t.y;const I=l[g],v=l[0===g?r-2:g-1]??Vt;if(ie(u,y,c,t.x,t.y,I,v,i,!h),h=!0,0===o.n)x+=n*m,b+=n*A;else{x+=f*m,b+=f*A;let t=o.n;o.n>=1&&(se(y,a,Vt,x,b,i),x+=p*m,b+=p*A,t=o.n-1);const e=ae(y,a);for(let n=0;n<t;n++)re(e,x,b,i),x+=p*m,b+=p*A}u=y}}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,s,a){if(i<=0){const i=Math.abs(n-t)/2,s=Math.abs(o-e)/2,r=(t+n)/2,c=(e+o)/2;return void oe(0,2*Math.PI,i,s,r,c,a,!0)}const r=n-t,c=o-e;let d=le(i,s);const l=.5*d;if(r<l&&c<l){const i=Math.abs(n-t)/2,s=Math.abs(o-e)/2,r=(t+n)/2,c=(e+o)/2;return void oe(0,2*Math.PI,i,s,r,c,a,!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,s,a);const u=Math.sin(Kt)*d-1.5;let h=t,g=n,p=e,f=o;if(r>2*u)h+=u,g-=u;else{const e=(t+n)/2;h=e-.1,g=e+.1}if(c>2*u)p+=u,f-=u;else{const t=(o+e)/2;f=t+.1,p=t-.1}const y=function(t,e,n,o){const i=(t+n)/2,s=(e+o)/2,a=(n-t)/2,r=(o-e)/2;if(a<=0||r<=0)return[];const c=Math.max(32,Math.ceil(2*Math.max(a,r))),d=[];for(let l=0;l<=c;l++){const t=2*Math.PI*l/c;d.push({x:i+a*Math.cos(t),y:s+r*Math.sin(t)})}return d}(h,p,g,f),m=y.length;if(m<2)return;let A=0;for(let C=1;C<m;C++)A+=Zt(y[C-1],y[C]);const x=Math.cos(Vt);let b=2*x*d,I=Math.ceil(A/b);if(I<2){const i=Math.abs(n-t)/2,s=Math.abs(o-e)/2,r=(t+n)/2,c=(e+o)/2;return void oe(0,2*Math.PI,i,s,r,c,a,!0)}if(b=A/I,d=b/(2*x),d<.5)d=.5,b=2*x*d;else if(d<3){const i=Math.abs(n-t)/2,s=Math.abs(o-e)/2,r=(t+n)/2,c=(e+o)/2;return void oe(0,2*Math.PI,i,s,r,c,a,!0)}const v=[];let P=0;const S=.1*s;for(let C=0;C+1<m;C++){const t=y[C],e=y[C+1],n=e.x-t.x,o=e.y-t.y,i=Zt(t,e);if(0===i)continue;let s=i+P;if(s>=b-S||C===m-2){const e=Jt(n,i),a=te(o,i);let r=b-P;for(;s>=b-S;)v.push({x:t.x+r*e,y:t.y+r*a}),s-=b,r+=b;P=Math.max(0,s)}else P+=i}const R=v.length;let T=0,M=0;for(let C=0;C<R;C++){const t=(C+1)%R,e=v[C],n=v[t];if(0===C){const t=v[R-1];T=Math.atan2(e.y-t.y,e.x-t.x),M=ge(t,e,d,b)}const o=Math.atan2(n.y-e.y,n.x-e.x),i=ge(e,n,d,b);ie(T,o,d,e.x,e.y,i,M,a,0===C),T=o,M=i}}function ge(t,e,n,o){const i=Zt(t,e);if(0===i)return Vt;const s=(o/2+(i-o)/2)/n;return s<-1||s>1?0:Math.acos(s)}function pe(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:s,scale:a,pageSize:r}=n,[c,d]=mt([]),[l,u]=mt(null),h=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.height)}),g=()=>{const t=s();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},p=()=>{const t=c();if(t.length<3)return;const s=g();if(!s)return;const a=s.cloudyBorderIntensity??0,r=a>0?pe(a,s.strokeWidth,!1):s.strokeWidth/2,l=e.expandRect(e.rectFromPoints(t),r),h={...s,vertices:t,rect:l,type:e.PdfAnnotationSubtype.POLYGON,pageIndex:n.pageIndex,id:e.uuidV4(),created:new Date,...a>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=g();if(!o)return null;const i=o.cloudyBorderIntensity??0,s=i>0?pe(i,o.strokeWidth,!1):o.strokeWidth/2,a=[...t,n],r=e.expandRect(e.rectFromPoints(a),s);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/a/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 p();const o=c(),s=o[o.length-1];s&&Math.abs(s.x-n.x)<1&&Math.abs(s.y-n.y)<1||(d([...o,n]),u(n),i(f()))},onDoubleClick:t=>{p()},onPointerMove:t=>{if(c().length>0){const e=h(t);u(e),i(f())}},onPointerCancel:t=>{d([]),u(null),i(null)}}}},ye={annotationType:e.PdfAnnotationSubtype.SQUARE,create(n){const{pageIndex:o,onCommit:i,onPreview:s,getTool:a,pageSize:r}=n,[c,d]=mt(null),l=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.height)}),u=()=>{const t=a();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=Ft({threshold:5,getTool:a,onClickDetected:(n,s)=>{const a=u();if(!a)return;const c=s.clickBehavior;if(!(null==c?void 0:c.enabled))return;const{width:d,height:l}=c.defaultSize,h=d/2,g=l/2,p=t.clamp(n.x-h,0,r.width-d),f=t.clamp(n.y-g,0,r.height-l),y=a.strokeWidth,m=a.cloudyBorderIntensity??0,A=m>0?pe(m,y,!1):y/2,x={origin:{x:p-A,y:f-A},size:{width:d+2*A,height:l+2*A}},b={...a,type:e.PdfAnnotationSubtype.SQUARE,created:new Date,id:e.uuidV4(),pageIndex:o,rect:x,...m>0&&{rectangleDifferences:{left:A,top:A,right:A,bottom:A}}};i(b)}}),g=t=>{const n=c();if(!n)return null;const o=Math.min(n.x,t.x),i=Math.min(n.y,t.y),s=Math.abs(n.x-t.x),a=Math.abs(n.y-t.y),r=u();if(!r)return null;const d=r.strokeWidth,l=r.cloudyBorderIntensity??0,h=l>0?pe(l,d,!1):d/2,g={origin:{x:o-h,y:i-h},size:{width:s+2*h,height:a+2*h}};return{type:e.PdfAnnotationSubtype.SQUARE,bounds:g,data:{rect:g,...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),s(g(o)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=l(t);h.onMove(e),c()&&h.hasMoved()&&s(g(e))},onPointerUp:(t,n)=>{var a;if(!c())return;if(!u())return;const r=l(t);if(h.hasMoved()){const t=u();if(!t)return;const n=g(r);if(n){const s=t.cloudyBorderIntensity??0,a=s>0?pe(s,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!==a&&{rectangleDifferences:{left:a,top:a,right:a,bottom:a}}};i(r)}}else h.onEnd(r);d(null),s(null),h.reset(),null==(a=n.releasePointerCapture)||a.call(n)},onPointerLeave:(t,e)=>{var n;d(null),s(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;d(null),s(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},me={annotationType:e.PdfAnnotationSubtype.STAMP,create(t){const{services:n,onCommit:o,getTool:i,pageSize:s,pageRotation:a}=t;return{onPointerDown:r=>{const c=i();if(!c)return;const{imageSrc:d,imageSize:l}=c.defaults,u=(n,i,d)=>{var l;const u={origin:{x:r.x-i/2,y:r.y-d/2},size:{width:i,height:d}};let h={...c.defaults,rect:u,type:e.PdfAnnotationSubtype.STAMP,icon:c.defaults.icon??e.PdfAnnotationIcon.Draft,subject:c.defaults.subject??"Stamp",flags:c.defaults.flags??["print"],pageIndex:t.pageIndex,id:e.uuidV4(),created:new Date};(null==(l=c.behavior)?void 0:l.insertUpright)&&(h=Ot(h,a,!1)),h=Nt(h,s),o(h,{imageData:n})};d?n.processImage({source:d,maxWidth:s.width,maxHeight:s.height,onComplete:t=>u(t.imageData,(null==l?void 0:l.width)??t.width,(null==l?void 0:l.height)??t.height)}):n.requestFile({accept:"image/png,image/jpeg",onFile:t=>{n.processImage({source:t,maxWidth:s.width,maxHeight:s.height,onComplete:t=>u(t.imageData,t.width,t.height)})}})}}}},Ae={annotationType:e.PdfAnnotationSubtype.CIRCLE,create(n){const{pageIndex:o,onCommit:i,onPreview:s,getTool:a,pageSize:r}=n,[c,d]=mt(null),l=e=>({x:t.clamp(e.x,0,r.width),y:t.clamp(e.y,0,r.height)}),u=()=>{const t=a();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=Ft({threshold:5,getTool:a,onClickDetected:(n,s)=>{const a=u();if(!a)return;const c=s.clickBehavior;if(!(null==c?void 0:c.enabled))return;const{width:d,height:l}=c.defaultSize,h=d/2,g=l/2,p=t.clamp(n.x-h,0,r.width-d),f=t.clamp(n.y-g,0,r.height-l),y=a.strokeWidth,m=a.cloudyBorderIntensity??0,A=m>0?pe(m,y,!0):y/2,x={origin:{x:p-A,y:f-A},size:{width:d+2*A,height:l+2*A}},b={...a,type:e.PdfAnnotationSubtype.CIRCLE,created:new Date,id:e.uuidV4(),pageIndex:o,rect:x,...m>0&&{rectangleDifferences:{left:A,top:A,right:A,bottom:A}}};i(b)}}),g=t=>{const n=c();if(!n)return null;const o=Math.min(n.x,t.x),i=Math.min(n.y,t.y),s=Math.abs(n.x-t.x),a=Math.abs(n.y-t.y),r=u();if(!r)return null;const d=r.strokeWidth,l=r.cloudyBorderIntensity??0,h=l>0?pe(l,d,!0):d/2,g={origin:{x:o-h,y:i-h},size:{width:s+2*h,height:a+2*h}};return{type:e.PdfAnnotationSubtype.CIRCLE,bounds:g,data:{rect:g,...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),s(g(o)),null==(n=e.setPointerCapture)||n.call(e)},onPointerMove:t=>{const e=l(t);h.onMove(e),c()&&h.hasMoved()&&s(g(e))},onPointerUp:(t,n)=>{var a;if(!c())return;if(!u())return;const r=l(t);if(h.hasMoved()){const t=u();if(!t)return;const n=g(r);if(n){const s=t.cloudyBorderIntensity??0,a=s>0?pe(s,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!==a&&{rectangleDifferences:{left:a,top:a,right:a,bottom:a}}};i(r)}}else h.onEnd(r);d(null),s(null),h.reset(),null==(a=n.releasePointerCapture)||a.call(n)},onPointerLeave:(t,e)=>{var n;d(null),s(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)},onPointerCancel:(t,e)=>{var n;d(null),s(null),h.reset(),null==(n=e.releasePointerCapture)||n.call(e)}}}},xe={toolId:"__textMarkup__",handle(t,n,o){const i=t.getTool();if(i)for(const s of n){const n=e.uuidV4();o().then(e=>{var o;t.createAnnotation(s.pageIndex,{...i.defaults,rect:s.rect,segmentRects:s.segmentRects,pageIndex:s.pageIndex,created:new Date,id:n,...null!=e&&{custom:{text:e}}}),(null==(o=i.behavior)?void 0:o.selectAfterCreate)&&t.selectAnnotation(s.pageIndex,n)})}}};function be(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 Ie={toolId:"insertText",handle(t,n,o){const i=t.getTool();if(!i)return;const s=()=>({strokeColor:i.defaults.strokeColor??"#E44234",opacity:i.defaults.opacity??1,flags:i.defaults.flags??["print"]});for(const a of n){const n=a.segmentRects[a.segmentRects.length-1];if(!n)continue;const r=be(n),c=e.uuidV4(),d=s();o().then(n=>{var o;t.createAnnotation(a.pageIndex,{type:e.PdfAnnotationSubtype.CARET,id:c,pageIndex:a.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(a.pageIndex,c)})}}},ve={toolId:"replaceText",handle(t,n,o){const i=t.getTool();if(!i)return;const s=()=>({strokeColor:i.defaults.strokeColor??"#E44234",opacity:i.defaults.opacity??1,flags:i.defaults.flags??["print"]});for(const a of n){const n=a.segmentRects[a.segmentRects.length-1];if(!n)continue;const r=be(n),c=e.uuidV4(),d=e.uuidV4(),l=s();o().then(n=>{var o;t.createAnnotation(a.pageIndex,{type:e.PdfAnnotationSubtype.CARET,id:c,pageIndex:a.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(a.pageIndex,{type:e.PdfAnnotationSubtype.STRIKEOUT,id:d,pageIndex:a.pageIndex,rect:a.rect,segmentRects:a.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(a.pageIndex,c)})}}},Pe=(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:s,rects:a}=zt(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),g=Math.max(1,Math.min(s.size.width,s.size.height)),p=Math.min(h,g),f=Number(p.toFixed(1)),y=r(i,t.strokeWidth/2),m=r(s,f/2),A=m.size.width/Math.max(y.size.width,1e-6),x=m.size.height/Math.max(y.size.height,1e-6);return{...a,inkList:t.inkList.map(t=>({points:t.points.map(t=>({x:m.origin.x+(t.x-y.origin.x)*A,y:m.origin.y+(t.y-y.origin.y)*x}))})),strokeWidth:f}}case"rotate":{const e=Et(t,n);if(!e)return n.changes;const{dx:o,dy:i}=Lt(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),s=e.expandRect(e.rectFromPoints(i),o.strokeWidth/2),a=n.changes.rotation??t.rotation??0;return t.unrotatedRect||void 0!==n.changes.rotation?{...n.changes,unrotatedRect:s,rect:e.calculateRotatedRectAABBAroundPoint(s,a,Tt(t))}:{...n.changes,rect:s}}}},Se=(t,n)=>{switch(n.type){case"vertex-edit":if(n.changes.linePoints){const{start:e,end:o}=n.changes.linePoints,i=[e,o],s=Ct(t,i,St(i,t.strokeWidth,t.lineEndings));return{...Rt(t,St(s,t.strokeWidth,t.lineEndings)),linePoints:{start:s[0],end:s[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:s,rects:a}=zt(t,n.changes.rect,n.metadata);return{...a,linePoints:{start:{x:s.origin.x+(t.linePoints.start.x-i.origin.x)*e,y:s.origin.y+(t.linePoints.start.y-i.origin.y)*o},end:{x:s.origin.x+(t.linePoints.end.x-i.origin.x)*e,y:s.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}=Lt(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),s=n.changes.rotation??t.rotation??0;return t.unrotatedRect||void 0!==n.changes.rotation?{...n.changes,unrotatedRect:i,rect:e.calculateRotatedRectAABBAroundPoint(i,s,Tt(t))}:{...n.changes,rect:i}}default:return n.changes}},Re=(t,n)=>{switch(n.type){case"vertex-edit":if(n.changes.vertices&&n.changes.vertices.length){const e=n.changes.vertices,o=Ct(t,e,St(e,t.strokeWidth,t.lineEndings));return{...Rt(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:s,rects:a}=zt(t,n.changes.rect,n.metadata);return{...a,vertices:t.vertices.map(t=>({x:s.origin.x+(t.x-i.origin.x)*e,y:s.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}=Lt(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),s=n.changes.rotation??t.rotation??0;return t.unrotatedRect||void 0!==n.changes.rotation?{...n.changes,unrotatedRect:i,rect:e.calculateRotatedRectAABBAroundPoint(i,s,Tt(t))}:{...n.changes,rect:i}}default:return n.changes}};function Te(t,e){return(t??0)>0?pe(t,e,!1):e/2}const Me=(t,n)=>{switch(n.type){case"vertex-edit":if(n.changes.vertices&&n.changes.vertices.length){const o=Te(t.cloudyBorderIntensity,t.strokeWidth),i=n.changes.vertices,s=Ct(t,i,e.expandRect(e.rectFromPoints(i),o));return{...Rt(t,e.expandRect(e.rectFromPoints(s),o)),vertices:s}}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:s,rects:a}=zt(t,n.changes.rect,n.metadata);return{...a,vertices:t.vertices.map(t=>({x:s.origin.x+(t.x-i.origin.x)*e,y:s.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}=Lt(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},s=Te(i.cloudyBorderIntensity,i.strokeWidth),a=e.expandRect(e.rectFromPoints(i.vertices),s);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=pe(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:a,rect:e.calculateRotatedRectAABBAroundPoint(a,d,Tt(t))}:{...r,rect:a}}default:return n.changes}},Ce=(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?zt(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,s=(t.cloudyBorderIntensity??0)>0;if(o||i&&s){const o={...t,...e.changes},i=o.cloudyBorderIntensity??0;if(i>0){const t=pe(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,...wt(t,e.changes.rotation)}),n}default:return e.changes}},ke=(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?zt(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,s=(t.cloudyBorderIntensity??0)>0;if(o||i&&s){const o={...t,...e.changes},i=o.cloudyBorderIntensity??0;if(i>0){const t=pe(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,...wt(t,e.changes.rotation)}),n}default:return e.changes}},De=(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?zt(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,...wt(t,e.changes.rotation)}:e.changes;default:return e.changes}},Ee=(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?zt(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,...wt(t,e.changes.rotation)}:e.changes;default:return e.changes}},we=class extends t.BasePlugin{constructor(e,n,o){var i,s,a;super(e,n),this.ANNOTATION_HISTORY_TOPIC="annotations",this.state$=t.createBehaviorEmitter(),this.pendingContexts=new Map,this.isInitialLoadComplete=new Map,this.importQueue=new Map,this.commitInProgress=new Map,this.handlerFactories=new Map,this.selectionHandlerFactories=new Map,this.activeTool$=t.createBehaviorEmitter(),this.events$=t.createBehaviorEmitter(),this.toolsChange$=t.createBehaviorEmitter(),this.patchRegistry=new kt,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.config=o,this.selection=(null==(i=n.getPlugin("selection"))?void 0:i.provides())??null,this.history=(null==(s=n.getPlugin("history"))?void 0:s.provides())??null,this.interactionManager=(null==(a=n.getPlugin("interaction-manager"))?void 0:a.provides())??null,this.registerHandlerFactories(),this.registerSelectionHandlerFactories(),this.registerBuiltInPatches()}onDocumentLoadingStarted(t){this.dispatch(function(t,e){return{type:i,payload:{documentId:t,state:e}}}(t,{pages:{},byUid:{},selectedUids:[],selectedUid:null,activeToolId:null,hasPendingChanges:!1})),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:s,payload:t}}(t)),this.pendingContexts.delete(t),this.isInitialLoadComplete.delete(t),this.importQueue.delete(t),this.appearanceCache.delete(t),this.logger.debug("AnnotationPlugin","DocumentClosed",`Cleaned up annotation state for document: ${t}`)}registerHandlerFactories(){this.handlerFactories.set(e.PdfAnnotationSubtype.CIRCLE,Ae),this.handlerFactories.set(e.PdfAnnotationSubtype.SQUARE,ye),this.handlerFactories.set(e.PdfAnnotationSubtype.STAMP,me),this.handlerFactories.set(e.PdfAnnotationSubtype.POLYGON,fe),this.handlerFactories.set(e.PdfAnnotationSubtype.POLYLINE,Wt),this.handlerFactories.set(e.PdfAnnotationSubtype.LINE,Gt),this.handlerFactories.set(e.PdfAnnotationSubtype.INK,At),this.handlerFactories.set(e.PdfAnnotationSubtype.FREETEXT,$t),this.handlerFactories.set(e.PdfAnnotationSubtype.TEXT,Bt)}registerSelectionHandlerFactories(){this.selectionHandlerFactories.set("insertText",Ie),this.selectionHandlerFactories.set("replaceText",ve)}registerBuiltInPatches(){this.patchRegistry.register(e.PdfAnnotationSubtype.INK,Pe),this.patchRegistry.register(e.PdfAnnotationSubtype.LINE,Se),this.patchRegistry.register(e.PdfAnnotationSubtype.POLYLINE,Re),this.patchRegistry.register(e.PdfAnnotationSubtype.POLYGON,Me),this.patchRegistry.register(e.PdfAnnotationSubtype.CIRCLE,Ce),this.patchRegistry.register(e.PdfAnnotationSubtype.SQUARE,ke),this.patchRegistry.register(e.PdfAnnotationSubtype.FREETEXT,De),this.patchRegistry.register(e.PdfAnnotationSubtype.STAMP,Ee)}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(T(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 s=(i.pages[e]??[]).map(t=>i.byUid[t]).filter(t=>void 0!==t).filter(t=>K(t)).filter(t=>w(n,t.object.rect)).map(t=>t.object.id);if(s.length>0){const e=new Set;for(const n of s)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,s;if(!this.checkPermission(t,e.PdfPermissionFlag.ModifyAnnotations))return;const a=this.getActiveTool(t);if(!a||!a.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:a.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)};(this.selectionHandlerFactories.get(a.id)??xe).handle(d,r,()=>new Promise(t=>{c.wait(e=>t(e.join("\n")),()=>t(void 0))})),(null==(i=a.behavior)?void 0:i.deactivateToolAfterCreate)&&this.setActiveTool(null,t),null==(s=this.selection)||s.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=>this.setActiveTool(t),getState:()=>this.getDocumentState(),getPageAnnotations:t=>this.getPageAnnotations(t),getSelectedAnnotation:()=>this.getSelectedAnnotation(),getSelectedAnnotations:()=>this.getSelectedAnnotationsMethod(),getSelectedAnnotationIds:()=>this.getSelectedAnnotationIdsMethod(),getAnnotationById:t=>this.getAnnotationById(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),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),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),forDocument:t=>this.createAnnotationScope(t),getTools:()=>this.state.tools,getTool:t=>this.getTool(t),addTool:t=>{this.dispatch((t=>({type:I,payload:t}))(t)),this.registerInteractionForTool(t)},findToolForAnnotation:t=>this.findToolForAnnotation(t),setToolDefaults:(t,e)=>this.dispatch(((t,e)=>({type:b,payload:{toolId:t,patch:e}}))(t,e)),getColorPresets:()=>[...this.state.colorPresets],addColorPreset:t=>this.dispatch({type:x,payload:t}),transformAnnotation:(t,e)=>this.transformAnnotation(t,e),registerPatchFunction:(t,e)=>this.registerPatchFunction(t,e),onStateChange:this.state$.on,onActiveToolChange:this.activeTool$.on,onAnnotationEvent:this.events$.on,onToolsChange:this.toolsChange$.on}}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),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=>this.setActiveTool(e,t),findToolForAnnotation:t=>this.findToolForAnnotation(t),importAnnotations:e=>this.importAnnotations(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),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),onStateChange:e=>this.state$.on(n=>{n.documentId===t&&e(n.state)}),onAnnotationEvent:e=>this.events$.on(n=>{n.documentId===t&&e(n)}),onActiveToolChange:e=>this.activeTool$.on(n=>{n.documentId===t&&e(n.tool)})}}onStoreUpdated(t,e){for(const n in e.documents){const o=t.documents[n],i=e.documents[n];o!==i&&(this.state$.emit({documentId:n,state:i}),o&&o.activeToolId!==i.activeToolId&&this.activeTool$.emit({documentId:n,tool: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({documentId:t,tool:this.getActiveTool(t)});this.toolsChange$.emit({tools:e.tools})}}registerPatchFunction(t,e){this.patchRegistry.register(t,e)}transformAnnotation(t,e){const n={type:e.type,changes:e.changes,metadata:e.metadata};return this.patchRegistry.transform(t,n)}registerPageHandlers(t,e,n,o){var i;const s=this.getCoreDocument(t),a=null==(i=null==s?void 0:s.document)?void 0:i.pages[e];if(!a)return()=>{};if(!this.interactionManager)return()=>{};const r=[],c=((a.rotation??0)+((null==s?void 0:s.rotation)??0))%4;for(const d of this.state.tools){if(!d.defaults.type)continue;const i=this.handlerFactories.get(d.defaults.type);if(!i)continue;const s={pageIndex:e,pageSize:a.size,pageRotation:c,scale:n,services:o.services,onPreview:t=>o.onPreview(d.id,t),onCommit:(n,o)=>{var i,s;this.createAnnotation(e,n,o,t),(null==(i=d.behavior)?void 0:i.deactivateToolAfterCreate)&&this.setActiveTool(null,t),(null==(s=d.behavior)?void 0:s.selectAfterCreate)&&this.selectAnnotation(e,n.id,t)},getTool:()=>this.state.tools.find(t=>t.id===d.id)},l=this.interactionManager.registerHandlers({documentId:t,modeId:d.interaction.mode??d.id,handlers:i.create(s),pageIndex:e});r.push(l)}return()=>r.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:a,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({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(),s=this.getCoreDocument(i),a=null==s?void 0:s.document;if(!a)return e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Document not found"});const r=a.pages.find(t=>t.index===o);return r?this.engine.getPageAnnotations(a,r):e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Page not found"})}getSelectedAnnotation(t){return Q(this.getDocumentState(t))}getAnnotationById(t,e){const n=this.getDocumentState(e);return tt(n,t)}renderAnnotation({pageIndex:t,annotation:n,options:o},i){const s=i??this.getActiveDocumentId(),a=this.getCoreDocument(s),r=null==a?void 0:a.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(),s=this.getCoreDocument(i),a=null==s?void 0:s.document;if(!a)return e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Document not found"});const r=a.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(a,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 s=i.get(e);s&&delete s[t]}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,s=e.pageIndex,a=e.id;this.dispatch(M(t,s,e)),i&&n.set(a,i)}!1!==this.config.autoCommit&&this.commit(t)}}createAnnotation(t,n,o,i){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 a=n.id,r=this.pendingContexts.get(s);if(!r)return;const c={...n,author:n.author??this.config.annotationAuthor},d=()=>{this.dispatch(M(s,t,c)),o&&r.set(a,o),this.events$.emit({type:"create",documentId:s,annotation:c,pageIndex:t,ctx:o,committed:!1})};if(!this.history)return d(),void(this.config.autoCommit&&this.commit(s));const l={execute:d,undo:()=>{r.delete(a),this.dispatch(v(s)),this.dispatch(D(s,t,a)),this.events$.emit({type:"delete",documentId:s,annotation:c,pageIndex:t,committed:!1})},metadata:{annotationIds:[a]}};this.history.forDocument(s).register(l,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 s=i??this.getActiveDocumentId();if(!this.checkPermission(s,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","UpdateAnnotation",`Cannot update annotation: document ${s} lacks ModifyAnnotations permission`);const a=this.getDocumentState(s).byUid[n].object,r=this.buildPatch(a,{...o,author:o.author??this.config.annotationAuthor}),c=()=>{this.dispatch(C(s,t,n,r)),this.events$.emit({type:"update",documentId:s,annotation:a,pageIndex:t,patch:r,committed:!1})};if(!this.history)return c(),void(!1!==this.config.autoCommit&&this.commit(s));const d=Object.fromEntries(Object.keys(o).map(t=>[t,a[t]])),l={execute:c,undo:()=>{this.dispatch(C(s,t,n,d)),this.events$.emit({type:"update",documentId:s,annotation:a,pageIndex:t,patch:d,committed:!1})},metadata:{annotationIds:[n]}};this.history.forDocument(s).register(l,this.ANNOTATION_HISTORY_TOPIC)}deleteAnnotation(t,n,o){var i;const s=o??this.getActiveDocumentId();if(!this.checkPermission(s,e.PdfPermissionFlag.ModifyAnnotations))return void this.logger.debug("AnnotationPlugin","DeleteAnnotation",`Cannot delete annotation: document ${s} lacks ModifyAnnotations permission`);const a=this.getDocumentState(s),r=null==(i=a.byUid[n])?void 0:i.object;if(!r)return;const c=nt(a,n),d=c.map(t=>{var e;return null==(e=a.byUid[t.id])?void 0:e.object}).filter(t=>void 0!==t),l=()=>{var e;for(const t of c){const n=null==(e=a.byUid[t.id])?void 0:e.object;n&&(this.dispatch(D(s,t.pageIndex,t.id)),this.events$.emit({type:"delete",documentId:s,annotation:n,pageIndex:t.pageIndex,committed:!1}))}this.dispatch(v(s)),this.dispatch(D(s,t,n)),this.events$.emit({type:"delete",documentId:s,annotation:r,pageIndex:t,committed:!1})};if(!this.history)return l(),void(!1!==this.config.autoCommit&&this.commit(s));const u={execute:l,undo:()=>{this.dispatch(M(s,t,r)),this.events$.emit({type:"create",documentId:s,annotation:r,pageIndex:t,committed:!1});for(const t of d)this.dispatch(M(s,t.pageIndex,t)),this.events$.emit({type:"create",documentId:s,annotation:t,pageIndex:t.pageIndex,committed:!1})},metadata:{annotationIds:[n,...c.map(t=>t.id)]}};this.history.forDocument(s).register(u,this.ANNOTATION_HISTORY_TOPIC)}deleteAnnotationsMethod(t,e){for(const{pageIndex:n,id:o}of t)this.deleteAnnotation(n,o,e)}purgeAnnotationMethod(t,e,n){const o=n??this.getActiveDocumentId();this.dispatch(E(o,t,e))}selectAnnotation(t,e,n){const o=n??this.getActiveDocumentId();if(this.isInGroupMethod(e,o)){const t=this.getGroupMembersMethod(e,o).map(t=>t.object.id);this.dispatch(R(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(v(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],s=e.byUid[i];s&&(null==(o=this.interactionManager)||o.claimPageActivity(t,"annotation-selection",s.object.pageIndex))}getSelectedAnnotationsMethod(t){return Z(this.getDocumentState(t))}getSelectedAnnotationIdsMethod(t){return J(this.getDocumentState(t))}toggleSelectionMethod(t,e,n){const o=n??this.getActiveDocumentId(),i=this.getDocumentState(o);if(i.selectedUids.includes(e))this.dispatch(S(o,e));else if(this.isInGroupMethod(e,o)){const t=this.getGroupMembersMethod(e,o);for(const e of t)i.selectedUids.includes(e.object.id)||this.dispatch(P(o,e.object.pageIndex,e.object.id))}else this.dispatch(P(o,t,e))}addToSelectionMethod(t,e,n){const o=n??this.getActiveDocumentId();this.dispatch(P(o,t,e))}removeFromSelectionMethod(t,e){const n=e??this.getActiveDocumentId();this.dispatch(S(n,t))}setSelectionMethod(t,e){const n=e??this.getActiveDocumentId();this.dispatch(R(n,t))}getAttachedLinksMethod(t,e){return it(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],s=o.slice(1).map(t=>({pageIndex:t.object.pageIndex,id:t.object.id,patch:{inReplyToId:i.object.id,replyType:e.PdfAnnotationReplyType.Group}}));this.updateAnnotationsMethod(s,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 at(this.getDocumentState(e),t)}isInGroupMethod(t,e){return rt(this.getDocumentState(e),t)}getGroupingActionMethod(t){return ct(this.getDocumentState(t))}computeCombinedConstraints(t){let e=1/0,n=1/0,o=1/0,i=1/0;for(const s of t){const t=s.rect.origin.y,a=s.pageSize.height-(s.rect.origin.y+s.rect.size.height),r=s.rect.origin.x,c=s.pageSize.width-(s.rect.origin.x+s.rect.size.width);e=Math.min(e,t),n=Math.min(n,a),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 s=[...n,...i],a=new Map,r=[];for(const d of s){const e=this.getAnnotationById(d,t);e&&(a.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:s,originalRects:a,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 s=t.originalRects.get(o);if(!s)continue;const a={...s,origin:{x:s.origin.x+t.delta.x,y:s.origin.y+t.delta.y}};n[o]=this.transformAnnotation(i.object,{type:"move",changes:{rect:a}})}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 s=this.computeDragPreviewPatches(i,t);return this.unifiedDrag$.emit({documentId:t,type:"update",state:i,previewPatches:s}),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 s=this.getAnnotationById(i,t);if(!s)continue;const a=e.originalRects.get(i)??s.object.rect,r={...a,origin:{x:a.origin.x+n.x,y:a.origin.y+n.y}},c=this.transformAnnotation(s.object,{type:"move",changes:{rect:r}});o.push({pageIndex:s.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 s of t)e=Math.min(e,s.origin.x),n=Math.min(n,s.origin.y),o=Math.max(o,s.origin.x+s.size.width),i=Math.max(i,s.origin.y+s.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),s=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:s}})}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[s,a]of t){const t=this.getAnnotationById(s,e);if(!t)continue;let r=a;const c=null==i?void 0:i.get(s);(null==o?void 0:o.isGroupResize)&&(null==c?void 0:c.originalUnrotatedRect)&&(r=Ht(a,c.originalRect,c.originalUnrotatedRect,t.object.rotation??0)),n[s]=this.transformAnnotation(t.object,{type:"resize",changes:{rect:r}})}return n}startResize(t,e){const{annotationIds:n,pageSize:o,resizeHandle:i}=e,s=[],a=[];for(const p of n){const e=this.getAnnotationById(p,t);if(e){a.push({id:p,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(p,t);for(const t of n)s.includes(t.object.id)||(s.push(t.object.id),a.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:p}))}}const r=[...n,...s],c=a.map(t=>t.rect),d=this.computeUnifiedGroupBoundingBox(c),l=this.computeUnifiedRelativePositions(a,d),u=this.computeUnifiedResizedRects(l,d),h={documentId:t,isResizing:!0,isGroupResize:n.length>1,primaryIds:n,attachedLinkIds:s,allParticipantIds:r,originalGroupBox:d,currentGroupBox:d,participatingAnnotations:l,resizeHandle:i,computedRects:u};this.unifiedResizeStates.set(t,h);const g=this.computeResizePreviewPatches(u,t);this.unifiedResize$.emit({documentId:t,type:"start",state:h,computedRects:Object.fromEntries(u),previewPatches:g})}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 s=Object.fromEntries(o),a=this.computeResizePreviewPatches(o,t);return this.unifiedResize$.emit({documentId:t,type:"update",state:i,computedRects:s,previewPatches:a}),s}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[a,r]of n){const n=this.getAnnotationById(a,t);if(!n)continue;let s=r;const c=i.get(a);e.isGroupResize&&(null==c?void 0:c.originalUnrotatedRect)&&(s=Ht(r,c.originalRect,c.originalUnrotatedRect,n.object.rotation??0));const d=this.transformAnnotation(n.object,{type:"resize",changes:{rect:s}});o.push({pageIndex:n.object.pageIndex,id:a,patch:d})}o.length>0&&this.updateAnnotationsMethod(o,t);const s=this.computeResizePreviewPatches(n,t);this.unifiedResize$.emit({documentId:t,type:"end",state:{...e,isResizing:!1},computedRects:Object.fromEntries(n),previewPatches:s}),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 s=this.getAttachedLinksMethod(i,e);for(const e of s)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 s=this.getAnnotationById(i.id,n);if(!s)continue;const a=Tt({rect:i.rect,unrotatedRect:i.unrotatedRect,rotation:i.rotation}),r=e.rotatePointAround(a,t.rotationCenter,t.delta),c={x:r.x-a.x,y:r.y-a.y},d=this.normalizeAngle(i.rotation+t.delta),l=this.transformAnnotation(s.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:s.object.rect.origin.x+c.x,y:s.object.rect.origin.y+c.y},size:{...s.object.rect.size}}),o[i.id]=l}return o}startRotation(t,e){const{annotationIds:n,cursorAngle:o,rotationCenter:i}=e,{participants:s,attachedLinkIds:a}=this.buildRotationParticipants(n,t);if(0===s.length)return;const r=s.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:a,allParticipantIds:s.map(t=>t.id),rotationCenter:d,cursorStartAngle:o,currentAngle:o,delta:0,participants:s};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),s={...o,currentAngle:e,delta:i};this.unifiedRotateStates.set(t,s);const a=this.computeRotatePreviewPatches(s,t);this.unifiedRotate$.emit({documentId:t,type:"update",state:s,previewPatches:a})}commitRotation(t){const e=this.unifiedRotateStates.get(t);if(!e)return;const n=this.computeRotatePreviewPatches(e,t),o=[];for(const[i,s]of Object.entries(n)){const e=this.getAnnotationById(i,t);e&&o.push({pageIndex:e.object.pageIndex,id:i,patch:s})}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),s=t.map(({pageIndex:t,id:e,patch:n})=>{var o;const s=null==(o=i.byUid[e])?void 0:o.object;if(!s)return null;return{pageIndex:t,id:e,patch:this.buildPatch(s,{...n,author:n.author??this.config.annotationAuthor}),originalObject:s}}).filter(t=>null!==t);if(0===s.length)return;const a=()=>{for(const{pageIndex:t,id:e,patch:n,originalObject:i}of s)this.dispatch(C(o,t,e,n)),this.events$.emit({type:"update",documentId:o,annotation:i,pageIndex:t,patch:n,committed:!1})};if(!this.history)return a(),void(!1!==this.config.autoCommit&&this.commit(o));const r=s.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:a,undo:()=>{for(const{pageIndex:t,id:e,originalPatch:n,originalObject:i}of r)this.dispatch(C(o,t,e,n)),this.events$.emit({type:"update",documentId:o,annotation:i,pageIndex:t,patch:n,committed:!1})},metadata:{annotationIds:s.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),s=t.map(({pageIndex:t,id:e,patch:n})=>{var o;const s=null==(o=i.byUid[e])?void 0:o.object;return s?{pageIndex:t,id:e,patch:n,originalObject:s}:null}).filter(t=>null!==t);if(0===s.length)return;const a=()=>{for(const{pageIndex:t,id:e,patch:n,originalObject:i}of s)this.dispatch(k(o,t,e,n)),this.events$.emit({type:"update",documentId:o,annotation:i,pageIndex:t,patch:n,committed:!1})};if(!this.history)return a(),void(!1!==this.config.autoCommit&&this.commit(o));const r=s.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:a,undo:()=>{for(const{pageIndex:t,id:e,originalPatch:n,originalObject:i}of r)this.dispatch(k(o,t,e,n)),this.events$.emit({type:"update",documentId:o,annotation:i,pageIndex:t,patch:n,committed:!1})},metadata:{annotationIds:s.map(t=>t.id)}};this.history.forDocument(o).register(c,this.ANNOTATION_HISTORY_TOPIC)}moveAnnotationMethod(t,e,n,o="delta",i){const s=i??this.getActiveDocumentId(),a=this.getAnnotationById(e,s);if(!a)return;const r=a.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(a.object,{type:"move",changes:{rect:c}});this.moveAnnotationsMethod([{pageIndex:t,id:e,patch:d}],s)}getActiveTool(t){const e=this.getDocumentState(t);return e.activeToolId?this.state.tools.find(t=>t.id===e.activeToolId)??null:null}setActiveTool(t,n){var o,i;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 a=this.getDocumentState(s);if(t===a.activeToolId)return;this.dispatch(T(s,t));const r=this.state.tools.find(e=>e.id===t);r?null==(o=this.interactionManager)||o.forDocument(s).activate(r.interaction.mode??r.id):null==(i=this.interactionManager)||i.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}collectPendingChanges(t,e){const n=this.getDocumentState(t),o=this.pendingContexts.get(t),i={creations:[],updates:[],deletions:[],committedUids:[],isEmpty:!0};for(const[s,a]of Object.entries(n.byUid)){if("synced"===a.commitState)continue;if(e.pages.find(t=>t.index===a.object.pageIndex))switch(i.committedUids.push(s),i.isEmpty=!1,a.commitState){case"new":i.creations.push({uid:s,ta:a,ctx:null==o?void 0:o.get(a.object.id)});break;case"moved":i.updates.push({uid:s,ta:a,moved:!0});break;case"dirty":i.updates.push({uid:s,ta:a});break;case"deleted":i.deletions.push({uid:s,ta:a})}}return i}executeCommitBatch(t,n,o){const i=new e.Task,s=this.pendingContexts.get(t),a=[];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);a.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);a.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),a.push({type:"delete",task:t,ta:d,uid:c})}else this.dispatch(E(t,d.object.pageIndex,c))}const r=a.map(t=>t.task);return e.Task.allSettled(r).wait(()=>{this.emitCommitEvents(t,a,s);for(const o of a)"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:m,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({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({type:"update",documentId:t,annotation:i.ta.object,pageIndex:i.ta.object.pageIndex,patch:i.ta.object,committed:!0});break;case"delete":this.dispatch(E(t,i.ta.object.pageIndex,i.uid)),this.events$.emit({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 s=this.collectPendingChanges(n,i);if(s.isEmpty)return this.releaseCommitLock(n),e.PdfTaskHelper.resolve(!0);const a=new e.Task;return this.executeCommitBatch(n,i,s).wait(()=>{this.releaseCommitLock(n);this.getDocumentState(n).hasPendingChanges?this.commit(n).wait(t=>a.resolve(t),t=>a.fail(t)):a.resolve(!0)},t=>{this.releaseCommitLock(n),a.fail(t)}),a}};we.id="annotation";let je=we;function ze(t){return e=>(null==e?void 0:e.id)===t}const Le=ze("highlight"),Oe=ze("squiggly"),Ne=ze("underline"),Ue=ze("strikeout"),Be=ze("ink"),Fe=ze("inkHighlighter"),$e=ze("square"),Ge=ze("circle"),We=ze("line"),He=ze("polyline"),_e=ze("polygon"),Ye=ze("freeText"),Ve=ze("stamp"),Xe=ze("insertText"),Ke=ze("replaceText"),qe={manifest:o,create:(t,e)=>new je(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 s:{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 a:{const{documentId:n,annotations:o}=e.payload,i=t.documents[n];if(!i)return t;const s={},a={};for(const[t,e]of Object.entries(o)){const n=Number(t),o=i.pages[n]||[];for(const t of o)delete a[t];const r=e.map(t=>{const e=t.id;return a[e]={commitState:"synced",object:t},e});s[n]=r}return{...t,documents:{...t.documents,[n]:{...i,pages:s,byUid:a}}}}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 s=[...i.selectedUids,o];return{...t,documents:{...t.documents,[n]:{...i,selectedUids:s,selectedUid:ht(s)}}}}case l:{const{documentId:n,id:o}=e.payload,i=t.documents[n];if(!i)return t;const s=i.selectedUids.filter(t=>t!==o);return{...t,documents:{...t.documents,[n]:{...i,selectedUids:s,selectedUid:ht(s)}}}}case u:{const{documentId:n,ids:o}=e.payload,i=t.documents[n];return i?{...t,documents:{...t.documents,[n]:{...i,selectedUids:o,selectedUid:ht(o)}}}:t}case h:{const{documentId:n,toolId:o}=e.payload,i=t.documents[n];return i?{...t,documents:{...t.documents,[n]:{...i,activeToolId:o}}}:t}case g:{const{documentId:n,pageIndex:o,annotation:i}=e.payload,s=t.documents[n];if(!s)return t;const a=i.id;return{...t,documents:{...t.documents,[n]:{...s,pages:{...s.pages,[o]:[...s.pages[o]??[],a]},byUid:{...s.byUid,[a]:{commitState:"new",object:i,dictMode:!0}},hasPendingChanges:!0}}}}case y:{const{documentId:n,pageIndex:o,id:i}=e.payload,s=t.documents[n];return s&&s.byUid[i]?{...t,documents:{...t.documents,[n]:{...s,pages:{...s.pages,[o]:(s.pages[o]??[]).filter(t=>t!==i)},byUid:{...s.byUid,[i]:{...s.byUid[i],commitState:"deleted"}},hasPendingChanges:!0}}}:t}case p:{const{documentId:n,id:o,patch:i}=e.payload,s=t.documents[n];return s?{...t,documents:{...t.documents,[n]:pt(s,o,i)}}:t}case f:{const{documentId:n,id:o,patch:i}=e.payload,s=t.documents[n];return s?{...t,documents:{...t.documents,[n]:ft(s,o,i)}}:t}case m:{const{documentId:n,committedUids:o}=e.payload,i=t.documents[n];if(!i)return t;const s=new Set(o),a={};let r=!1;for(const[t,e]of Object.entries(i.byUid))s.has(t)?a[t]={...e,commitState:"dirty"===e.commitState||"new"===e.commitState||"moved"===e.commitState?"synced":e.commitState}:(a[t]=e,"new"!==e.commitState&&"dirty"!==e.commitState&&"moved"!==e.commitState&&"deleted"!==e.commitState||(r=!0));return{...t,documents:{...t.documents,[n]:{...i,byUid:a,hasPendingChanges:r}}}}case A:{const{documentId:n,pageIndex:o,uid:i}=e.payload,s=t.documents[n];if(!s||!s.byUid[i])return t;const{[i]:a,...r}=s.byUid;return{...t,documents:{...t.documents,[n]:{...s,pages:{...s.pages,[o]:(s.pages[o]??[]).filter(t=>t!==i)},byUid:r}}}}case I:{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 b:{const{toolId:n,patch:o}=e.payload;return{...t,tools:t.tools.map(t=>t.id===n?{...t,defaults:{...t.defaults,...o}}:t)}}case x:return t.colorPresets.includes(e.payload)?t:{...t,colorPresets:[...t.colorPresets,e.payload]};default:return t}},initialState:(t,e)=>yt(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=je,exports.AnnotationPluginPackage=qe,exports.convertAABBRectToUnrotatedSpace=Ht,exports.createToolPredicate=ze,exports.generateCloudyEllipsePath=function(t,e,n,o){const i=new qt;let s=0,a=0,r=t.width,c=t.height;return e&&(s+=e.left,a+=e.top,r-=e.right,c-=e.bottom),he(s,-c,r,-a,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 s=t.map(t=>({x:t.x-e.x,y:-(t.y-e.y)})),a=s[0],r=s[s.length-1];return a.x===r.x&&a.y===r.y||s.push({x:a.x,y:a.y}),de(s,!1,n,o,i),i.close(),i.build(o)},exports.generateCloudyRectanglePath=function(t,e,n,o){const i=new qt;let s=0,a=0,r=t.width,c=t.height;return e?(s+=e.left,a+=e.top,r-=e.right,c-=e.bottom):(s+=o/2,a+=o/2,r-=o/2,c-=o/2),de([{x:s,y:-a},{x:r,y:-a},{x:r,y:-c},{x:s,y:-c},{x:s,y:-a}],!1,n,o,i),i.close(),i.build(o)},exports.getAnnotationByUid=tt,exports.getAnnotations=t=>{const e={};for(const n of Object.keys(t.pages).map(Number))e[n]=q(t,n);return e},exports.getAnnotationsByPageIndex=q,exports.getAttachedLinks=it,exports.getCloudyBorderExtent=pe,exports.getGroupLeaderId=st,exports.getGroupMembers=at,exports.getIRTChildIds=nt,exports.getIRTChildrenByType=ot,exports.getSelectedAnnotation=Q,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=J,exports.getSelectedAnnotations=Z,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=ct,exports.getSidebarAnnotationsWithReplies=t=>{const e=et(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=et,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)=>it(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.hasMultipleSelected=t=>t.selectedUids.length>1,exports.initialDocumentState=gt,exports.initialState=yt,exports.isAnnotationSelected=(t,e)=>t.selectedUids.includes(e),exports.isCaret=X,exports.isCircle=z,exports.isCircleTool=Ge,exports.isFreeText=H,exports.isFreeTextTool=Ye,exports.isHighlight=B,exports.isHighlightTool=Le,exports.isInGroup=rt,exports.isInk=j,exports.isInkHighlighterTool=Fe,exports.isInkTool=Be,exports.isInsertTextTool=Xe,exports.isLine=N,exports.isLineTool=We,exports.isLink=function(t){return t.object.type===e.PdfAnnotationSubtype.LINK},exports.isPolygon=L,exports.isPolygonTool=_e,exports.isPolyline=U,exports.isPolylineTool=He,exports.isRedact=V,exports.isReplaceTextTool=Ke,exports.isSidebarAnnotation=K,exports.isSquare=O,exports.isSquareTool=$e,exports.isSquiggly=G,exports.isSquigglyTool=Oe,exports.isStamp=_,exports.isStampTool=Ve,exports.isStrikeout=$,exports.isStrikeoutTool=Ue,exports.isText=Y,exports.isTextMarkup=W,exports.isUnderline=F,exports.isUnderlineTool=Ne,exports.manifest=o,exports.patching=Ut,exports.rectsIntersect=w,exports.resolveInteractionProp=function(t,e,n){return void 0===t?n:"function"==typeof t?t(e):t};
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|