import { proxyCustomElement, HTMLElement, createEvent, h } from '@stencil/core/internal/client';
import { t as translate, a as tooltipIconSvg } from './tooltipIcon.js';

const radioInputCss = "*,*::before,*::after{padding:0;margin:0;box-sizing:border-box}.radio__fieldset{border:none;position:relative}.radio__wrapper{display:flex;gap:5px}.radio__error-message{position:absolute;top:calc(100% + 5px);left:0;color:var(--emfe-w-color-error, var(--emfe-w-color-red, #ed0909))}.radio__tooltip-icon{position:absolute;right:0;bottom:10px}.radio__tooltip{position:absolute;bottom:35px;right:10px;background-color:var(--emfe-w-color-white, #FFFFFF);border:1px solid var(--emfe-w-color-gray-100, #E6E6E6);color:var(--emfe-w-registration-typography, var(--emfe-w-color-black, #000000));padding:10px;border-radius:5px;opacity:0;transition:opacity 0.3s ease-in-out;z-index:10}.radio__tooltip.visible{opacity:1}";

const RadioInput = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
    this.sendInputValue = createEvent(this, "sendInputValue", 7);
    this.sendValidityState = createEvent(this, "sendValidityState", 7);
    /**
     * Client custom styling via inline style
     */
    this.clientStyling = '';
    this.limitStylingAppends = false;
    this.showTooltip = false;
    this.setClientStyling = () => {
      let sheet = document.createElement('style');
      sheet.innerHTML = this.clientStyling;
      this.stylingContainer.prepend(sheet);
    };
  }
  validityChanged() {
    this.validityStateHandler({ valid: this.isValid, name: this.name });
    if (this.emitValue == true) {
      this.valueHandler({ name: this.name, value: this.value });
    }
  }
  emitValueHandler(newValue) {
    if (newValue == true && this.isValid) {
      this.valueHandler({ name: this.name, value: this.value });
    }
  }
  valueHandler(inputValueEvent) {
    this.sendInputValue.emit(inputValueEvent);
  }
  validityStateHandler(inputStateEvent) {
    this.sendValidityState.emit(inputStateEvent);
  }
  handleClickOutside(event) {
    if (event.composedPath()[0] === this.tooltipIconReference)
      return;
    if (event.composedPath()[0] !== this.tooltipReference)
      this.showTooltip = false;
  }
  componentDidRender() {
    // start custom styling area
    if (!this.limitStylingAppends && this.stylingContainer) {
      if (this.clientStyling)
        this.setClientStyling();
      this.limitStylingAppends = true;
    }
    // end custom styling area
  }
  handleClick(event) {
    this.value = event.target.value;
    this.isValid = this.setValidity();
    this.errorMessage = this.setErrorMessage();
    this.emitValueHandler(true);
  }
  setValidity() {
    return this.inputReference.validity.valid;
  }
  setErrorMessage() {
    if (this.inputReference.validity.valueMissing) {
      return translate('requiredError', this.language);
    }
  }
  renderTooltip() {
    if (this.showTooltip) {
      return (h("div", { class: `radio__tooltip ${this.showTooltip ? 'visible' : ''}`, ref: (el) => this.tooltipReference = el, innerHTML: this.tooltip }));
    }
    return null;
  }
  render() {
    return h("fieldset", { class: `radio__fieldset ${this.name}__input`, ref: el => this.stylingContainer = el }, h("legend", { class: 'radio__legend' }, this.displayName, ":"), this.optionsGroup.map(option => h("div", { class: 'radio__wrapper' }, h("input", { type: "radio", class: 'radio__input', id: `${option.label}__input`, ref: (el) => this.inputReference = el, value: option.value, name: this.name, required: this.validation.mandatory, onClick: (e) => this.handleClick(e) }), h("label", { htmlFor: `${option.label}__input` }, option.label))), h("small", { class: 'radio__error-message' }, this.errorMessage), this.tooltip &&
      h("img", { class: 'radio__tooltip-icon', src: tooltipIconSvg, alt: "", ref: (el) => this.tooltipIconReference = el, onClick: () => this.showTooltip = !this.showTooltip }), this.renderTooltip());
  }
  static get watchers() { return {
    "isValid": ["validityChanged"],
    "emitValue": ["emitValueHandler"]
  }; }
  static get style() { return radioInputCss; }
}, [1, "radio-input", {
    "name": [513],
    "displayName": [513, "display-name"],
    "optionsGroup": [16],
    "validation": [16],
    "tooltip": [513],
    "language": [513],
    "emitValue": [516, "emit-value"],
    "clientStyling": [513, "client-styling"],
    "errorMessage": [32],
    "isValid": [32],
    "limitStylingAppends": [32],
    "showTooltip": [32]
  }, [[4, "click", "handleClickOutside"]]]);
function defineCustomElement() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["radio-input"];
  components.forEach(tagName => { switch (tagName) {
    case "radio-input":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, RadioInput);
      }
      break;
  } });
}

export { RadioInput as R, defineCustomElement as d };
