/* esm.sh - @embedpdf/plugin-viewport@2.15.1 */
import{BasePlugin as A,createBehaviorEmitter as a,createEmitter as O}from"../../../core@2.15.1/X-ZHJlYWN0LWRvbUAxOS4zLjEscmVhY3RAMTkuMy4x/es2022/core.mjs";var S="viewport",C={id:S,name:"Viewport Plugin",version:"1.0.0",provides:["viewport"],requires:[],optional:[],defaultConfig:{viewportGap:10,scrollEndDelay:300}},v="INIT_VIEWPORT_STATE",V="CLEANUP_VIEWPORT_STATE",f="REGISTER_VIEWPORT",T="UNREGISTER_VIEWPORT",m="SET_VIEWPORT_METRICS",E="SET_VIEWPORT_SCROLL_METRICS",y="SET_VIEWPORT_GAP",R="SET_SCROLL_ACTIVITY",G="SET_SMOOTH_SCROLL_ACTIVITY",D="SET_ACTIVE_VIEWPORT_DOCUMENT",_="ADD_VIEWPORT_GATE",$="REMOVE_VIEWPORT_GATE";function I(o){return{type:v,payload:{documentId:o}}}function W(o){return{type:V,payload:{documentId:o}}}function L(o){return{type:f,payload:{documentId:o}}}function b(o){return{type:T,payload:{documentId:o}}}function z(o){return{type:y,payload:o}}function H(o,t){return{type:m,payload:{documentId:o,metrics:t}}}function q(o,t){return{type:E,payload:{documentId:o,scrollMetrics:t}}}function U(o,t){return{type:R,payload:{documentId:o,isScrolling:t}}}function w(o,t){return{type:G,payload:{documentId:o,isSmoothScrolling:t}}}function N(o,t){return{type:_,payload:{documentId:o,key:t}}}function x(o,t){return{type:$,payload:{documentId:o,key:t}}}var Y={viewportMetrics:{width:0,height:0,scrollTop:0,scrollLeft:0,clientWidth:0,clientHeight:0,scrollWidth:0,scrollHeight:0,clientLeft:0,clientTop:0,relativePosition:{x:0,y:0}},isScrolling:!1,isSmoothScrolling:!1,gates:new Set},P={viewportGap:0,documents:{},activeViewports:new Set,activeDocumentId:null},B=(o=P,t)=>{switch(t.type){case v:{let{documentId:i}=t.payload;return{...o,documents:{...o.documents,[i]:{...Y,gates:new Set}}}}case V:{let{documentId:i}=t.payload,{[i]:e,...s}=o.documents,r=new Set(o.activeViewports);return r.delete(i),{...o,documents:s,activeViewports:r,activeDocumentId:o.activeDocumentId===i?null:o.activeDocumentId}}case f:{let{documentId:i}=t.payload,e=new Set(o.activeViewports);return e.add(i),{...o,activeViewports:e,activeDocumentId:o.activeDocumentId??i}}case T:{let{documentId:i}=t.payload,e=new Set(o.activeViewports);return e.delete(i),{...o,activeViewports:e}}case D:return{...o,activeDocumentId:t.payload};case y:return{...o,viewportGap:t.payload};case m:{let{documentId:i,metrics:e}=t.payload,s=o.documents[i];return s?{...o,documents:{...o.documents,[i]:{...s,viewportMetrics:{width:e.width,height:e.height,scrollTop:e.scrollTop,scrollLeft:e.scrollLeft,clientWidth:e.clientWidth,clientHeight:e.clientHeight,scrollWidth:e.scrollWidth,scrollHeight:e.scrollHeight,clientLeft:e.clientLeft,clientTop:e.clientTop,relativePosition:{x:e.scrollWidth<=e.clientWidth?0:e.scrollLeft/(e.scrollWidth-e.clientWidth),y:e.scrollHeight<=e.clientHeight?0:e.scrollTop/(e.scrollHeight-e.clientHeight)}}}}}:o}case E:{let{documentId:i,scrollMetrics:e}=t.payload,s=o.documents[i];return s?{...o,documents:{...o.documents,[i]:{...s,viewportMetrics:{...s.viewportMetrics,scrollTop:e.scrollTop,scrollLeft:e.scrollLeft},isScrolling:!0}}}:o}case R:{let{documentId:i,isScrolling:e}=t.payload,s=o.documents[i];return s?{...o,documents:{...o.documents,[i]:{...s,isScrolling:e}}}:o}case G:{let{documentId:i,isSmoothScrolling:e}=t.payload,s=o.documents[i];return s?{...o,documents:{...o.documents,[i]:{...s,isSmoothScrolling:e}}}:o}case _:{let{documentId:i,key:e}=t.payload,s=o.documents[i];if(!s)return o;let r=new Set(s.gates);return r.add(e),{...o,documents:{...o.documents,[i]:{...s,gates:r}}}}case $:{let{documentId:i,key:e}=t.payload,s=o.documents[i];if(!s)return o;let r=new Set(s.gates);return r.delete(e),{...o,documents:{...o.documents,[i]:{...s,gates:r}}}}default:return o}},M=class extends A{constructor(t,i,e){super(t,i),this.id=t,this.viewportResize$=a(),this.viewportMetrics$=a(),this.scrollMetrics$=a(),this.scrollActivity$=a(),this.gateState$=a(),this.scrollRequests$=new Map,e.viewportGap&&this.dispatch(z(e.viewportGap)),this.scrollEndDelay=e.scrollEndDelay||100}onDocumentLoadingStarted(t){this.dispatch(I(t)),this.scrollRequests$.set(t,O()),this.logger.debug("ViewportPlugin","DocumentOpened",`Initialized viewport state for document: ${t}`)}onDocumentClosed(t){var i;this.dispatch(W(t)),(i=this.scrollRequests$.get(t))==null||i.clear(),this.scrollRequests$.delete(t),this.logger.debug("ViewportPlugin","DocumentClosed",`Cleaned up viewport state for document: ${t}`)}buildCapability(){return{getViewportGap:()=>this.state.viewportGap,getMetrics:()=>this.getMetrics(),scrollTo:t=>this.scrollTo(t),isScrolling:()=>this.isScrolling(),isSmoothScrolling:()=>this.isSmoothScrolling(),isGated:t=>this.isGated(t),hasGate:(t,i)=>this.hasGate(t,i),getGates:t=>this.getGates(t),forDocument:t=>this.createViewportScope(t),gate:(t,i)=>this.gate(t,i),releaseGate:(t,i)=>this.releaseGate(t,i),isViewportMounted:t=>this.state.activeViewports.has(t),onViewportChange:this.viewportMetrics$.on,onViewportResize:this.viewportResize$.on,onScrollChange:this.scrollMetrics$.on,onScrollActivity:this.scrollActivity$.on,onGateChange:this.gateState$.on}}createViewportScope(t){return{getMetrics:()=>this.getMetrics(t),scrollTo:i=>this.scrollTo(i,t),isScrolling:()=>this.isScrolling(t),isSmoothScrolling:()=>this.isSmoothScrolling(t),isGated:()=>this.isGated(t),hasGate:i=>this.hasGate(i,t),getGates:()=>this.getGates(t),gate:i=>this.gate(i,t),releaseGate:i=>this.releaseGate(i,t),onViewportChange:i=>this.viewportMetrics$.on(e=>{e.documentId===t&&i(e.metrics)}),onScrollChange:i=>this.scrollMetrics$.on(e=>{e.documentId===t&&i(e.scrollMetrics)}),onScrollActivity:i=>this.scrollActivity$.on(e=>{e.documentId===t&&i(e.activity)}),onGateChange:i=>this.gateState$.on(e=>{e?.documentId===t&&i(e)})}}registerViewport(t){if(!this.state.documents[t])throw new Error(`Cannot register viewport for ${t}: document state not found. Document must be opened before registering viewport.`);this.state.activeViewports.has(t)||(this.dispatch(L(t)),this.logger.debug("ViewportPlugin","RegisterViewport",`Registered viewport (DOM mounted) for document: ${t}`))}unregisterViewport(t){this.registry.isDestroyed()||this.state.activeViewports.has(t)&&(this.dispatch(b(t)),this.logger.debug("ViewportPlugin","UnregisterViewport",`Unregistered viewport (DOM unmounted) for document: ${t}. State preserved.`))}setViewportResizeMetrics(t,i){if(this.registry.isDestroyed())return;this.dispatch(H(t,i));let e=this.state.documents[t];e&&this.viewportResize$.emit({documentId:t,metrics:e.viewportMetrics})}setViewportScrollMetrics(t,i){if(this.registry.isDestroyed())return;let e=this.state.documents[t];e&&(i.scrollTop!==e.viewportMetrics.scrollTop||i.scrollLeft!==e.viewportMetrics.scrollLeft)&&(this.dispatch(q(t,i)),this.bumpScrollActivity(t),this.scrollMetrics$.emit({documentId:t,scrollMetrics:i}))}onScrollRequest(t,i){let e=this.scrollRequests$.get(t);if(!e)throw new Error(`Cannot subscribe to scroll requests for ${t}: document state not initialized`);return e.on(i)}gate(t,i){let e=this.state.documents[i];if(!e){this.logger.warn("ViewportPlugin","GateViewport",`Cannot gate viewport for ${i}: document not found`);return}e.gates.has(t)||(this.dispatch(N(i,t)),this.logger.debug("ViewportPlugin","GateAdded",`Added gate '${t}' for document: ${i}. Total gates: ${e.gates.size+1}`))}releaseGate(t,i){let e=this.state.documents[i];if(!e){this.logger.warn("ViewportPlugin","ReleaseGate",`Cannot release gate for ${i}: document not found`);return}e.gates.has(t)&&(this.dispatch(x(i,t)),this.logger.debug("ViewportPlugin","GateReleased",`Released gate '${t}' for document: ${i}. Remaining gates: ${e.gates.size-1}`))}getViewportState(t){let i=t??this.getActiveDocumentId(),e=this.state.documents[i];if(!e)throw new Error(`Viewport state not found for document: ${i}`);return e}getMetrics(t){return this.getViewportState(t).viewportMetrics}isScrolling(t){return this.getViewportState(t).isScrolling}isSmoothScrolling(t){return this.getViewportState(t).isSmoothScrolling}isGated(t){return this.getViewportState(t).gates.size>0}hasGate(t,i){return this.getViewportState(i).gates.has(t)}getGates(t){let i=this.getViewportState(t);return Array.from(i.gates)}scrollTo(t,i){let e=i??this.getActiveDocumentId(),s=this.getViewportState(e),{x:r,y:l,alignX:n,alignY:p,behavior:h="auto"}=t;h==="smooth"&&this.dispatch(w(e,!0));let c=s.viewportMetrics,g=r,u=l;n!==void 0&&(g=r-c.clientWidth*(n/100)),p!==void 0&&(u=l-c.clientHeight*(p/100));let d=this.scrollRequests$.get(e);d&&d.emit({x:g,y:u,behavior:h})}bumpScrollActivity(t){this.debouncedDispatch(U(t,!1),this.scrollEndDelay),this.debouncedDispatch(w(t,!1),this.scrollEndDelay)}onStoreUpdated(t,i){for(let e in i.documents){let s=t.documents[e],r=i.documents[e];if(s!==r&&(this.viewportMetrics$.emit({documentId:e,metrics:r.viewportMetrics}),s&&(s.isScrolling!==r.isScrolling||s.isSmoothScrolling!==r.isSmoothScrolling)&&this.scrollActivity$.emit({documentId:e,activity:{isScrolling:r.isScrolling,isSmoothScrolling:r.isSmoothScrolling}}),s&&s.gates!==r.gates)){let l=Array.from(s.gates),n=Array.from(r.gates),p=n.find(c=>!l.includes(c)),h=l.find(c=>!n.includes(c));this.gateState$.emit({documentId:e,isGated:r.gates.size>0,gates:n,addedGate:p,removedGate:h}),this.logger.debug("ViewportPlugin","GateStateChanged",`Gate state changed for document ${e}. Gates: [${n.join(", ")}], Gated: ${r.gates.size>0}`)}}}async initialize(t){this.logger.info("ViewportPlugin","Initialize","Viewport plugin initialized")}async destroy(){this.viewportMetrics$.clear(),this.viewportResize$.clear(),this.scrollMetrics$.clear(),this.scrollActivity$.clear(),this.gateState$.clear(),this.scrollRequests$.forEach(t=>t.clear()),this.scrollRequests$.clear(),super.destroy()}};M.id="viewport";var X=M,J={manifest:C,create:(o,t)=>new X(S,o,t),reducer:B,initialState:P};export{S as VIEWPORT_PLUGIN_ID,X as ViewportPlugin,J as ViewportPluginPackage,C as manifest};
//# sourceMappingURL=plugin-viewport.mjs.map