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, Host, h, Element, Prop, Event, Watch, State } from '@stencil/core';
import { createEnterKeyHandlerToTriggerClick } from '../../utils/accessibility';
import { sanitizeVegaComponent } from '../../utils/component';
import ChangeManager from '../../helpers/change-manager/change-manager';
import { RADIO_GROUP_DISABLED_CHANGE, RADIO_VALUE_CHANGE, } from '../../helpers/change-manager/subject/static-subject/static-subject-title';
import { Observer } from '../../helpers/change-manager/observer/observer';
import { findParent } from '../../utils/ui';
import { InjectVegaGlobalSlimmer, InjectVegaSlimmer } from 'vega-slimmer/core';
import { createEventEmitSlimmer, } from '../../helpers/event-manager/slimmers/event-emit-slimmer';
import { VegaBlur, VegaChange, VegaFocus } from '../../helpers/event-manager/event-id/vega-event-id';
import { VegaRadioShowErrorController } from './slimmers/controllers/vega-radio-show-error-controller';
import { VegaComponentUsageRuntimeMetricsSlimmer } from '../../helpers/slimmers/component-usage-runtime-metrics';
/**
 * @vegaVersion 1.0.7
 */
export class VegaRadio {
  constructor() {
    this.globalSlimmers = {};
    this.changeEventEmitter = createEventEmitSlimmer(VegaRadio, VegaChange);
    this.focusEventEmitter = createEventEmitSlimmer(VegaRadio, VegaFocus);
    this.blurEventEmitter = createEventEmitSlimmer(VegaRadio, VegaBlur);
    this.vegaRadioShowErrorController = new VegaRadioShowErrorController();
    this.vegaComponentUsageRuntimeMetricsSlimmer = new VegaComponentUsageRuntimeMetricsSlimmer();
    /**
     * Indicates whether the parent radio group is disabled.
     */
    this.groupDisabled = false;
    /**
     * Indicates whether the parent radio group show error message.
     */
    this.showError = false;
    /**
     * Represents the value associated with the radio button
     *    that is currently selected or checked.
     *
     * @vegaVersion 1.0.7
     */
    this.value = '';
    /**
     * Determines if the radio button is selected or checked.
     *
     * @vegaVersion 1.0.7
     */
    /* eslint-disable-next-line @stencil/strict-mutable */
    this.checked = false;
    /**
     * Determines if the radio button is disabled.
     *
     * @vegaVersion 1.3.0
     */
    this.disabled = false;
    /**
     * Specifies the size of the Radio Button component.
     *
     * The size should be selected based on the context and
     *    design requirements of the specific form.
     *
     * @vegaVersion 1.9.2
     */
    this.size = 'default';
    /**
     * The below method is e2e-test covered in
     * @see{module:vega-radio-change-e2e}
     */
    /* istanbul ignore next */
    this.updateValue = (e) => {
      const currentTarget = e.currentTarget;
      this.checked = currentTarget.checked;
      e.stopPropagation();
    };
    /**
     * The below method is e2e-test covered in
     * @see{module:vega-radio-focus-e2e}
     */
    /* istanbul ignore next */
    this.handleFocus = (e) => {
      this.focusEventEmitter.emit(e, e);
    };
    /**
     * The below method is e2e-test covered in
     * @see{module:vega-radio-blur-e2e}
     */
    /* istanbul ignore next */
    this.handleBlur = (e) => {
      this.blurEventEmitter.emit(e, e);
    };
    this.handleClick = (e) => {
      e.stopPropagation();
    };
  }
  watchCheckedChange() {
    this.changeEventEmitter.emit(this.checked);
    ChangeManager.notify(RADIO_VALUE_CHANGE, { host: this.host, detail: this.checked });
  }
  connectedCallback() {
    this.ariaLabel = this.host.textContent;
    if (this.ariaLabel == '') {
      this.ariaLabel = this.value;
    }
  }
  disconnectedCallback() {
    this.unRegisterGroupDisabledObserver();
  }
  componentDidLoad() {
    if (this.host.textContent === '') {
      this.labelElement.classList.add('vega-hidden');
    }
    this.initAndRegisterGroupDisabledObserver();
  }
  render() {
    var _a;
    /* istanbul ignore next */
    const isInsideRadioGroup = ((_a = this.host.parentElement) === null || _a === void 0 ? void 0 : _a.nodeName) == 'VEGA-RADIO-GROUP';
    return sanitizeVegaComponent(h(Host, { class: {
        'vega-flex-shrink-0 v-min-w-min': isInsideRadioGroup,
      } },
      h("label", { class: {
          'vega-radio-container': true,
          [this.size]: true,
          'vega-radio-checked': this.checked,
          'vega-radio-disabled': this.isDisabled(),
          'error': this.showError,
        } },
        h("input", { type: "radio", name: this.name, checked: this.checked, value: this.value, onFocus: this.handleFocus, onBlur: this.handleBlur, onClick: this.handleClick, onChange: this.updateValue, "aria-label": this.ariaLabel || 'No label defined', disabled: this.isDisabled(), tabIndex: this.isDisabled() ? -1 : 0, onKeyDown: createEnterKeyHandlerToTriggerClick() }),
        h("span", { class: "vega-radio-icon" },
          h("span", { class: "vega-radio-circle" })),
        h("div", { class: "vega-radio-label", ref: (ref) => (this.labelElement = ref) },
          h("slot", null)))), this.host);
  }
  /**
   * indicating whether the component is disabled or not, single radio will take the self disabled value and the radio in group will take the self disabled and group disabled value
   *
   * @returns {boolean} the radio disabled or not
   */
  isDisabled() {
    return this.disabled || this.groupDisabled;
  }
  /**
   * The function checks if a radio button element can accept a disabled state from a group.
   *
   * @param {GroupDisabledPayload} input - The input parameter is of type GroupDisabledPayload, which is an object containing information about a disabled radio button group.
   * @returns {boolean} a boolean value, which indicates whether the input element (this.host) is present in a group  of radio buttons (vega-radio) or not.
   */
  canAcceptGroupDisabledObserver(input) {
    return findParent(this.host, 'vega-radio-group') === input.host;
  }
  /**
   * This function listens for changes in the disabled state of a group and updates the `groupDisabled` variable accordingly.
   *
   * @param {GroupDisabledPayload} input - The input parameter is of type GroupDisabledPayload, which is likely an object containing information about a disabled group.
   */
  groupDisabledObserverListener(input) {
    this.groupDisabled = input.host.disabled;
  }
  /**
   * This function initializes an observer for changes in the disabled state of a radio group.
   */
  initAndRegisterGroupDisabledObserver() {
    if (findParent(this.host, 'vega-radio-group')) {
      this.groupDisabledObserver = new Observer(this.canAcceptGroupDisabledObserver.bind(this), this.groupDisabledObserverListener.bind(this));
      ChangeManager.register(RADIO_GROUP_DISABLED_CHANGE, this.groupDisabledObserver);
    }
  }
  /**
   * This function unregister a group disabled observer if it exists.
   */
  unRegisterGroupDisabledObserver() {
    if (this.groupDisabledObserver) {
      ChangeManager.unregister(RADIO_GROUP_DISABLED_CHANGE, this.groupDisabledObserver);
    }
  }
  static get is() { return "vega-radio"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["vega-radio.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["vega-radio.css"]
  }; }
  static get properties() { return {
    "value": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.0.7"
          }],
        "text": "Represents the value associated with the radio button\n   that is currently selected or checked."
      },
      "attribute": "value",
      "reflect": false,
      "defaultValue": "''"
    },
    "name": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.3.0"
          }],
        "text": "Assigns a common name to the input element of the radio button,\n   to differentiate it from other buttons in a radio group."
      },
      "attribute": "name",
      "reflect": false
    },
    "checked": {
      "type": "boolean",
      "mutable": true,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.0.7"
          }],
        "text": "Determines if the radio button is selected or checked."
      },
      "attribute": "checked",
      "reflect": false,
      "defaultValue": "false"
    },
    "disabled": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.3.0"
          }],
        "text": "Determines if the radio button is disabled."
      },
      "attribute": "disabled",
      "reflect": false,
      "defaultValue": "false"
    },
    "size": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "'default' | 'small'",
        "resolved": "\"default\" | \"small\"",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.9.2"
          }],
        "text": "Specifies the size of the Radio Button component.\n\nThe size should be selected based on the context and\n   design requirements of the specific form."
      },
      "attribute": "size",
      "reflect": false,
      "defaultValue": "'default'"
    }
  }; }
  static get states() { return {
    "groupDisabled": {},
    "showError": {}
  }; }
  static get events() { return [{
      "method": "vegaFocus",
      "name": "vegaFocus",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.0.7"
          }],
        "text": "An event emitter notifying the focus event of the radio input."
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }, {
      "method": "focus",
      "name": "focus",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "eventSemantics",
            "text": "namespace:native"
          }, {
            "name": "vegaVersion",
            "text": "2.0.0"
          }],
        "text": "An event emitter notifying the focus event of the radio input."
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }, {
      "method": "vegaBlur",
      "name": "vegaBlur",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.0.7"
          }],
        "text": "An event emitter notifying the blur event of the radio input."
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }, {
      "method": "blur",
      "name": "blur",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "eventSemantics",
            "text": "namespace:native"
          }, {
            "name": "vegaVersion",
            "text": "2.0.0"
          }],
        "text": "An event emitter notifying the blur event of the radio input."
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }, {
      "method": "vegaChange",
      "name": "vegaChange",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.0.10"
          }],
        "text": "An event emitter notifying the change in the check status\n   of the radio input."
      },
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "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 the change in the check status\n   of the radio input."
      },
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      }
    }]; }
  static get elementRef() { return "host"; }
  static get watchers() { return [{
      "propName": "checked",
      "methodName": "watchCheckedChange"
    }]; }
}
__decorate([
  InjectVegaGlobalSlimmer()
], VegaRadio.prototype, "globalSlimmers", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaRadio.prototype, "changeEventEmitter", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaRadio.prototype, "focusEventEmitter", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaRadio.prototype, "blurEventEmitter", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaRadio.prototype, "vegaRadioShowErrorController", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaRadio.prototype, "vegaComponentUsageRuntimeMetricsSlimmer", void 0);
