import{JoomlaEditor as c,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 f={},h=(o,e,i)=>{f[e.id]&&clearTimeout(f[e.id]),f[e.id]=setTimeout(()=>{o.remove(),c.unregister(e.id),Joomla.JoomlaTinyMCE.setupEditor(e,i)},500)};class k 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 i=!1;return e||this.instance.isHidden()?(this.instance.show(),i=!0):this.instance.hide(),i}}Joomla.JoomlaTinyMCE={setupEditors:o=>{const e=o||document,i=Joomla.getOptions("plg_editor_tinymce",{});e.querySelectorAll(".js-editor-tinymce").forEach(a=>{const t=a.querySelector("textarea"),r=a.querySelector(".js-tiny-toggler-button"),d=r?r.querySelector(".icon-eye"):!1;Joomla.JoomlaTinyMCE.setupEditor(t,i),r&&r.removeAttribute("disabled"),a.addEventListener("click",m=>{c.setActive(t.id);const s=m.target.closest(".js-tiny-toggler-button"),l=c.getActive();if(s&&l){const n=l.toggle();d&&d.setAttribute("class",n?"icon-eye":"icon-eye-slash")}})})},setupEditor:(o,e)=>{if(c.get(o.id))return;const i=o?o.getAttribute("name").replace(/\[\]|\]/g,"").split("[").pop():"default",u=e?e.tinyMCE||{}:{},a=u.default||{};let t=u[i]?u[i]: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,d=t.skin_dark;if(delete t.skin_light,delete t.skin_dark,t.skin=r,"colorSchemeOs"in document.documentElement.dataset){const n=window.matchMedia("(prefers-color-scheme: dark)");t.skin=n.matches?d:r,n.matches&&(t.content_css=t.content_css?`${t.content_css},dark`:"dark")}else document.documentElement.dataset.colorScheme==="dark"&&(t.skin=d,t.content_css=t.content_css?`${t.content_css},dark`:"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 s=new tinyMCE.Editor(o.id,t,tinymce.EditorManager),l=new k(s,"tinymce",o.id);if(!s.inline){let n=!1,g=!1;const y=()=>{s.getContentAreaContainer().querySelector("iframe").addEventListener("load",()=>{h(s,o,e)})};s.on("load",()=>{n=!0,g&&y()}),s.on("PostRender",()=>{g=!0,n&&y()})}s.on("focus",()=>{c.setActive(l)}),s.render(),c.register(l)}},document.addEventListener("DOMContentLoaded",()=>{Joomla.JoomlaTinyMCE.setupEditors(document)}),document.addEventListener("joomla:updated",({target:o})=>Joomla.JoomlaTinyMCE.setupEditors(o));
