/**
 * Copyright 2019 The Pennsylvania State University
 * @license Apache-2.0, see License.md for full text.
 */
import{LitElement as a}from"../../lit/index.js";import"../es-global-bridge/es-global-bridge.js";import"../super-daemon/lib/super-daemon-toast.js";class Hal9000 extends a{static get properties(){return{toast:{type:Boolean,reflect:!0},commands:{name:"commands",type:Object},respondsTo:{name:"respondsTo",type:String,attribute:"responds-to"},debug:{name:"debug",type:Boolean},auto:{name:"auto",type:Boolean,reflect:!0},enabled:{name:"enabled",type:Boolean,reflect:!0},pitch:{name:"pitch",type:Number,reflect:!0},rate:{name:"rate",type:Number,reflect:!0},language:{name:"language",type:String,reflect:!0}}}static get tag(){return"hal-9000"}constructor(){super(),this.toast=!1,this.windowControllers=new AbortController,this.commands={},this.respondsTo="(merlin)",this.debug=!1,this.pitch=.9,this.rate=.9,this.language=globalThis.navigator.language,globalThis.Hal9000=globalThis.Hal9000||{},globalThis.Hal9000.instance=this;const a=`${new URL("./lib/annyang/annyang.min.js",import.meta.url).href}`;globalThis.addEventListener("es-bridge-annyang-loaded",this._annyangLoaded.bind(this),{signal:this.windowControllers.signal}),globalThis.ESGlobalBridge.requestAvailability().load("annyang",a),void 0!==globalThis.speechSynthesis&&(globalThis.SpeechRecognition||globalThis.webkitSpeechRecognition||globalThis.mozSpeechRecognition||globalThis.msSpeechRecognition||globalThis.oSpeechRecognition)&&(this.synth=globalThis.speechSynthesis)}_commandsChanged(a){this.addCommands(a)}addCommands(a){if(this.annyang){if(this.annyang.removeCommands(),a["*anything"]){const t=a["*anything"];delete a["*anything"],a["*anything"]=t}this.annyang.addCommands(a)}}speak(a,t=!1,e=!0){return new Promise((n=>{this.__text=a,this.synth?(this.utter=new SpeechSynthesisUtterance(this.__text),this.utter.pitch=this.pitch,this.utter.rate=this.rate,this.utter.lang=this.language,globalThis.HAXCMS&&(globalThis.HAXCMS.instance.setCursor("hax:wizard-hat"),globalThis.HAXCMS.instance.setFavicon("hax:wizard-hat")),this.synth.speak(this.utter),this.toast&&this.setToast(a,t,e),this.utter.onend=a=>{globalThis.HAXCMS&&(globalThis.HAXCMS.instance.resetCursor(),globalThis.HAXCMS.instance.resetFavicon()),t||e||globalThis.dispatchEvent(new CustomEvent("super-daemon-toast-hide",{bubbles:!0,composed:!0,cancelable:!1,detail:!1})),n(a)}):n(!1)}))}setToast(a,t=!1,e=!0){globalThis.dispatchEvent(new CustomEvent("super-daemon-toast-show",{bubbles:!0,composed:!0,cancelable:!0,detail:{text:a,future:!0,merlin:!0,accentColor:"purple",duration:4e3,alwaysvisible:t,awaitingMerlinInput:e}}))}_annyangLoaded(){if(this.annyang=globalThis.annyang,this.annyang){this.annyang.addCommands(this.commands),this.annyang.debug(this.debug),this.auto?this.annyang.start({autoRestart:!0,continuous:!0}):this.enabled&&this.annyang.start();const a=new CustomEvent("hal-9000-online",{bubbles:!0,cancelable:!1,detail:!0});this.dispatchEvent(a)}}_respondsToChanged(a,t){this.annyang&&this.annyang.removeCommands();var e={};for(var n in this.commands)n.replace(t,a)!==n?e[n.replace(t,a)]=this.commands[n]:e[n]=this.commands[n];e.length>0&&(this.commands={...e})}_autoChanged(a){this.enabled=a}_enabledChanged(a){this.annyang&&(a?this.auto?this.annyang.start({autoRestart:!0,continuous:!0}):this.annyang.start():this.annyang.abort())}_debugChanged(a,t){this.annyang&&this.annyang.debug(a)}updated(a){a.forEach(((a,t)=>{"commands"==t&&void 0!==a&&this._commandsChanged(this[t]),"respondsTo"==t&&this._respondsToChanged(this[t],a),"debug"==t&&this._debugChanged(this[t],a),"auto"==t&&this._autoChanged(this[t],a),"enabled"==t&&this._enabledChanged(this[t],a)}))}disconnectedCallback(){this.windowControllers.abort(),super.disconnectedCallback()}}customElements.define(Hal9000.tag,Hal9000);export{Hal9000};globalThis.Hal9000=globalThis.Hal9000||{},globalThis.Hal9000.requestAvailability=()=>{if(!globalThis.Hal9000.instance){const a=globalThis.document.createElement("hal-9000");globalThis.document.body.appendChild(a),globalThis.Hal9000.instance=a}return globalThis.Hal9000.instance};export const HAL9000Instance=globalThis.Hal9000.requestAvailability();