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

const textInputCss = "*,*::before,*::after{padding:0;margin:0;box-sizing:border-box}.text{font-family:\"Roboto\";font-style:normal}.text__wrapper{position:relative;width:100%;display:flex;flex-direction:column;gap:5px;height:100%}.text__wrapper--autofilled{pointer-events:none}.text__wrapper--autofilled .text__label{color:var(--emfe-w-registration-typography, var(--emfe-w-color-black, #000000))}.text__wrapper--autofilled .text__input::part(input-field){color:var(--emfe-w-color-black, #000000)}.text__wrapper--flex{display:flex;gap:5px}.text__wrapper--relative{position:relative}.text__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))}.text__label--required::after{content:\"*\";font-family:inherit;color:var(--emfe-w-login-color-primary, var(--emfe-w-color-primary, #D0046C));margin-left:2px}.text__input{font-family:inherit;width:100%;height:44px;border:1px solid var(--emfe-w-color-gray-100, #E6E6E6);background-color:var(--emfe-w-color-white, #FFFFFF);color:var(--emfe-w-color-black, #000000);border-radius:5px;font-size:16px;font-weight:300;line-height:1.5;padding:5px 15px}.text__input:focus{border:1px solid var(--emfe-w-login-color-primary, var(--emfe-w-color-primary, #D0046C))}.text__input--invalid{border:1px solid var(--emfe-w-color-error, var(--emfe-w-color-red, #ed0909))}.text__input::placeholder{color:var(--emfe-w-color-gray-150, #828282)}.text__error-message{position:absolute;top:calc(100% + 5px);left:0;color:var(--emfe-w-color-error, var(--emfe-w-color-red, #ed0909))}.text__tooltip-icon{width:16px;height:auto}.text__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}.text__tooltip.visible{opacity:1}";

const TextInput = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
    this.sendValidityState = createEvent(this, "sendValidityState", 7);
    this.sendInputValue = createEvent(this, "sendInputValue", 7);
    /**
     * Default value for the input.
     */
    this.defaultValue = '';
    /**
     * Client custom styling via inline style
     */
    this.clientStyling = '';
    this.errorMessage = '';
    this.limitStylingAppends = false;
    this.showTooltip = false;
    this.value = '';
    this.validationPattern = '';
    this.duplicateInputValue = null;
    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.validityStateHandler({ valid: this.isValid, name: this.name });
        this.emitValueHandler(true);
      }, 500);
    };
    this.handleBlur = (event) => {
      this.value = event.target.value;
      this.touched = true;
      this.isValid = this.setValidity();
      this.errorMessage = this.setErrorMessage();
      this.validityStateHandler({ valid: this.isValid, name: this.name });
    };
    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 && this.name === event.detail.name + 'Duplicate') {
      this.duplicateInputValue = event.detail.value;
      if (this.touched) {
        this.isValid = this.setValidity();
        this.errorMessage = this.setErrorMessage();
      }
    }
    if (this.name === event.detail.name + 'Duplicate'
      && this.name.replace('Duplicate', '') === event.detail.name
      && this.touched === true) {
      this.isValid = this.setValidity();
      this.errorMessage = this.setErrorMessage();
    }
  }
  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() {
    if (this.defaultValue) {
      this.value = this.defaultValue;
      this.valueHandler({ name: this.name, value: this.value });
      if (this.isDuplicateInput) {
        this.touched = true;
      }
    }
    this.isValid = this.setValidity();
  }
  setValidity() {
    var _a;
    if (this.isDuplicateInput && this.duplicateInputValue !== this.value) {
      return false;
    }
    else {
      return (_a = this.inputReference) === null || _a === void 0 ? void 0 : _a.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: `text__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: `text__wrapper ${this.name}__input ${this.autofilled ? 'text__wrapper--autofilled' : ''}`, ref: el => this.stylingContainer = el }, h("div", { class: 'text__wrapper--flex' }, h("label", { class: `text__label  ${this.validation.mandatory ? 'text__label--required' : ''}`, htmlFor: `${this.name}__input` }, this.displayName), h("div", { class: 'text__wrapper--relative' }, this.tooltip &&
      h("img", { class: 'text__tooltip-icon', src: tooltipIconSvg, alt: "", ref: (el) => this.tooltipIconReference = el, onClick: () => this.showTooltip = !this.showTooltip }), this.renderTooltip())), h("input", { name: this.name, id: `${this.name}__input`, value: this.defaultValue, type: 'text', class: `text__input ${invalidClass}`, placeholder: `${this.placeholder}`, ref: (el) => this.inputReference = el, readOnly: this.autofilled, pattern: this.validationPattern, required: this.validation.mandatory, minlength: this.validation.minLength, maxlength: this.validation.maxLength, onInput: this.handleInput, onBlur: this.handleBlur }), h("small", { class: 'text__error-message' }, this.errorMessage));
  }
  static get watchers() { return {
    "isValid": ["validityChanged"],
    "emitValue": ["emitValueHandler"]
  }; }
  static get style() { return textInputCss; }
}, [1, "text-input", {
    "name": [513],
    "displayName": [513, "display-name"],
    "placeholder": [513],
    "validation": [16],
    "defaultValue": [513, "default-value"],
    "autofilled": [516],
    "tooltip": [513],
    "language": [513],
    "checkValidity": [516, "check-validity"],
    "emitValue": [516, "emit-value"],
    "isDuplicateInput": [516, "is-duplicate-input"],
    "clientStyling": [513, "client-styling"],
    "isValid": [32],
    "errorMessage": [32],
    "limitStylingAppends": [32],
    "showTooltip": [32]
  }, [[4, "click", "handleClickOutside"], [16, "sendInputValue", "valueChangedHandler"]]]);
function defineCustomElement() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["text-input"];
  components.forEach(tagName => { switch (tagName) {
    case "text-input":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, TextInput);
      }
      break;
  } });
}

export { TextInput as T, defineCustomElement as d };
