import { Component, Host, h, Element, Watch, Event, Prop } from '@stencil/core';
/* note this is a variation of the ionic/stencil radio group specific to UXPL stylings */
export class UxplRadioGroup {
  constructor() {
    this.inputId = `${radioGroupIds++}-radio-group`;
    this.labelId = `${this.inputId}-lbl`;
    /**
    * If `true`, the radios can be deselected.
    */
    this.allowEmptySelection = false;
    this.setRadioTabIdx = (value) => {
      const radios = this.getRadios();
      // Get the first radio that is not disabled and the checked one
      const firstRad = radios.find(radio => !radio.disabled);
      const checkedRad = radios.find(radio => (radio.value === value && !radio.disabled));
      if (!firstRad && !checkedRad) {
        return;
      }
      // If an enabled checked radio exists, set it to be the focusable radio
      // otherwise we default to focus the first radio
      const focusable = checkedRad || firstRad;
      for (const radio of radios) {
        const tabindex = radio === focusable ? 0 : -1;
        radio.setButtonTabIdx(tabindex);
      }
    };
    this.onClick = (ev) => {
      const selectedRadio = ev.target && ev.target.closest('uxpl-radio');
      if (selectedRadio) {
        const currentValue = this.value;
        const newValue = selectedRadio.value;
        if (newValue !== currentValue) {
          this.value = newValue;
        }
        else if (this.allowEmptySelection) {
          this.value = undefined;
        }
      }
    };
  }
  valueChanged(value) {
    this.setRadioTabIdx(value);
    this.uxplChanged.emit({ value });
  }
  getRadios() {
    return Array.from(this.el.querySelectorAll('uxpl-radio'));
  }
  componentDidLoad() {
    this.setRadioTabIdx(this.value);
  }
  render() {
    return (h(Host, { role: "radiogroup", "aria-labelledby": this.labelId, onClick: this.onClick },
      h("slot", null)));
  }
  static get is() { return "uxpl-radio-group"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["uxpl-radio-group.css"]
  }; }
  static get styleUrls() { return {
    "$": ["uxpl-radio-group.css"]
  }; }
  static get properties() { return {
    "allowEmptySelection": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "If `true`, the radios can be deselected."
      },
      "attribute": "allow-empty-selection",
      "reflect": false,
      "defaultValue": "false"
    },
    "value": {
      "type": "any",
      "mutable": true,
      "complexType": {
        "original": "any | null",
        "resolved": "any",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": "set checked item"
      },
      "attribute": "value",
      "reflect": false
    }
  }; }
  static get events() { return [{
      "method": "uxplChanged",
      "name": "uxplChanged",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": "Output checked item"
      },
      "complexType": {
        "original": "UxplRadioGroupChangeEventDetail",
        "resolved": "UxplRadioGroupChangeEventDetail",
        "references": {
          "UxplRadioGroupChangeEventDetail": {
            "location": "import",
            "path": "./uxpl-radion-group-interface"
          }
        }
      }
    }]; }
  static get elementRef() { return "el"; }
  static get watchers() { return [{
      "propName": "value",
      "methodName": "valueChanged"
    }]; }
}
let radioGroupIds = 0;
