import{JoomlaEditor as d,JoomlaEditorDecorator as E}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 g={},h=(o,e,s)=>{g[e.id]&&clearTimeout(g[e.id]),g[e.id]=setTimeout(()=>{o.remove(),d.unregister(e.id),Joomla.JoomlaTinyMCE.setupEditor(e,s)},500)};class p extends E{getValue(){return this.instance.getContent()}setValue(e){return this.instance.setContent(e),this}getSelection(){return this.instance.selection.getContent({format:"text"})}replaceSelection(e){return this.instance.execCommand("mceInsertContent",!1,e),this}disable(e){return this.instance.setMode(e?"design":"readonly"),this}toggle(e){let s=!1;return e||this.instance.isHidden()?(this.instance.show(),s=!0):this.instance.hide(),s}}Joomla.JoomlaTinyMCE={setupEditors:o=>{const e=o||document,s=Joomla.getOptions("plg_editor_tinymce",{});e.querySelectorAll(".js-editor-tinymce").forEach(a=>{const t=a.querySelector("textarea"),r=a.querySelector(".js-tiny-toggler-button"),u=r?r.querySelector(".icon-eye"):!1;Joomla.JoomlaTinyMCE.setupEditor(t,s),r&&r.removeAttribute("disabled"),a.addEventListener("click",m=>{d.setActive(t.id);const i=m.target.closest(".js-tiny-toggler-button"),c=d.getActive();if(i&&c){const n=c.toggle();u&&u.setAttribute("class",n?"icon-eye":"icon-eye-slash")}})})},setupEditor:(o,e)=>{if(d.get(o.id))return;const s=o?o.getAttribute("name").replace(/\[\]|\]/g,"").split("[").pop():"default",l=e?e.tinyMCE||{}:{},a=l.default||{};let t=l[s]?l[s]:a;t.joomlaMergeDefaults?t=Joomla.extend(Joomla.extend({},a),t):t=Joomla.extend({},t),o&&(t.selector=null,t.target=o);const r=t.skin_light,u=t.skin_dark;"colorSchemeOs"in document.body.dataset?t.skin=window.matchMedia("(prefers-color-scheme: dark)").matches?u:r:t.skin=r,delete t.skin_light,delete t.skin_dark;let m=!1;o&&(m=o.readOnly),t.setup=n=>{n.mode.set(m?"readonly":"design")},t.init_instance_callback=n=>{n.on("submit",()=>{n.isHidden()&&n.show()},!0)};const i=new tinyMCE.Editor(o.id,t,tinymce.EditorManager),c=new p(i,"tinymce",o.id);if(!i.inline){let n=!1,f=!1;const y=()=>{i.getContentAreaContainer().querySelector("iframe").addEventListener("load",()=>{h(i,o,e)})};i.on("load",()=>{n=!0,f&&y()}),i.on("PostRender",()=>{f=!0,n&&y()})}i.on("focus",()=>{d.setActive(c)}),i.render(),d.register(c)}},document.addEventListener("DOMContentLoaded",()=>{Joomla.JoomlaTinyMCE.setupEditors(document)}),document.addEventListener("joomla:updated",({target:o})=>Joomla.JoomlaTinyMCE.setupEditors(o));
