/*!
 * KoliBri - The accessible HTML-Standard
 */
import{am as validateRequired,A as watchValidator,an as orientationOptions,ao as validateOptions,ap as mapString2Unknown,y as setState,aq as STATE_CHANGE_EVENT}from"./index2.js";import{I as InputController}from"./controller.js";const fillKeyOptionMap=(t,o,e="")=>{o.forEach(((o,i)=>{const a=`${e}-${i}`;"object"==typeof o&&null!==o&&"string"==typeof o.label&&o.label.length>0&&(Array.isArray(o.options)?fillKeyOptionMap(t,o.options,a):t.set(a,o))}))};class InputCheckboxRadioController extends InputController{constructor(t,o,e){super(t,o,e),this.component=t}validateRequired(t){validateRequired(this.component,t)}componentWillLoad(){super.componentWillLoad(),this.validateRequired(this.component._required)}}class InputRadioController extends InputCheckboxRadioController{constructor(t,o,e){super(t,o,e),this.keyOptionMap=new Map,this.getOptionByKey=t=>this.keyOptionMap.get(t),this.isValueInOptions=(t,o)=>void 0!==o.find((o=>o.value===t)),this.afterPatchOptions=(t,o,e,i)=>{"_value"===i&&this.setFormAssociatedValue(t)},this.beforePatchOptions=(t,o)=>{const e=o.has("_options")?o.get("_options"):this.component.state._options;if(Array.isArray(e)&&e.length>0){this.keyOptionMap.clear(),fillKeyOptionMap(this.keyOptionMap,e);const t=o.has("_value")?o.get("_value"):this.component.state._value;!1===this.isValueInOptions(t,e)&&(o.set("_value",e[0].value),this.onStateChange())}},this.component=t}validateOrientation(t){watchValidator(this.component,"_orientation",(t=>"string"==typeof t&&orientationOptions.includes(t)),new Set([`Orientation {${orientationOptions.join(", ")}`]),t,{defaultValue:"vertical"})}validateOptions(t){validateOptions(this.component,t,{hooks:{afterPatch:this.afterPatchOptions,beforePatch:this.beforePatchOptions}})}validateValue(t){t=mapString2Unknown(t),t=Array.isArray(t)?t[0]:t,setState(this.component,"_value",t,{afterPatch:this.afterPatchOptions,beforePatch:this.beforePatchOptions})}componentWillLoad(t){super.componentWillLoad(),this.onStateChange=()=>{if("function"==typeof t){const o=setTimeout((()=>{clearTimeout(o),t(STATE_CHANGE_EVENT)}))}},this.validateOrientation(this.component._orientation),this.validateOptions(this.component._options),this.validateValue(this.component._value)}}export{InputCheckboxRadioController as I,InputRadioController as a,fillKeyOptionMap as f};