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

const emailInputCss = "*,*::before,*::after{padding:0;margin:0;box-sizing:border-box}.email{font-family:\"Roboto\";font-style:normal}.email__wrapper{position:relative;width:100%}.email__wrapper--autofilled{pointer-events:none}.email__wrapper--autofilled .email__label{color:var(--emfe-w-registration-typography, var(--emfe-w-color-black, #000000))}.email__wrapper--autofilled .email__input{color:var(--emfe-w-color-black, #000000)}.email__wrapper--flex{display:flex;gap:5px}.email__wrapper--relative{position:relative}.email__label{font-family:inherit;font-style:normal;font-weight:500;font-size:16px;line-height:20px;color:var(--emfe-w-registration-typography, var(--emfe-w-color-black, #000000))}.email__label--required::after{content:\"*\";font-family:inherit;color:var(--emfe-w-login-color-primary, var(--emfe-w-color-primary, #D0046C));margin-left:2px}.email__input{font-family:inherit;border-radius:4px;width:100%;height:40px;border:2px solid var(--emfe-w-color-gray-100, #E6E6E6);color:var(--emfe-w-color-black, #000000);border-radius:5px;box-sizing:border-box;font-size:16px;font-weight:300;line-height:1.5;padding:10px}.email__input:focus{outline-color:var(--emfe-w-login-color-primary, var(--emfe-w-color-primary, #D0046C))}.email__input--invalid{border:1px solid var(--emfe-w-color-error, var(--emfe-w-color-red, #ed0909))}.email__input::placeholder{color:var(--emfe-w-color-gray-150, #828282)}.email__error-message{position:absolute;top:calc(100% + 5px);left:0;color:var(--emfe-w-color-error, var(--emfe-w-color-red, #ed0909))}.email__tooltip-icon{width:16px;height:auto}.email__tooltip{position:absolute;top:0;left:20px;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}.email__tooltip.visible{opacity:1}";

