import{__decorate as t}from"tslib";import{classMap as e}from"lit/directives/class-map.js";import{query as i,state as s,property as l,customElement as o}from"lit/decorators.js";import{H as r}from"../internal/slot.esm.js";import{html as a}from"lit";import{w as h}from"../internal/watch.esm.js";import{N as n}from"../internal/nile-element.esm.js";import{s as d}from"./nile-radio-group.css.esm.js";import{N as c}from"../internal/enum.esm.js";import"../internal/accessibility/a11y.state.enum.esm.js";import"../internal/accessibility/a11y.property.enum.esm.js";import"../internal/accessibility/role.enum.esm.js";let u=class extends n{constructor(){super(...arguments),this.hasSlotController=new r(this,"help-text","label"),this.customValidityMessage="",this.hasButtonGroup=!1,this.defaultValue="",this.label="",this.name="option",this.value="",this.form="",this.required=!1,this.labelInline=!1,this.disabled=!1,this.helpText="",this.errorMessage="",this.islabelborder=!1,this.allowUncheckGroup=!1}connectedCallback(){super.connectedCallback(),this.defaultValue=this.value,this.handleInitialDisabledState(),this.emit("nile-init")}disconnectedCallback(){super.disconnectedCallback(),this.emit("nile-destroy")}getAllRadios(){return[...this.querySelectorAll("nile-radio")]}handleRadioClick(t){const e=t.target.closest("nile-radio"),i=this.getAllRadios(),s=this.value;e.disabled||this.disabled||(this.value=e.value,this.allowUncheckGroup||i.forEach((t=>{t.checked=t===e})),this.allowUncheckGroup||this.value===s?this.allowUncheckGroup&&(this.emit("change",{value:this.value,checked:e.checked}),this.emit("input"),this.emit(c.NILE_CHANGE,{value:this.value,checked:e.checked}),this.emit(c.NILE_INPUT,{value:this.value,checked:e.checked})):(this.emit("change",{value:this.value}),this.emit("input"),this.emit(c.NILE_CHANGE,{value:this.value}),this.emit(c.NILE_INPUT,{value:this.value})))}updated(t){super.updated(t),t.has("allowUncheckGroup")&&this.allowUncheckGroup&&this.getAllRadios().forEach((t=>{t.allowUncheck=!0}))}handleDisabledChange(){this.getAllRadios().forEach((t=>{t.disabled=this.disabled}))}handleInitialDisabledState(){this.getAllRadios().forEach((t=>{t.disabled=t.hasAttribute("disabled")?t.disabled:this.disabled}))}handleKeyDown(t){if(this.disabled)return;if(!["ArrowUp","ArrowDown","ArrowLeft","ArrowRight"," "].includes(t.key))return;const e=this.getAllRadios().filter((t=>!t.disabled)),i=e.find((t=>t.checked))??e[0],s=" "===t.key?0:["ArrowUp","ArrowLeft"].includes(t.key)?-1:1,l=this.value;let o=e.indexOf(i)+s;o<0&&(o=e.length-1),o>e.length-1&&(o=0),this.getAllRadios().forEach((t=>{t.checked=!1,this.hasButtonGroup||(t.tabIndex=-1)})),this.value=e[o].value,e[o].checked=!0,this.hasButtonGroup?e[o].shadowRoot.querySelector("button").focus():(e[o].tabIndex=0,e[o].focus()),this.value!==l&&(this.emit("change"),this.emit("input"),this.emit(c.NILE_CHANGE,{value:this.value}),this.emit(c.NILE_INPUT,{value:this.value})),t.preventDefault()}handleLabelClick(){if(this.disabled)return;const t=this.getAllRadios(),e=t.find((t=>t.checked))||t[0];e&&e.focus()}handleSlotChange(){const t=this.getAllRadios();if(t.forEach((t=>{t.checked=t.value===this.value,this.islabelborder&&(t.islabelborder=!0)})),this.disabled)return void t.forEach((t=>t.tabIndex=-1));if(this.hasButtonGroup=t.some((t=>"nile-radio-button"===t.tagName.toLowerCase())),t.length>0&&!t.some((t=>t.checked)))if(this.hasButtonGroup){t[0].shadowRoot.querySelector("button").tabIndex=0}else t[0].tabIndex=0;this.hasButtonGroup&&this.shadowRoot?.querySelector("nile-button-group")}updateCheckedRadio(){this.getAllRadios().forEach((t=>t.checked=t.value===this.value))}handleValueChange(){this.hasUpdated&&this.updateCheckedRadio()}render(){const t=this.hasSlotController.test("label");this.hasSlotController.test("help-text");const i=this.hasSlotController.test("label-suffix"),s=!!this.label||!!t,l=!!this.helpText,o=!!this.errorMessage,r=a`
      <slot
        @click=${this.handleRadioClick}
        @keydown=${this.handleKeyDown}
        @slotchange=${this.handleSlotChange}
        role="presentation"
      ></slot>
    `;return a`
      <fieldset
        part="form-control"
        class=${e({"form-control":!0,"form-control--medium":!0,"form-control--radio-group":!0,"form-control--has-label":s,"form-control--has-help-text":l})}
        role="radiogroup"
        aria-labelledby="label"
        aria-describedby="help-text"
        aria-errormessage="error-message"
      >
        <label
          part="form-control-label"
          id="label"
          class="form-control__label"
          aria-hidden=${s?"false":"true"}
          @click=${this.handleLabelClick}
        >
          <slot name="label">${this.label}</slot>
        </label>

        ${i?a` <slot name="label-suffix"></slot> `:""}

        <div part="form-control-input" class="form-control-input">
          <div class="visually-hidden">
            <label class="radio-group__validation">
              <input
                type="text"
                class="radio-group__validation-input"
                ?required=${this.required}
                tabindex="-1"
                hidden
              />
            </label>
          </div>

          ${this.hasButtonGroup?a`
            ${r}
              `:a`
            <div part="options-base" class="${this.labelInline?"form-control--inline-radio":""}"> ${r}</div> `}
        </div>

        ${l?a`
                <nile-form-help-text>${this.helpText}</nile-form-help-text>
              `:""}

        ${o?a`
                <nile-form-error-message
                  >${this.errorMessage}</nile-form-error-message
                >
              `:""}

      </fieldset>
    `}};u.styles=d,t([i("slot:not([name])")],u.prototype,"defaultSlot",void 0),t([s()],u.prototype,"hasButtonGroup",void 0),t([s()],u.prototype,"defaultValue",void 0),t([l()],u.prototype,"label",void 0),t([l()],u.prototype,"name",void 0),t([l({reflect:!0})],u.prototype,"value",void 0),t([l({reflect:!0})],u.prototype,"form",void 0),t([l({type:Boolean,reflect:!0})],u.prototype,"required",void 0),t([l({type:Boolean,reflect:!0})],u.prototype,"labelInline",void 0),t([l({type:Boolean,reflect:!0})],u.prototype,"disabled",void 0),t([l({attribute:"help-text",reflect:!0})],u.prototype,"helpText",void 0),t([l({attribute:"error-message",reflect:!0})],u.prototype,"errorMessage",void 0),t([l({type:Boolean,reflect:!0})],u.prototype,"islabelborder",void 0),t([l({type:Boolean,reflect:!0,attribute:!0})],u.prototype,"allowUncheckGroup",void 0),t([h("disabled",{waitUntilFirstUpdate:!0})],u.prototype,"handleDisabledChange",null),t([h("value")],u.prototype,"handleValueChange",null),u=t([o("nile-radio-group")],u);export{u as N};
