var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { Component, h, Element, Prop, Event, Watch, Host } from '@stencil/core';
import { RequiredFieldRule } from '../../../helpers/validator/rules/required-field-rule';
import { sanitizeVegaComponent } from '../../../utils/component';
import { ComponentLoadRequired } from '../../../decorators/method-decorators/component-load-required';
import { CHECKBOX_GROUP_DISABLED_CHANGE, CHECKBOX_VALUE_CHANGE, FORM_ELEMENT_VALUE_CHANGE, } from '../../../helpers/change-manager/subject/static-subject/static-subject-title';
import ChangeManager from '../../../helpers/change-manager/change-manager';
import { InjectVegaGlobalSlimmer, InjectVegaSlimmer, methodPlaceholder, } from 'vega-slimmer/core';
import { FormFieldControllerSlimmer } from '../../../helpers/slimmers/form-field-controller-slimmer';
import { ChildNodesNotifyObserverSlimmer, } from '../../../helpers/event-manager/slimmers/child-nodes-notify-observer-slimmer';
import { ChildNodesEventPreventSlimmer } from '../../../helpers/event-manager/slimmers/child-nodes-event-prevent-slimmer';
import { VegaChange } from '../../../helpers/event-manager/event-id/vega-event-id';
import { createEventEmitSlimmer, } from '../../../helpers/event-manager/slimmers/event-emit-slimmer';
import { VegaComponentUsageRuntimeMetricsSlimmer } from '../../../helpers/slimmers/component-usage-runtime-metrics';
/**
 * @vegaVersion 1.0.7
 */
