import{JoomlaEditor as c,JoomlaEditorDecorator as h}from"editor-api";/**
 * @copyright  (C) 2018 Open Source Matters, Inc. <https://www.joomla.org>
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 */const f={},y=(o,t,s)=>{f[t.id]&&clearTimeout(f[t.id]),f[t.id]=setTimeout(()=>{o.remove(),c.unregister(t.id),Joomla.JoomlaTinyMCE.setupEditor(t,s)},500)};class C extends h{getValue(){return this.instance.getContent()}setValue(t){return this.instance.setContent(t),this}getSelection(){return this.instance.selection.getContent({format:"text"})}replaceSelection(t){return this.instance.execCommand("mceInsertContent",!1,t),this}disable(t){return this.instance.setMode(t?"design":"readonly"),this}toggle(t){let s=!1;return t||this.instance.isHidden()?(this.instance.show(),s=!0):this.instance.hide(),s}}Joomla.JoomlaTinyMCE={setupEditors:o=>{const t=o||document,s=Joomla.getOptions("plg_editor_tinymce",{});t.querySelectorAll(".js-editor-tinymce").forEach(a=>{const e=a.querySelector("textarea"),r=a.querySelector(".js-tiny-toggler-button"),d=r?r.querySelector(".icon-eye"):!1;Joomla.JoomlaTinyMCE.setupEditor(e,s),r&&r.removeAttribute("disabled"),a.addEventListener("click",g=>{c.setActive(e.id);const i=g.target.closest(".js-tiny-toggler-button"),l=c.getActive();if(i&&l){const n=l.toggle();d&&d.setAttribute("class",n?"icon-eye":"icon-eye-slash")}})})},setupEditor:(o,t)=>{if(c.get(o.id))return;const s=o?o.getAttribute("name").replace(/\[\]|\]/g,"").split("[").pop():"default",m=t?t.tinyMCE||{}:{},a=m.default||{};let e=m[s]?m[s]:a;e.joomlaMergeDefaults?e=Joomla.extend(Joomla.extend({},a),e):e=Joomla.extend({},e),o&&(e.selector=null,e.target=o);const r=e.skin_light,d=e.skin_dark;if(delete e.skin_light,delete e.skin_dark,e.skin=r,"colorSchemeOs"in document.documentElement.dataset){const n=window.matchMedia("(prefers-color-scheme: dark)");e.skin=n.matches?d:r}else document.documentElement.dataset.colorScheme==="dark"&&(e.skin=d);let g=!1;o&&(g=o.readOnly),e.setup=n=>{n.mode.set(g?"readonly":"design")},e.init_instance_callback=n=>{n.on("submit",()=>{n.isHidden()&&n.show()},!0)};const i=new tinyMCE.Editor(o.id,e,tinymce.EditorManager),l=new C(i,"tinymce",o.id);if(!i.inline){let n=2;const E=()=>{if(n--,n)return;const u=i.getContentAreaContainer().querySelector("iframe");u.contentDocument.readyState!=="complete"?u.onload=()=>{u.onload=null,u.addEventListener("load",()=>y(i,o,t))}:u.addEventListener("load",()=>y(i,o,t))};i.on("load",E),i.on("init",E)}i.on("focus",()=>{c.setActive(l)}),i.render(),c.register(l)}},document.addEventListener("DOMContentLoaded",()=>{Joomla.JoomlaTinyMCE.setupEditors(document)}),document.addEventListener("joomla:updated",({target:o})=>Joomla.JoomlaTinyMCE.setupEditors(o));