const EmailInput = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
    this.sendValidityState = createEvent(this, "sendValidityState", 7);
    this.sendInputValue = createEvent(this, "sendInputValue", 7);
    /**
     * Client custom styling via inline style
     */
    this.clientStyling = '';
    this.limitStylingAppends = false;
    this.showTooltip = false;
    this.validationPattern = '';
    this.touched = false;
    this.handleInput = (event) => {
      this.value = event.target.value;
      this.touched = true;
      if (this.debounceTime) {
        clearTimeout(this.debounceTime);
      }
      this.debounceTime = setTimeout(() => {
        this.isValid = this.setValidity();
        this.errorMessage = this.setErrorMessage();
        this.emitValueHandler(true);
      }, 500);
    };
    this.handleBlur = () => {
      this.isValid = this.setValidity();
      this.errorMessage = this.setErrorMessage();
      this.touched = true;
    };
    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 });
    }
  }
  validityStateHandler(inputStateEvent) {
    this.sendValidityState.emit(inputStateEvent);
  }
  valueHandler(inputValueEvent) {
    this.sendInputValue.emit(inputValueEvent);
  }
  handleClickOutside(event) {
    if (event.composedPath()[0] === this.tooltipIconReference)
      return;
    if (event.composedPath()[0] !== this.tooltipReference)
      this.showTooltip = false;
  }
  valueChangedHandler(event) {
    if (this.isDuplicateInput) {
      if (this.name === event.detail.name + 'Duplicate') {
        this.duplicateInputValue = event.detail.value;
      }
    }
  }
  connectedCallback() {
    this.validationPattern = this.setPattern();
  }
  componentDidRender() {
    // start custom styling area
    if (!this.limitStylingAppends && this.stylingContainer) {
      if (this.clientStyling)
        this.setClientStyling();
      this.limitStylingAppends = true;
    }
    // end custom styling area
  }
  componentDidLoad() {
    this.isValid = this.setValidity();
    if (this.defaultValue) {
      this.value = this.defaultValue;
      this.valueHandler({ name: this.name, value: this.value });
    }
  }
  setValidity() {
    return this.inputReference.validity.valid;
  }
  setPattern() {
    var _a, _b;
    if (((_a = this.validation.custom) === null || _a === void 0 ? void 0 : _a.length) > 0) {
      return (_b = this.validation.custom.find(customValidation => customValidation.rule === 'regex')) === null || _b === void 0 ? void 0 : _b.pattern;
    }
  }
  setErrorMessage() {
    var _a, _b, _c, _d;
    if (this.inputReference.validity.patternMismatch) {
      const errorCode = (_a = this.validation.custom.find(customValidation => customValidation.rule === 'regex')) === null || _a === void 0 ? void 0 : _a.errorKey;
      const errorMessage = (_b = this.validation.custom.find(customValidation => customValidation.rule === 'regex')) === null || _b === void 0 ? void 0 : _b.errorMessage;
      return translate(`${errorCode}`, this.language) ? translate(`${errorCode}`, this.language) : errorMessage;
    }
    if (this.inputReference.validity.tooShort || this.inputReference.validity.tooLong) {
      return translate('lengthError', this.language, { values: { minLength: this.validation.minLength, maxLength: this.validation.maxLength } });
    }
    if (this.inputReference.validity.valueMissing) {
      return translate('requiredError', this.language);
    }
    if (this.isDuplicateInput && this.duplicateInputValue !== this.value) {
      const errorCode = (_c = this.validation.custom.find(customRule => customRule.rule === 'duplicate-input')) === null || _c === void 0 ? void 0 : _c.errorKey;
      const errorMessage = (_d = this.validation.custom.find(customRule => customRule.rule === 'duplicate-input')) === null || _d === void 0 ? void 0 : _d.errorMessage;
      return translate(`${errorCode}`, this.language) ? translate(`${errorCode}`, this.language) : errorMessage;
    }
  }
  renderTooltip() {
    if (this.showTooltip) {
      return (h("div", { class: `email__tooltip ${this.showTooltip ? 'visible' : ''}`, ref: (el) => this.tooltipReference = el, innerHTML: this.tooltip }));
    }
    return null;
  }
  render() {
    let invalidClass = '';
    if (this.touched) {
      invalidClass = this.isValid == true || this.isValid == undefined ? '' : 'text__input--invalid';
    }
    return h("div", { class: `email__wrapper ${this.autofilled ? 'number__wrapper--autofilled' : ''}  ${this.name}__input`, ref: el => this.stylingContainer = el }, h("div", { class: 'email__wrapper--flex' }, h("label", { class: `email__label ${this.validation.mandatory ? 'email__label--required' : ''}`, htmlFor: `${this.name}__input` }, this.displayName), h("div", { class: 'email__wrapper--relative' }, this.tooltip &&
      h("img", { class: 'email__tooltip-icon', src: tooltipIconSvg, alt: "", ref: (el) => this.tooltipIconReference = el, onClick: () => this.showTooltip = !this.showTooltip }), this.renderTooltip())), h("input", { id: `${this.name}__input`, type: 'email', class: `email__input ${invalidClass}`, value: this.defaultValue, readOnly: this.autofilled, placeholder: `${this.placeholder}`, ref: (el) => this.inputReference = el, pattern: this.validationPattern, required: this.validation.mandatory, minlength: this.validation.minLength, maxlength: this.validation.maxLength, onInput: this.handleInput, onBlur: this.handleBlur }), h("small", { class: 'email__error-message' }, this.errorMessage));
  }
  static get watchers() { return {
    "isValid": ["validityChanged"],
    "emitValue": ["emitValueHandler"]
  }; }
  static get style() { return emailInputCss; }
}, [1, "email-input", {
    "name": [513],
    "displayName": [513, "display-name"],
    "placeholder": [513],
    "validation": [16],
    "defaultValue": [513, "default-value"],
    "autofilled": [516],
    "tooltip": [513],
    "language": [513],
    "emitValue": [516, "emit-value"],
    "isDuplicateInput": [516, "is-duplicate-input"],
    "clientStyling": [513, "client-styling"],
    "errorMessage": [32],
    "isValid": [32],
    "limitStylingAppends": [32],
    "showTooltip": [32]
  }, [[4, "click", "handleClickOutside"], [16, "sendInputValue", "valueChangedHandler"]]]);
function defineCustomElement() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["email-input"];
  components.forEach(tagName => { switch (tagName) {
    case "email-input":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, EmailInput);
      }
      break;
  } });
}

export { EmailInput as E, defineCustomElement as d };
