/**
 * Copyright 2019 Penn State University
 * @license Apache-2.0, see License.md for full text.
 */
import{LitElement as t,html as e,css as i}from"../../../lit/index.js";import{RichTextEditorPromptButtonBehaviors as s}from"../../rich-text-editor/lib/buttons/rich-text-editor-prompt-button.js";import"../../hax-iconset/lib/simple-hax-iconset.js";class HaxTextEditorButton extends(s(t)){static get tag(){return"hax-text-editor-button"}static get properties(){return{...super.properties,element:{type:Object}}}constructor(){super(),this.tag=HaxTextEditorButton.tag,this.toggles=!0,this.command="insertHTML"}get targetedNode(){let t=this.__highlight.querySelectorAll(this.tagsList);return 1===t.length?t[0]:this.__highlight}firstUpdated(t){this.updateElement(),super.firstUpdated&&super.firstUpdated(t)}updated(t){super.updated(t),t.forEach(((t,e)=>{"element"===e&&this.element&&this.updateElement()}))}updateElement(){this.dispatchEvent(new CustomEvent("deregister-button",{bubbles:!0,cancelable:!0,composed:!0,detail:this}));let t=this.element||{},e=t.settings||{},i=t.gizmo||{};this.fields=[...(e.inline||e.configure||[]).map((t=>(""===t.slot&&(t.property="innerHTML"),t)))].filter((t=>"collapse"!==t.inputMethod)),this.tagsList=i.tag||"span",this.icon=i.icon||"add",this.label=i.title||i.tag,this.value=this.getValue(void 0),this.dispatchEvent(new CustomEvent("register-button",{bubbles:!0,cancelable:!0,composed:!0,detail:this}))}getValue(){let t=super.getValue();return this.fields.forEach((e=>{e.property&&"innerHTML"!==e.property&&(t[e.property]=this.targetedNode&&this.targetedNode.getAttribute?this.targetedNode.getAttribute(e.property):void 0),e.slot&&""!==e.slot&&this.targetedNode&&this.targetedNode.querySelector&&this.targetedNode.querySelector(`[slot=${e.slot}]`)&&this.targetedNode.querySelector(`[slot=${e.slot}]`).innerHTML,e.slot&&""===e.slot&&this.targetedNode&&[...this.targetedNode.children||[]].filter(":not([slot])")&&[...this.targetedNode.children||[]].filter(":not([slot])").length>0&&[...this.targetedNode.children||[]].filter(":not([slot])").join("\n")})),t}setToggled(){this.toggled=!!this.value}tagClickCallback(t={}){t.detail&&this.open(t.detail);let e=this.__highlight.innerHTML;this.__highlight.innerHTML="",this.__highlight.innerHTML=e}updateSelection(){let t=globalThis.document.createElement(this.tagsList),e="";this.fields.forEach((i=>{i.property&&"innerHTML"!==i.property&&"innerText"!==i.property&&(t[i.property]=this.value[i.property]),i.slot&&this.value[i.slot]&&(e+=`<${this.getSlotWrapper(i)}${Object.keys(i.slotAttributes||{}).map((t=>` ${t}="${i.slotAttributes[t]}"`))}>\n            ${this.value[slot]}\n          </${this.getSlotWrapper(i)}>`)})),this.value&&0!==Object.keys(this.value).length||(this.value={innerHTML:this.__highlight.innerHTML}),e+=this.value.innerHTML||"",this.__highlight.innerHTML="",this.__highlight.parentNode.insertBefore(t,this.__highlight),this.__highlight.unwrap(),t.innerHTML=e}getSlotWrapper(t){let e=t.slotWrapper,i=t.allowedSlotWrappers,s=t.excludedSlotWrappers||[],r=["span","div","p"].filter((t=>!s.includes(t)));return e||(i&&i[0]?i[0]:r)}}customElements.define(HaxTextEditorButton.tag,HaxTextEditorButton);export{HaxTextEditorButton};