/**
 * Copyright 2018 The Pennsylvania State University
 * @license Apache-2.0, see License.md for full text.
 */
const RadioBehaviors=function(e){return class extends e{render(){return""}static get properties(){return{...super.properties,itemData:{type:Array},selection:{type:String,attribute:"selection"}}}constructor(){super(),this.addEventListener(this.__selectEvent,this._handleSelectItem)}connectedCallback(){super.connectedCallback(),this.__observer.observe(this,{attributes:!0,childList:!0,subtree:!0})}disconnectedCallback(){this.__observer&&this.__observer.disconnect&&this.__observer.disconnect(),this.removeEventListener(this.__selectEvent,this._handleSelectItem),super.disconnectedCallback()}firstUpdated(e){super.firstUpdated&&super.firstUpdated(e),this.selectItem(this.selection)}updated(e){super.updated&&super.updated(e),e.forEach(((e,t)=>{"selection"===t&&this.selection!==e&&this.selectItem(this.selection)}))}get selectedIndex(){let e=this.itemData.filter((e=>e.id===this.selection));return e&&e[0]?e[0].index:0}get __observer(){return new MutationObserver(((e,t)=>this._handleItemChange(e,t)))}get __allowNull(){return!1}get __query(){return"> item"}get __selected(){return"selected"}get __selectEvent(){return"select-item"}selectItem(e){e="string"==typeof e&&e.trim().length>0?this._getItemById(e):"integer"==typeof e?this._getItemByIndex(e):e,this.__allowNull||e&&!e.disabled||(e=this.selection&&this._getItemByQuery(`#${this.selection}`)?this._getItemByQuery(`#${this.selection}`):this.__selected&&this._getItemByQuery(`[${this.__selected}]`)?this._getItemByQuery(`[${this.__selected}]`):this._getItemByQuery()),e&&!this._isItemSelected(e)?(e.id=e.id||this._generateUUID(),this.selection=e.id,this._handleSelectionChange()):!e&&this.__allowNull&&this.selection&&(this.selection=void 0,this._handleSelectionChange()),this._updateItemData()}_getDataFromItems(){let e=this.querySelectorAll(`${this.__query}`);return Object.keys(e||{}).map((t=>this._getDataFromItem(e[t],t,this.__selected)))}_getDataFromItem(e,t,s){return(!e.id||e.id.trim().length<1)&&(e.id=this._generateUUID()),{id:e.id,index:parseInt(t),node:e,innerHTML:e.innerHTML,selected:e.getAttribute(s)}}_generateUUID(){return"item-ss-s-s-s-sss".replace(/s/g,Math.floor(65536*(1+Math.random())).toString(16).substring(1))}_getItemById(e){return this._getItemByQuery(`#${e}`)}_getItemByIndex(e){let t=this.querySelectorAll(this.__query);return t&&t[e]&&!t[e].disabled?t[e]:void 0}_getItemByQuery(e=""){let t=this.querySelector(`${this.__query}${e}`);return t&&!t.disabled?t:void 0}_handleItemChange(e,t){let s=!1;e.forEach((e=>{let t="childList"===e.type&&e.addedNodes.length>0,i="childList"===e.type&&e.removedNodes.length>0&&Object.keys(e.removedNodes||{}).filter((t=>e.removedNodes[t].id===this.selection)).length>0,r="attributes"===e.type&&"id"===e.attributeName;s=s||t||i||r})),s&&(this.querySelectorAll(`${this.__query}`).forEach((e=>{e.id||(e.id=this._generateUUID())})),this.selectItem(this.selection)),this._updateItemData()}_handleSelectItem(e){e.stopPropagation&&e.stopPropagation(),this.selectItem(e.detail.controls)}_handleSelectionChange(){this.querySelectorAll(`${this.__query}`).forEach((e=>this._setItemSelected(e))),this.dispatchEvent(new CustomEvent("selection-changed",{bubbles:!1,cancelable:!0,composed:!0,detail:this}))}_isItemSelected(e){return e&&(e.id&&e.id===this.selection||e.hasAttribute(this.__selected))}_setItemSelected(e){e.id!==this.selection?e.removeAttribute(this.__selected):e.setAttribute(this.__selected,!0)}_updateItemData(){this.itemData=this._getDataFromItems(),this.render&&this.render()}}};export{RadioBehaviors};