import{Plugin as b}from"@ckeditor/ckeditor5-core";import{Template as y,View as x}from"@ckeditor/ckeditor5-ui";import{env as T}from"@ckeditor/ckeditor5-utils";/**
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/function p(e){if(e.is("$text")||e.is("$textProxy"))return e.data;const t=e;let n="",i=null;for(const r of t.getChildren()){const o=p(r);i&&i.is("element")&&(n+=`
`),n+=o,i=r}return n}function C(e){return typeof e=="object"&&e!==null}function O(e,t,{signal:n,edges:i}={}){let r,o=null;const u=i!=null&&i.includes("leading"),c=i==null||i.includes("trailing"),l=()=>{o!==null&&(e.apply(r,o),r=void 0,o=null)},a=()=>{c&&l(),g()};let s=null;const h=()=>{s!=null&&clearTimeout(s),s=setTimeout(()=>{s=null,a()},t)},d=()=>{s!==null&&(clearTimeout(s),s=null)},g=()=>{d(),r=void 0,o=null},_=()=>{d(),l()},f=function(...w){if(n?.aborted)return;r=this,o=w;const m=s==null;h(),u&&m&&l()};return f.schedule=h,f.cancel=g,f.flush=_,n?.addEventListener("abort",g,{once:!0}),f}function P(e,t=0,n={}){typeof n!="object"&&(n={});const{leading:i=!1,trailing:r=!0,maxWait:o}=n,u=Array(2);i&&(u[0]="leading"),r&&(u[1]="trailing");let c,l=null;const a=O(function(...d){c=e.apply(this,d),l=null},t,{edges:u}),s=function(...d){return o!=null&&(l===null&&(l=Date.now()),Date.now()-l>=o)?(c=e.apply(this,d),l=Date.now(),a.cancel(),a.schedule(),c):(a.apply(this,d),c)},h=()=>(a.flush(),c);return s.cancel=a.cancel,s.flush=h,s}function W(e,t=0,n={}){typeof n!="object"&&(n={});const{leading:i=!0,trailing:r=!0}=n;return P(e,t,{leading:i,trailing:r,maxWait:t})}function S(e){if(typeof e!="object"||e==null)return!1;if(Object.getPrototypeOf(e)===null)return!0;if(Object.prototype.toString.call(e)!=="[object Object]"){const n=e[Symbol.toStringTag];return n==null||!Object.getOwnPropertyDescriptor(e,Symbol.toStringTag)?.writable?!1:e.toString()===`[object ${n}]`}let t=e;for(;Object.getPrototypeOf(t)!==null;)t=Object.getPrototypeOf(t);return Object.getPrototypeOf(e)===t}function V(e){return C(e)&&e.nodeType===1&&!S(e)}/**
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/class E extends b{_config;_outputView;_wordsMatchRegExp;constructor(t){super(t),this.set("characters",0),this.set("words",0),Object.defineProperties(this,{characters:{get(){return this.characters=this._getCharacters(this._getText())}},words:{get(){return this.words=this._getWords(this._getText())}}}),this.set("_wordsLabel",void 0),this.set("_charactersLabel",void 0),this._config=t.config.get("wordCount")||{},this._outputView=void 0,this._wordsMatchRegExp=T.features.isRegExpUnicodePropertySupported?new RegExp("([\\p{L}\\p{N}]+\\S?)+","gu"):/([a-zA-Z0-9À-ž]+\S?)+/gu}static get pluginName(){return"WordCount"}static get licenseFeatureCode(){return"WC"}static get isOfficialPlugin(){return!0}static get isPremiumPlugin(){return!0}init(){this.editor.model.document.on("change:data",W(this._refreshStats.bind(this),250)),typeof this._config.onUpdate=="function"&&this.on("update",(n,i)=>{this._config.onUpdate(i)}),V(this._config.container)&&this._config.container.appendChild(this.wordCountContainer)}destroy(){this._outputView&&(this._outputView.element.remove(),this._outputView.destroy()),super.destroy()}get wordCountContainer(){const t=this.editor,n=t.t,i=t.config.get("wordCount.displayWords"),r=t.config.get("wordCount.displayCharacters"),o=y.bind(this,this),u=[];return this._outputView||(this._outputView=new x,(i||i===void 0)&&(this.bind("_wordsLabel").to(this,"words",c=>n("Words: %0",c)),u.push({tag:"div",children:[{text:[o.to("_wordsLabel")]}],attributes:{class:"ck-word-count__words"}})),(r||r===void 0)&&(this.bind("_charactersLabel").to(this,"characters",c=>n("Characters: %0",c)),u.push({tag:"div",children:[{text:[o.to("_charactersLabel")]}],attributes:{class:"ck-word-count__characters"}})),this._outputView.setTemplate({tag:"div",attributes:{class:["ck","ck-word-count"]},children:u}),this._outputView.render()),this._outputView.element}_getText(){let t="";for(const n of this.editor.model.document.getRoots())t!==""&&(t+=`
`),t+=p(n);return t}_getCharacters(t){return t.replace(/\n/g,"").length}_getWords(t){return(t.match(this._wordsMatchRegExp)||[]).length}_refreshStats(){const t=this._getText(),n=this.words=this._getWords(t),i=this.characters=this._getCharacters(t);this.fire("update",{words:n,characters:i})}}export{E as WordCount,p as _modelElementToPlainText};
