/**
 * 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{useLexicalComposerContext as e}from"@lexical/react/LexicalComposerContext";import{useEffect as t,useState as n,useMemo as r}from"react";import{OverflowNode as o,$isOverflowNode as i,$createOverflowNode as s}from"@lexical/overflow";import{$rootTextContent as l}from"@lexical/text";import{mergeRegister as c,$dfs as a,$findMatchingParent as f,$unwrapNode as g}from"@lexical/utils";import{HISTORY_MERGE_TAG as m,DELETE_CHARACTER_COMMAND as u,$getSelection as d,$isRangeSelection as h,$isElementNode as p,COMMAND_PRIORITY_LOW as x,$isLeafNode as C,$isTextNode as S,$setSelection as v}from"lexical";import{jsx as w}from"react/jsx-runtime";function T(e,n,r=Object.freeze({})){const{strlen:s=e=>e.length,remainingCharacters:w=()=>{}}=r;t(()=>{e.hasNodes([o])||function(e,...t){const n=new URL("https://lexical.dev/docs/error"),r=new URLSearchParams;r.append("code",e);for(const e of t)r.append("v",e);throw n.search=r.toString(),Error(`Minified Lexical error #${e}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}(57)},[e]),t(()=>{let t=e.getEditorState().read(l),r=0;return c(e.registerTextContentListener(e=>{t=e}),e.registerUpdateListener(({dirtyLeaves:o,dirtyElements:l})=>{const c=e.isComposing(),u=o.size>0||l.size>0;if(c||!u)return;const p=s(t),x=p>n||null!==r&&r>n;if(w(n-p),null===r||x){const r=function(e,t,n){const r=Intl.Segmenter;let o=0,i=0;if("function"==typeof r){const s=(new r).segment(e);for(const{segment:e}of s){const r=i+n(e);if(r>t)break;i=r,o+=e.length}}else{const r=Array.from(e),s=r.length;for(let e=0;e<s;e++){const s=r[e],l=i+n(s);if(l>t)break;i=l,o+=s.length}}return o}(t,n,s);e.update(()=>{!function(e){const t=a(),n=t.length;let r=0;for(let o=0;o<n;o+=1){const{node:n}=t[o],s=C(n)&&!f(n,i);if(i(n)){const t=r;if(r+n.getTextContentSize()<=e){const e=n.getParent(),t=n.getPreviousSibling(),r=n.getNextSibling();g(n);const o=d();!h(o)||o.anchor.getNode().isAttached()&&o.focus.getNode().isAttached()||(S(t)?t.select():S(r)?r.select():null!==e&&e.select())}else if(t<e){const r=n.getFirstDescendant(),o=t+(null!==r?r.getTextContentSize():0);(S(r)&&r.isSimpleText()||o<=e)&&g(n)}}else if(s){const t=r;if(r+=n.getTextContentSize(),r>e&&!i(n.getParent())){const r=d();let o;if(t<e&&S(n)&&n.isSimpleText()){const[,r]=n.splitText(e-t);o=y(r)}else o=y(n);null!==r&&v(r),N(o)}}}}(r)},{tag:m})}r=p}),e.registerCommand(u,e=>{const t=d();if(!h(t))return!1;const n=t.anchor.getNode().getParent(),r=n?n.getParent():null,o=r?r.getNextSibling():null;return t.deleteCharacter(e),r&&r.isEmpty()?r.remove():p(o)&&o.isEmpty()&&o.remove(),!0},x))},[e,n,w,s])}function y(e){const t=s();return e.replace(t),t.append(e),t}function N(e){const t=e.getPreviousSibling();if(!i(t))return;const n=e.getFirstChild(),r=t.getChildren(),o=r.length;if(null===n)e.append(...r);else for(let e=0;e<o;e++)n.insertBefore(r[e]);const s=d();if(h(s)){const n=s.anchor,r=n.getNode(),i=s.focus,l=n.getNode();r.is(t)?n.set(e.getKey(),n.offset,"element"):r.is(e)&&n.set(e.getKey(),o+n.offset,"element"),l.is(t)?i.set(e.getKey(),i.offset,"element"):l.is(e)&&i.set(e.getKey(),o+i.offset,"element")}t.remove()}let b=null;function E(e){const t=void 0===window.TextEncoder?null:(null===b&&(b=new window.TextEncoder),b);if(null===t){const t=encodeURIComponent(e).match(/%[89ABab]/g);return e.length+(t?t.length:0)}return t.encode(e).length}function L({remainingCharacters:e}){return w("span",{className:"characters-limit "+(e<0?"characters-limit-exceeded":""),children:e})}function U({charset:t="UTF-16",maxLength:o=5,renderer:i=L}){const[s]=e(),[l,c]=n(o);return T(s,o,r(()=>({remainingCharacters:c,strlen:e=>{if("UTF-8"===t)return E(e);if("UTF-16"===t)return e.length;throw new Error("Unrecognized charset")}}),[t])),i({remainingCharacters:l})}export{U as CharacterLimitPlugin};
