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 n}from"../internal/watch.esm.js";import{N as h}from"../internal/nile-element.esm.js";import{s as d}from"./nile-checkbox-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 m=class extends h{constructor(){super(...arguments),this.hasSlotController=new r(this,"help-text","label"),this.boundDocumentListener=null,this.defaultValue=[],this.label="",this.name="",this.value=[],this.for="",this.form="",this.required=!1,this.orientation="vertical",this.labelInline=!1,this.disabled=!1,this.helpText="",this.errorMessage="",this.bordered=!1,this.legend=""}get isHorizontal(){return"horizontal"===this.orientation||this.labelInline}connectedCallback(){super.connectedCallback(),this.defaultValue=[...this.value],this.setupDocumentListener(),this.emit(c.NILE_INIT)}disconnectedCallback(){super.disconnectedCallback(),this.teardownDocumentListener(),this.emit(c.NILE_DESTROY)}setupDocumentListener(){this.for&&(this.boundDocumentListener=t=>{const e=t.target;"nile-checkbox"===e.tagName?.toLowerCase()&&e.group===this.for&&this.handleCheckboxChange(e)},document.addEventListener(c.NILE_CHANGE,this.boundDocumentListener))}teardownDocumentListener(){this.boundDocumentListener&&(document.removeEventListener(c.NILE_CHANGE,this.boundDocumentListener),this.boundDocumentListener=null)}handleForChange(){this.teardownDocumentListener(),this.setupDocumentListener(),this.syncCheckboxStates(),this.enforceMaxDisabled()}getAllCheckboxes(){if(this.for){return[...this.getRootNode().querySelectorAll(`nile-checkbox[group="${this.for}"]`)]}return[...this.querySelectorAll("nile-checkbox")]}handleCheckboxClick(t){const e=t.target.closest("nile-checkbox");e&&this.handleCheckboxChange(e)}handleChildInput(t){t.stopPropagation()}handleCheckboxChange(t){if(t.disabled||this.disabled)return;const e=this.getAllCheckboxes(),i=e.filter((t=>t.checked)).length;if(void 0!==this.max&&i>this.max)return void(t.checked=!1);const s=e.filter((t=>t.checked)).map((t=>String(t.value))),l=this.value;this.value=s,this.enforceMaxDisabled(),JSON.stringify(l)!==JSON.stringify(s)&&this.emit(c.NILE_CHANGE,{value:this.value})}enforceMaxDisabled(){if(void 0===this.max)return;const t=this.getAllCheckboxes(),e=t.filter((t=>t.checked)).length>=this.max;t.forEach((t=>{t.checked||this.disabled||(t.disabled=e)}))}updated(t){super.updated(t),t.has("max")&&this.enforceMaxDisabled()}handleDisabledChange(){this.disabled?this.getAllCheckboxes().forEach((t=>{t.disabled=!0})):(this.getAllCheckboxes().forEach((t=>{t.disabled=t.hasAttribute("disabled")})),this.enforceMaxDisabled())}handleInitialDisabledState(){this.disabled?this.getAllCheckboxes().forEach((t=>{t.disabled=!0})):this.getAllCheckboxes().forEach((t=>{t.disabled=!!t.hasAttribute("disabled")}))}handleLabelClick(){if(this.disabled)return;const t=this.getAllCheckboxes()[0];t&&t.focus()}handleSlotChange(){this.getAllCheckboxes().forEach((t=>{t.checked=this.value.includes(String(t.value))})),this.handleInitialDisabledState(),this.enforceMaxDisabled()}syncCheckboxStates(){this.getAllCheckboxes().forEach((t=>{t.checked=this.value.includes(String(t.value))}))}handleValueChange(){this.hasUpdated&&(void 0!==this.max&&this.value.length>this.max&&(this.value=this.value.slice(0,this.max)),this.syncCheckboxStates(),this.enforceMaxDisabled())}render(){const t=this.hasSlotController.test("label"),i=this.hasSlotController.test("label-suffix"),s=!!this.label||!!t,l=!!this.helpText,o=!!this.errorMessage,r=a`
      <slot
        @click=${this.handleCheckboxClick}
        @input=${this.handleChildInput}
        @slotchange=${this.handleSlotChange}
        role="presentation"
      ></slot>
    `;return a`
      <fieldset
        part="form-control"
        class=${e({"form-control":!0,"form-control--medium":!0,"form-control--checkbox-group":!0,"form-control--has-label":s,"form-control--has-help-text":l,"form-control--bordered":this.bordered})}
        role="group"
        aria-labelledby="label"
        aria-describedby="help-text"
        aria-errormessage="error-message"
      >
        ${this.legend?a`<legend part="legend legend-label" class="form-control__legend">${this.legend}</legend>`:""}

        <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" part="validation-hidden">
            <label class="checkbox-group__validation" part="validation-label" >
              <input
                type="text"
                class="checkbox-group__validation-input"
                part="validation-input"
                ?required=${this.required}
                tabindex="-1"
                hidden
              />
            </label>
          </div>

          <div part="options-base" class="${this.isHorizontal?"form-control--inline-checkbox":""}">
            ${r}
          </div>
        </div>

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

${o?a`
      <nile-form-error-message id="error-message">${this.errorMessage}</nile-form-error-message>
            `:""}
      </fieldset>
    `}};m.styles=d,t([i("slot:not([name])")],m.prototype,"defaultSlot",void 0),t([s()],m.prototype,"defaultValue",void 0),t([l()],m.prototype,"label",void 0),t([l()],m.prototype,"name",void 0),t([l({type:Array})],m.prototype,"value",void 0),t([l({reflect:!0,attribute:!0})],m.prototype,"for",void 0),t([l({reflect:!0})],m.prototype,"form",void 0),t([l({type:Boolean,reflect:!0})],m.prototype,"required",void 0),t([l({reflect:!0})],m.prototype,"orientation",void 0),t([l({type:Boolean,reflect:!0})],m.prototype,"labelInline",void 0),t([l({type:Boolean,reflect:!0})],m.prototype,"disabled",void 0),t([l({attribute:!0,reflect:!0})],m.prototype,"helpText",void 0),t([l({attribute:!0,reflect:!0})],m.prototype,"errorMessage",void 0),t([l({type:Number,reflect:!0})],m.prototype,"max",void 0),t([l({type:Number,reflect:!0})],m.prototype,"min",void 0),t([l({type:Boolean,reflect:!0})],m.prototype,"bordered",void 0),t([l({type:String,reflect:!0})],m.prototype,"legend",void 0),t([n("for")],m.prototype,"handleForChange",null),t([n("disabled",{waitUntilFirstUpdate:!0})],m.prototype,"handleDisabledChange",null),t([n("value")],m.prototype,"handleValueChange",null),m=t([o("nile-checkbox-group")],m);export{m as N};
