import{Callback}from"Beckhoff.TwinCAT.HMI.Framework/index.esm.js";export class InputHints{__keysHost;__parentControl;__localization;__localizedElements=new Map;__localizationReader=void 0;__focusedElement=null;__mutationObserver=new MutationObserver(()=>{this.__update()});__onUpdate=new Callback.Collection;onUpdate=this.__onUpdate.getManipulators();__element=document.createElement("div");__min=new MinHint(this.__element,{add:this.__addLocalizedElement.bind(this),remove:this.__removeLocalizedElement.bind(this)});__max=new MaxHint(this.__element,{add:this.__addLocalizedElement.bind(this),remove:this.__removeLocalizedElement.bind(this)});__displayType=DisplayType.None;__alignment="Right";constructor(__keysHost,__parentControl){this.__keysHost=__keysHost,this.__parentControl=__parentControl,this.__element.classList.add("TcHmi_Controls_Beckhoff_TcHmiKeyboard-template-hints","hidden"),this.__localization=new TcHmi.Locale.ControlLocalization(this.__parentControl.getType());const destroyLocalization=this.__localization.watch(data=>{if(data.error!==TcHmi.Errors.NONE)return;if(!data.reader)return;this.__localizationReader=data.reader;for(const[element,info]of this.__localizedElements){let localizedText=data.reader.get(info.key);info.parameters&&(localizedText=tchmi_format_string(localizedText,...info.parameters)),element.textContent=tchmi_decode_control_characters(localizedText)}const separator=data.reader.get("InputHint_Separator");this.__element.style.setProperty("--input-hint-separator","InputHint_Separator"!==separator?`'${separator}'`:"''")});TcHmi.EventProvider.register(this.__parentControl.getId()+".onDestroyed",event=>{event.destroy(),destroyLocalization(),this.__mutationObserver.disconnect(),this.__focusedElement=null})}update(focusedElement){this.__focusedElement!==focusedElement?(this.__focusedElement&&this.__mutationObserver.disconnect(),this.__focusedElement=focusedElement,focusedElement?(this.__mutationObserver.observe(focusedElement,{attributes:!0,attributeFilter:["data-tchmikeyboard-min","data-tchmikeyboard-max","min","max"]}),this.__update()):this.hide()):this.__show()}__update(){if(!this.__focusedElement)return;let min=null,max=null;const minAttribute=this.__focusedElement.getAttribute("data-tchmikeyboard-min")??this.__focusedElement.getAttribute("min"),maxAttribute=this.__focusedElement.getAttribute("data-tchmikeyboard-max")??this.__focusedElement.getAttribute("max");null!==minAttribute&&""!==minAttribute.trim()&&(min=Number(minAttribute),Number.isNaN(min)&&(min=null)),null!==maxAttribute&&""!==maxAttribute.trim()&&(max=Number(maxAttribute),Number.isNaN(max)&&(max=null)),this.__min.displayValue=null!==min?minAttribute:null,this.__min.value=min,this.__max.displayValue=null!==max?maxAttribute:null,this.__max.value=max,this.__onUpdate.trigger(),null!==min||null!==max?this.__show():this.hide()}__show(){this.__displayType!==DisplayType.None&&(this.__element.classList.remove("hidden"),this.__element.isConnected||this.__keysHost.appendChild(this.__element))}hide(){this.__element.classList.add("hidden")}validate(currentInput){let numericValue=Number(currentInput);0!==numericValue||currentInput.trim()||(numericValue=NaN);const minValid=this.__min.validate(numericValue),maxValid=this.__max.validate(numericValue);return minValid&&maxValid}resetValidation(){this.__min.resetValidation(),this.__max.resetValidation()}getElement(){return this.__element}setDisplayType(displayType){switch(this.__displayType=displayType,displayType){case DisplayType.None:this.hide();break;case DisplayType.TextboxOverlay:this.__element.classList.add("textbox-overlay");break;case DisplayType.LayoutDefined:this.__element.classList.remove("textbox-overlay")}}getDisplayType(){return this.__displayType}setAlignment(alignment){this.__alignment=alignment,this.__element.classList.toggle("left-aligned","Left"===this.__alignment),this.__element.classList.toggle("right-aligned","Right"===this.__alignment)}getAlignment(){return this.__alignment}__addLocalizedElement(element,key,...parameters){const info={key};if(0!==parameters.length&&(info.parameters=parameters),this.__localizedElements.set(element,info),this.__localizationReader){let localizedText=this.__localizationReader.get(key);info.parameters&&(localizedText=tchmi_format_string(localizedText,...info.parameters)),element.textContent=tchmi_decode_control_characters(localizedText)}}__removeLocalizedElement(element){this.__localizedElements.delete(element)}}export var DisplayType;!function(DisplayType){DisplayType[DisplayType.None=0]="None",DisplayType[DisplayType.TextboxOverlay=1]="TextboxOverlay",DisplayType[DisplayType.LayoutDefined=2]="LayoutDefined"}(DisplayType||(DisplayType={}));class InputHint{__parent;__value=null;__displayValue=null;__elementLabel=document.createElement("span");__elementValue=document.createElement("span");constructor(__parent){this.__parent=__parent}set value(value){this.__value=value,this.resetValidation(),this.__updateHint()}get value(){return this.__value}set displayValue(value){this.__displayValue=value,this.__updateHint()}get displayValue(){return this.__displayValue}__updateHint(){null!==this.__value?(this.__elementValue.textContent=this.__displayValue??this.__value.toString(10),this.show()):(this.__elementValue.textContent="",this.hide())}hide(){(this.__elementLabel.isConnected||this.__elementValue.isConnected)&&(this.__elementLabel.remove(),this.__elementValue.remove())}validate(value){const isValid=null===this.__value||!Number.isNaN(value)&&this.__isValidValue(value);return this.__setIsViolated(!isValid),isValid}resetValidation(){this.__setIsViolated(!1)}__setIsViolated(violated){this.__elementLabel.classList.toggle("violated",violated),this.__elementValue.classList.toggle("violated",violated)}}class MinHint extends InputHint{constructor(parent,localizer){super(parent),this.__elementLabel.classList.add("min","label"),this.__elementValue.classList.add("min","value"),localizer.add(this.__elementLabel,"InputHint_Min")}show(){this.__elementLabel.isConnected&&this.__elementValue.isConnected||(this.__parent.insertAdjacentElement("afterbegin",this.__elementValue),this.__parent.insertAdjacentElement("afterbegin",this.__elementLabel))}__isValidValue(value){return null===this.__value||value>=this.__value}}class MaxHint extends InputHint{constructor(parent,localizer){super(parent),this.__elementLabel.classList.add("max","label"),this.__elementValue.classList.add("max","value"),localizer.add(this.__elementLabel,"InputHint_Max")}show(){this.__elementLabel.isConnected&&this.__elementValue.isConnected||(this.__parent.insertAdjacentElement("beforeend",this.__elementLabel),this.__parent.insertAdjacentElement("beforeend",this.__elementValue))}__isValidValue(value){return null===this.__value||value<=this.__value}}