/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */
'use strict';var p=require("lexical");
class t{constructor(a,b,c=1){this._offsetMap=a;this._firstNode=b;this._blockOffsetSize=c}createSelectionFromOffsets(a,b,c){var d=this._firstNode;if(null===d)return null;var f=y(d,a,this._blockOffsetSize);let h=y(d,b,this._blockOffsetSize);void 0!==c&&(a=z(a,f,c,this,this._blockOffsetSize),f=y(d,a,this._blockOffsetSize),b=z(b,h,c,this,this._blockOffsetSize),h=y(d,b,this._blockOffsetSize));if(null===f||null===h)return null;c=f.key;d=h.key;let m=p.$getNodeByKey(c),k=p.$getNodeByKey(d);if(null===m||null===
k)return null;let l=0,e=0,g="element",n="element";"text"===f.type?(l=a-f.start,g="text",f=m.getNextSibling(),a!==b&&l===m.getTextContentSize()&&p.$isTextNode(f)&&(l=0,c=f.__key)):"inline"===f.type&&(c=m.getParentOrThrow().getKey(),l=b>f.start?f.end:f.start);"text"===h.type?(e=b-h.start,n="text"):"inline"===h.type&&(d=k.getParentOrThrow().getKey(),e=b>h.start?h.end:h.start);a=p.$createRangeSelection();if(null===a)return null;a.anchor.set(c,l,g);a.focus.set(d,e,n);return a}getOffsetsFromSelection(a){var b=
a.anchor,c=a.focus,d=this._offsetMap;let f=b.offset;var h=c.offset;let m=a=-1;if("text"===b.type)b=d.get(b.key),void 0!==b&&(a=b.start+f);else if(b=b.getNode().getDescendantByIndex(f),null!==b){let k=d.get(b.getKey());void 0!==k&&(a=b.getIndexWithinParent()!==f?k.end:k.start)}"text"===c.type?(h=d.get(c.key),void 0!==h&&(m=h.start+c.offset)):(c=c.getNode().getDescendantByIndex(h),null!==c&&(d=d.get(c.getKey()),void 0!==d&&(m=c.getIndexWithinParent()!==h?d.end:d.start)));return[a,m]}}
function z(a,b,c,d,f){let h=c._offsetMap;d=d._offsetMap;let m=new Set,k=a;for(;null!==b;){var l=b.key,e=h.get(l),g=b.end-b.start;m.add(l);void 0===e?k+=g:(l=e.end-e.start,l!==g&&(k+=g-l));g=b.prev;if(null!==g)b=g;else{for(b=b.parent;null!==b;)e=b.prev,null!==e&&(g=e.key,l=h.get(g),e=e.end-e.start,m.add(g),void 0===l?k+=e:(g=l.end-l.start,g!==e&&(k+=e-g))),b=b.parent;break}}c=c._firstNode;if(null!==c){b=y(c,a,f);for(a=!1;null!==b;){if(!m.has(b.key)){a=!0;break}b=b.parent}if(!a)for(;null!==b;)a=b.key,
m.has(a)||(f=d.get(a),a=b.end-b.start,void 0===f?k-=a:(f=f.end-f.start,a!==f&&(k+=f-a))),b=b.prev}return k}function y(a,b,c){for(;null!==a;){if(b<a.end+("element"!==a.type||0===c?1:0)){let d=a.child;if(null!==d){a=d;continue}return a}a=a.next;if(null===a)break}return null}
function A(a,b,c,d,f,h){let m=null,k=null,l=b.length;for(let v=0;v<l;v++){{var e=a;var g=b[v];var n=c,q=d,w=f,u=h,r=q.get(g);if(void 0===r)throw Error("Minified Lexical error #3; visit https://lexical.dev/docs/error?code=3 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.");let x=e.offset;if(p.$isElementNode(r)){let C=B(r,q);q=(r=0===C.length)?null:A(e,C,null,q,w,u);if(!e.prevIsBlock||r)e.prevIsBlock=!0,e.offset+=u;n={child:q,end:x,key:g,
next:null,parent:n,prev:null,start:x,type:"element"};null!==q&&(q.parent=n);n.end=e.offset;w.set(g,n);g=n}else e.prevIsBlock=!1,u=p.$isTextNode(r),e={child:null,end:e.offset+=u?r.__text.length:1,key:g,next:null,parent:n,prev:null,start:x,type:u?"text":"inline"},w.set(g,e),g=e}null===k?m=g:(g.prev=k,k.next=g);k=g}return m}
function B(a,b){let c=[];for(a=a.__first;null!==a;){let d=null===b?p.$getNodeByKey(a):b.get(a);if(null===d||void 0===d)throw Error("createChildrenArray: node does not exist in nodeMap");c.push(a);a=d.__next}return c}exports.$createOffsetView=function(a,b=1,c){c=(c||a._pendingEditorState||a._editorState)._nodeMap;let d=c.get("root");a=new Map;c=A({offset:0,prevIsBlock:!1},B(d,c),null,c,a,b);return new t(a,c,b)};exports.OffsetView=t;exports.createChildrenArray=B
