/**
 * 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.
 *
 */

import{$getNodeByKey as t,$isTextNode as e,$createRangeSelection as n,$isElementNode as o}from"lexical";function s(t,...e){const n=new URL("https://lexical.dev/docs/error"),o=new URLSearchParams;o.append("code",t);for(const t of e)o.append("v",t);throw n.search=o.toString(),Error(`Minified Lexical error #${t}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}class l{_offsetMap;_firstNode;_blockOffsetSize;constructor(t,e,n=1){this._offsetMap=t,this._firstNode=e,this._blockOffsetSize=n}createSelectionFromOffsets(o,s,l){const f=this._firstNode;if(null===f)return null;let c=o,u=s,a=i(f,c,this._blockOffsetSize),d=i(f,u,this._blockOffsetSize);if(void 0!==l&&(c=r(c,a,l,this,this._blockOffsetSize),a=i(f,c,this._blockOffsetSize),u=r(u,d,l,this,this._blockOffsetSize),d=i(f,u,this._blockOffsetSize)),null===a||null===d)return null;let p=a.key,g=d.key;const h=t(p),_=t(g);if(null===h||null===_)return null;let k=0,v=0,y="element",x="element";if("text"===a.type){k=c-a.start,y="text";const t=h.getNextSibling();c!==u&&k===h.getTextContentSize()&&e(t)&&(k=0,p=t.__key)}else"inline"===a.type&&(p=h.getParentOrThrow().getKey(),k=u>a.start?a.end:a.start);"text"===d.type?(v=u-d.start,x="text"):"inline"===d.type&&(g=_.getParentOrThrow().getKey(),v=u>d.start?d.end:d.start);const S=n();return null===S?null:(S.anchor.set(p,k,y),S.focus.set(g,v,x),S)}getOffsetsFromSelection(t){const e=t.anchor,n=t.focus,o=this._offsetMap,s=e.offset,l=n.offset;let r=-1,i=-1;if("text"===e.type){const t=o.get(e.key);void 0!==t&&(r=t.start+s)}else{const t=e.getNode().getDescendantByIndex(s);if(null!==t){const e=o.get(t.getKey());if(void 0!==e){r=t.getIndexWithinParent()!==s?e.end:e.start}}}if("text"===n.type){const t=o.get(n.key);void 0!==t&&(i=t.start+n.offset)}else{const t=n.getNode().getDescendantByIndex(l);if(null!==t){const e=o.get(t.getKey());if(void 0!==e){i=t.getIndexWithinParent()!==l?e.end:e.start}}}return[r,i]}}function r(t,e,n,o,s){const l=n._offsetMap,r=o._offsetMap,f=new Set;let c=t,u=e;for(;null!==u;){const t=u.key,e=l.get(t),n=u.end-u.start;if(f.add(t),void 0===e)c+=n;else{const t=e.end-e.start;t!==n&&(c+=n-t)}const o=u.prev;if(null!==o){u=o;continue}let s=u.parent;for(;null!==s;){let t=s.prev;if(null!==t){const e=t.key,n=l.get(e),o=t.end-t.start;if(f.add(e),void 0===n)c+=o;else{const t=n.end-n.start;t!==o&&(c+=o-t)}t=t.prev}s=s.parent}break}const a=n._firstNode;if(null!==a){u=i(a,t,s);let e=!1;for(;null!==u;){if(!f.has(u.key)){e=!0;break}u=u.parent}if(!e)for(;null!==u;){const t=u.key;if(!f.has(t)){const e=r.get(t),n=u.end-u.start;if(void 0===e)c-=n;else{const t=e.end-e.start;n!==t&&(c+=t-n)}}u=u.prev}}return c}function i(t,e,n){let o=t;for(;null!==o;){if(e<o.end+("element"!==o.type||0===n?1:0)){const t=o.child;if(null!==t){o=t;continue}return o}const t=o.next;if(null===t)break;o=t}return null}function f(t,e,n,o,s,l){return{child:t,end:o,key:s,next:null,parent:l,prev:null,start:n,type:e}}function c(t,n,l,r,i,c){const d=r.get(n);void 0===d&&s(3);const p=t.offset;if(o(d)){const e=a(d,r),o=0===e.length,s=o?null:u(t,e,null,r,i,c);t.prevIsBlock&&!o||(t.prevIsBlock=!0,t.offset+=c);const g=f(s,"element",p,p,n,l);null!==s&&(s.parent=g);const h=t.offset;return g.end=h,i.set(n,g),g}t.prevIsBlock=!1;const g=e(d),h=g?d.__text.length:1,_=f(null,g?"text":"inline",p,t.offset+=h,n,l);return i.set(n,_),_}function u(t,e,n,o,s,l){let r=null,i=null;const f=e.length;for(let u=0;u<f;u++){const f=c(t,e[u],n,o,s,l);null===i?r=f:(f.prev=i,i.next=f),i=f}return r}function a(e,n){const o=[];let l=e.__first;for(;null!==l;){const e=null===n?t(l):n.get(l);null==e&&s(174),o.push(l),l=e.__next}return o}const d=a;function p(t,e=1,n){const o=(n||t._pendingEditorState||t._editorState)._nodeMap,s=o.get("root"),r=new Map,i=u({offset:0,prevIsBlock:!1},a(s,o),null,o,r,e);return new l(r,i,e)}export{a as $createChildrenArray,p as $createOffsetView,l as OffsetView,d as createChildrenArray};