export class VegaCheckboxGroup {
  constructor() {
    this.globalSlimmers = {};
    this.formFieldController = new FormFieldControllerSlimmer({
      propertyRules: [
        {
          propertyName: 'required',
          ruleFactory: (isRequired) => (isRequired ? new RequiredFieldRule() : null),
        },
      ],
      defaultValue: [],
      validationRulesName: 'validationRules',
      attributeGetter: (propertyName) => this[propertyName],
      inputContainerGetter: () => this.inputContainerRef,
      touchableAreaGetter: () => this.touchableAreaGetter(),
    });
    this.checkboxValueChangeObserver = new ChildNodesNotifyObserverSlimmer(CHECKBOX_VALUE_CHANGE, (input) => this.childCheckboxValueChange(input));
    this.checkboxEventPrevent = new ChildNodesEventPreventSlimmer([VegaChange], () => this.vegaFlexElement);
    this.changeEventEmitter = createEventEmitSlimmer(VegaCheckboxGroup, VegaChange);
    this.vegaComponentUsageRuntimeMetricsSlimmer = new VegaComponentUsageRuntimeMetricsSlimmer();
    /**
     * Specifies the label or title for the checkbox group.
     *    It provides descriptive text that helps identify
     *    the purpose of the checkbox group.
     *
     * @vegaVersion 1.3.0
     */
    this.label = '';
    /**
     * Stores the selected values of the nested checkboxes
     *    within the checkbox group.
     *
     * @vegaVersion 1.0.7
     */
    /* eslint-disable-next-line @stencil/strict-mutable */
    this.value = [];
    /**
     * Specifies all properties of a `vega-flex` component,
     *    accessible as an object.
     *
     * @vegaVersion 1.3.0
     */
    this.vegaFlexProp = {};
    /**
     * Indicates if the checkbox group is disabled.
     *
     * @vegaVersion 1.3.0
     */
    this.disabled = false;
    /**
     * Indicates whether at least one checkbox in the
     *    checkbox group must be checked to pass
     *    form validation.
     *
     * @vegaVersion 1.3.0
     */
    this.required = false;
    /**
     * Provides a supplementary prompt message that
     *    assists users in selecting the appropriate
     *    value for the checkbox group.
     *
     * @vegaVersion 1.3.0
     */
    this.hint = '';
    /** Specifies the validation status of the
     *    checkbox group component.
     *
     * @vegaVersion 1.3.0
     */
    /* eslint-disable-next-line @stencil/strict-mutable */
    this.isValid = null;
    /**
     * Determines whether automatic validation should be implemented for
     *    the checkbox group upon selection.
     *
     * @vegaVersion 2.6.0
     */
    this.autoValidation = true;
    /**
     * Defines custom validation rules for the checkbox group.
     *
     * @vegaVersion 2.6.0
     */
    this.validationRules = [];
    /**
     * Update checkbox group value when child checkbox is changed.
     *
     * @param {NotifyObserverPayload<boolean>} input change payload from child checkbox
     */
    this.childCheckboxValueChange = (input) => {
      this.syncCheckboxItemsValue(input.detail, input.host.identifier);
    };
    this.touchableAreaGetter = () => {
      const inputArray = [];
      this.vegaCheckboxArray &&
        this.vegaCheckboxArray.forEach(async (vegaCheckbox) => {
          inputArray.push(vegaCheckbox.shadowRoot.querySelector('input'));
        });
      return inputArray;
    };
  }
  async watchValue() {
    this.initCheckboxArray();
    this.changeEventEmitter.emit(this.value);
    ChangeManager.notify(FORM_ELEMENT_VALUE_CHANGE, {
      host: this.host,
      detail: this.value,
    });
  }
  watchVegaFlexProp() {
    this.setVegaFlexProperty();
  }
  async watchDisabled() {
    this.notifyGroupDisabledChange();
  }
  watchValidationRules() {
    methodPlaceholder();
  }
  componentDidLoad() {
    this.setVegaFlexProperty();
    this.initCheckboxArray();
    this.notifyGroupDisabledChange();
  }
  /**
   * The below method is e2e-test covered in
   * @see{module:vega-checkbox-group-dark-change-background-color }
   */
  /* istanbul ignore next */
  render() {
    return sanitizeVegaComponent(h(Host, null,
      this.renderLabelDom(),
      h("div", { class: "vega-checkbox-group-container", ref: (e) => (this.inputContainerRef = e) },
        h("vega-flex", { ref: (el) => {
            this.vegaFlexElement = el;
          } },
          h("slot", null))),
      this.renderHintDom()), this.host);
  }
  renderLabelDom() {
    return (this.label && (h("div", { class: "vega-label" },
      h("label", { "aria-label": this.label },
        this.label,
        this.renderRequireDom()))));
  }
  renderRequireDom() {
    return this.required ? h("span", { class: "vega-require" }, "*") : null;
  }
  renderHintDom() {
    return (this.hint && (h("div", { class: "vega-hint" },
      h("label", null, this.hint))));
  }
  setVegaFlexProperty() {
    if (this.vegaFlexProp) {
      Object.assign(this.vegaFlexElement, {
        gap: 'size-16',
        direction: 'col',
      }, this.vegaFlexProp);
    }
  }
  /**
   * 1. If the checkbox is checked, the value of checkbox group doesn't contain the value of the selected checkbox and needs to be added
   * 2. If the checkbox isn't checked, the value of checkbox group contains the value of the selected checkbox and needs to be deleted
   * @see{module:vega-checkbox-group-with-listen-vega-change-event-e2e}
   */
  /* istanbul ignore next */
  syncCheckboxItemsValue(checked, checkboxValue) {
    if (checked && !this.value.includes(checkboxValue)) {
      this.value = [...this.value, checkboxValue];
    }
    else if (!checked && this.value.includes(checkboxValue)) {
      this.value = [...this.value.filter((value) => value !== checkboxValue)];
    }
  }
  initCheckboxArray() {
    this.vegaCheckboxArray = Array.from(this.host.querySelectorAll('vega-checkbox'));
    this.vegaCheckboxArray.forEach((vegaCheckbox) => {
      if (this.value && this.value.length > 0) {
        vegaCheckbox.checked = this.value.includes(vegaCheckbox.identifier);
      }
      else {
        vegaCheckbox.checked = false;
      }
    });
  }
  /**
   * This function notifies a change in the disabled state of a checkbox group component.
   */
  notifyGroupDisabledChange() {
    ChangeManager.notify(CHECKBOX_GROUP_DISABLED_CHANGE, { host: this.host });
  }
  static get is() { return "vega-checkbox-group"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["vega-checkbox-group.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["vega-checkbox-group.css"]
  }; }
  static get properties() { return {
    "label": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.3.0"
          }],
        "text": "Specifies the label or title for the checkbox group.\n   It provides descriptive text that helps identify\n   the purpose of the checkbox group."
      },
      "attribute": "label",
      "reflect": false,
      "defaultValue": "''"
    },
    "value": {
      "type": "unknown",
      "mutable": true,
      "complexType": {
        "original": "string[]",
        "resolved": "string[]",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.0.7"
          }],
        "text": "Stores the selected values of the nested checkboxes\n   within the checkbox group."
      },
      "defaultValue": "[]"
    },
    "vegaFlexProp": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "VegaFlexProps",
        "resolved": "{ direction?: ResponsiveType<\"row\" | \"col\" | \"column\" | \"row-reverse\" | \"col-reverse\" | \"column-reverse\">; gap?: ResponsiveType<GapType>; alignItems?: ResponsiveType<FlexAlignItemType>; justifyContent?: ResponsiveType<FlexJustifyContentType>; margin?: ResponsiveType<MarginType>; }",
        "references": {
          "VegaFlexProps": {
            "location": "import",
            "path": "../../../types/ui.type"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.3.0"
          }],
        "text": "Specifies all properties of a `vega-flex` component,\n   accessible as an object."
      },
      "defaultValue": "{}"
    },
    "disabled": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.3.0"
          }],
        "text": "Indicates if the checkbox group is disabled."
      },
      "attribute": "disabled",
      "reflect": true,
      "defaultValue": "false"
    },
    "required": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.3.0"
          }],
        "text": "Indicates whether at least one checkbox in the\n   checkbox group must be checked to pass\n   form validation."
      },
      "attribute": "required",
      "reflect": true,
      "defaultValue": "false"
    },
    "hint": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.3.0"
          }],
        "text": "Provides a supplementary prompt message that\n   assists users in selecting the appropriate\n   value for the checkbox group."
      },
      "attribute": "hint",
      "reflect": false,
      "defaultValue": "''"
    },
    "isValid": {
      "type": "boolean",
      "mutable": true,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.3.0"
          }],
        "text": "Specifies the validation status of the\n  checkbox group component."
      },
      "attribute": "is-valid",
      "reflect": true,
      "defaultValue": "null"
    },
    "autoValidation": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "2.6.0"
          }],
        "text": "Determines whether automatic validation should be implemented for\n   the checkbox group upon selection."
      },
      "attribute": "auto-validation",
      "reflect": true,
      "defaultValue": "true"
    },
    "validationRules": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "FormFieldValidationRule<unknown>[]",
        "resolved": "FormFieldValidationRule<unknown>[]",
        "references": {
          "FormFieldValidationRule": {
            "location": "import",
            "path": "../../../helpers/validator/rules/form-field-validation-rule"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "2.6.0"
          }],
        "text": "Defines custom validation rules for the checkbox group."
      },
      "defaultValue": "[]"
    }
  }; }
  static get events() { return [{
      "method": "vegaChange",
      "name": "vegaChange",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.0.10"
          }],
        "text": "An event emitter notifying changes in the value\n  of any nested checkbox within the checkbox group,\n  whether triggered programmatically or through\n  user interaction."
      },
      "complexType": {
        "original": "string[]",
        "resolved": "string[]",
        "references": {}
      }
    }, {
      "method": "change",
      "name": "change",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "eventSemantics",
            "text": "namespace:native"
          }, {
            "name": "vegaVersion",
            "text": "2.0.0"
          }],
        "text": "An event emitter notifying changes in the value\n  of any nested checkbox within the checkbox group,\n  whether triggered programmatically or through\n  user interaction."
      },
      "complexType": {
        "original": "string[]",
        "resolved": "string[]",
        "references": {}
      }
    }]; }
  static get elementRef() { return "host"; }
  static get watchers() { return [{
      "propName": "value",
      "methodName": "watchValue"
    }, {
      "propName": "vegaFlexProp",
      "methodName": "watchVegaFlexProp"
    }, {
      "propName": "disabled",
      "methodName": "watchDisabled"
    }, {
      "propName": "validationRules",
      "methodName": "watchValidationRules"
    }]; }
}
__decorate([
  InjectVegaGlobalSlimmer()
], VegaCheckboxGroup.prototype, "globalSlimmers", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaCheckboxGroup.prototype, "formFieldController", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaCheckboxGroup.prototype, "checkboxValueChangeObserver", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaCheckboxGroup.prototype, "checkboxEventPrevent", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaCheckboxGroup.prototype, "changeEventEmitter", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaCheckboxGroup.prototype, "vegaComponentUsageRuntimeMetricsSlimmer", void 0);
__decorate([
  ComponentLoadRequired()
], VegaCheckboxGroup.prototype, "watchVegaFlexProp", null);
