/**
 * Copyright 2020 The Pennsylvania 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{getRange as r}from"../../utils/utils.js";import"../simple-autocomplete.js";export class SimpleAutocompleteTextTrigger extends t{constructor(){super(),this.windowControllers=new AbortController,this.haxUIElement=!0,this.target=null,this.triggers={},this.value=""}render(){return e`<simple-autocomplete
        @item-selected="${this.valueChanged}"
        selection-position
        hide-input
      ></simple-autocomplete
      ><slot></slot>`}static get tag(){return"simple-autocomplete-text-trigger"}static get properties(){return{target:{type:Object},triggers:{type:Object},value:{type:String}}}valueChanged(t){this.value=t.detail.value}updated(t){t.forEach(((t,e)=>{if("value"==e&&""!=this.value&&this.target){const t=this.getTargetValue();if(this.setTargetValue(t.substring(0,this._triggerStart-1)+this.value+t.substring(this._triggerEnd)),["TEXTAREA","INPUT"].includes(this.target.tagName))this.target.setSelectionRange(this._triggerStart-1+this.value.length,this._triggerStart-1+this.value.length);else if(null!=this.target.getAttribute("contenteditable")){var i=globalThis.document.createRange(),r=this.getSelection();i.setStart(this.target.childNodes[0],this._triggerStart-1+this.value.length),i.collapse(!0),r.removeAllRanges(),r.addRange(i)}this.target.focus(),this._triggerStart=null,this._triggerEnd=null}}))}connectTargetEvents(t=!0){t?(this.windowControllers=new AbortController,globalThis.addEventListener("keydown",this.targetKeyDownMonitor.bind(this),{signal:this.windowControllers.signal}),globalThis.addEventListener("keyup",this.targetKeyMonitor.bind(this),{signal:this.windowControllers.signal})):this.windowControllers.abort()}getSelection(){if(this.target){if(this.target.getSelection)return this.target.getSelection();if(r(this.target))return r(this.target)}return globalThis.getSelection()}getRange(){let t=this.getSelection();return t&&t.getRangeAt&&t.rangeCount?t.getRangeAt(0):t||!1}targetKeyDownMonitor(t){this.$autocomplete.a11yInputKeys(t)}targetKeyMonitor(t){if(Object.keys(this.triggers).includes(t.key)){let e=[];["TEXTAREA","INPUT"].includes(this.target.tagName)?this._triggerStart=this.target.selectionStart:null!=this.target.getAttribute("contenteditable")&&(this._triggerStart=this.getRange().startOffset),e="function"==typeof this.triggers[t.key]?this.triggers[t.key](this):this.triggers[t.key],this.$autocomplete.items=[...e]}this._triggerStart&&(["TEXTAREA","INPUT"].includes(this.target.tagName)?this._triggerEnd=this.target.selectionEnd:null!=this.target.getAttribute("contenteditable")&&(this._triggerEnd=this.getRange().endOffset)),this._triggerStart!=this._triggerEnd&&(this.$autocomplete.opened=!0,setTimeout((()=>{this.$autocomplete.setValue(this.getTargetValue().substring(this._triggerStart,this._triggerEnd))}),1)),"Space"===t.code&&(this._triggerStart=null,this._triggerEnd=null,this.$autocomplete.opened=!1)}setTargetValue(t){this.target&&(["TEXTAREA","INPUT"].includes(this.target.tagName)?this.target.value=t:null!=this.target.getAttribute("contenteditable")&&(this.target.innerText=t))}getTargetValue(){if(this.target){if(["TEXTAREA","INPUT"].includes(this.target.tagName))return this.target.value;if(null!=this.target.getAttribute("contenteditable"))return this.target.innerText}return!1}firstUpdated(t){super.firstUpdated&&super.firstUpdated(t),this.$autocomplete=this.shadowRoot.querySelector("simple-autocomplete"),!this.target&&this.children&&1===this.children.length&&(this.target=this.children[0])}connectedCallback(){super.connectedCallback(),this.connectTargetEvents()}disconnectedCallback(){this.connectTargetEvents(!1),super.disconnectedCallback()}}customElements.define(SimpleAutocompleteTextTrigger.tag,SimpleAutocompleteTextTrigger);