import { r as registerInstance, c as createEvent, h, H as Host, g as getElement } from './index-baa5e267.js';
import { DateUtils, ElementIDUtils } from '@sankhyalabs/core';
import { C as CSSVarsUtils } from './CSSVarsUtils-acba92d7.js';
import { i as initI18n } from './index-41363fa6.js';

const ezDateInputCss = ":host{display:block;width:100%;--ez-date-input__input--background-color:var(--background--medium, #e0e0e0);--ez-date-input__input--border-color:var(--ez-date-input__input--background-color);--ez-date-input__calendar-image:url('data:image/svg+xml;utf8,<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" height=\"16px\" width=\"15px\"><path d=\"M 3.171875,5.25 C 2.6485088,5.25 2.21875,5.6797588 2.21875,6.203125 2.21875,6.7264912 2.6485088,7.15625 3.171875,7.15625 3.6952412,7.15625 4.125,6.7264912 4.125,6.203125 4.125,5.6797588 3.6952412,5.25 3.171875,5.25 Z m 2.875,0 C 5.5235088,5.25 5.09375,5.6797588 5.09375,6.203125 5.09375,6.7264912 5.5235088,7.15625 6.046875,7.15625 6.5702412,7.15625 7,6.7264912 7,6.203125 7,5.6797588 6.5702412,5.25 6.046875,5.25 Z m 2.875,0 c -0.5233662,0 -0.953125,0.4297588 -0.953125,0.953125 0,0.5233662 0.4297588,0.953125 0.953125,0.953125 0.5233662,0 0.9511719,-0.4297588 0.9511719,-0.953125 C 9.8730469,5.6797588 9.4452412,5.25 8.921875,5.25 Z m 2.873047,0 c -0.523366,0 -0.953125,0.4297588 -0.953125,0.953125 0,0.5233662 0.429759,0.953125 0.953125,0.953125 0.523366,0 0.953125,-0.4297588 0.953125,-0.953125 C 12.748047,5.6797588 12.318288,5.25 11.794922,5.25 Z M 3.171875,8.1230469 c -0.5233662,0 -0.953125,0.4297588 -0.953125,0.953125 0,0.5233662 0.4297588,0.9531251 0.953125,0.9531251 0.5233662,0 0.953125,-0.4297589 0.953125,-0.9531251 0,-0.5233662 -0.4297588,-0.953125 -0.953125,-0.953125 z m 2.875,0 c -0.5233662,0 -0.953125,0.4297588 -0.953125,0.953125 0,0.5233662 0.4297588,0.9531251 0.953125,0.9531251 C 6.5702412,10.029297 7,9.5995381 7,9.0761719 7,8.5528057 6.5702412,8.1230469 6.046875,8.1230469 Z m 2.8554688,0 c -0.5233663,0 -0.953125,0.4297588 -0.953125,0.953125 0,0.5233662 0.4297587,0.9531251 0.953125,0.9531251 0.5233661,0 0.9531249,-0.4297589 0.953125,-0.9531251 0,-0.5233662 -0.4297588,-0.953125 -0.953125,-0.953125 z m 2.9003902,0 c -0.523366,0 -0.953125,0.4297588 -0.953125,0.953125 0,0.5233662 0.429759,0.9531251 0.953125,0.9531251 0.523367,0 0.953125,-0.4297589 0.953125,-0.9531251 0,-0.5233662 -0.429758,-0.953125 -0.953125,-0.953125 z M 3.171875,10.998047 c -0.5233662,0 -0.953125,0.429759 -0.953125,0.953125 0,0.523366 0.4297588,0.953125 0.953125,0.953125 0.5233662,0 0.953125,-0.429759 0.953125,-0.953125 0,-0.523366 -0.4297588,-0.953125 -0.953125,-0.953125 z m 2.875,0 c -0.5233662,0 -0.953125,0.429759 -0.953125,0.953125 0,0.523366 0.4297588,0.953125 0.953125,0.953125 C 6.5702412,12.904297 7,12.474538 7,11.951172 7,11.427806 6.5702412,10.998047 6.046875,10.998047 Z m 2.8554688,0 c -0.5233663,0 -0.953125,0.429759 -0.953125,0.953125 0,0.523366 0.4297587,0.953125 0.953125,0.953125 0.5233661,0 0.9531249,-0.429759 0.953125,-0.953125 0,-0.523366 -0.4297588,-0.953125 -0.953125,-0.953125 z m 2.9003902,0 c -0.523366,0 -0.953125,0.429759 -0.953125,0.953125 0,0.523366 0.429759,0.953125 0.953125,0.953125 0.523367,0 0.953125,-0.429759 0.953125,-0.953125 0,-0.523366 -0.429758,-0.953125 -0.953125,-0.953125 z M 3.5,0 c -0.554,0 -1,0.446 -1,1 v 0.050781 C 1.0853217,1.2909766 0,2.5186658 0,4 v 9 c 0,1.652487 1.3475134,3 3,3 h 9 c 1.652487,0 3,-1.347513 3,-3 V 4 C 15,2.5186658 13.914678,1.2909766 12.5,1.0507812 V 1 c 0,-0.554 -0.446,-1 -1,-1 -0.554,0 -1,0.446 -1,1 h -2 c 0,-0.554 -0.446,-1 -1,-1 -0.554,0 -1,0.446 -1,1 h -2 C 4.5,0.446 4.054,0 3.5,0 Z m 1,2.0996094 h 2 v 0.640625 c 0,0.554 0.446,1 1,1 0.554,0 1,-0.446 1,-1 v -0.640625 h 2 v 0.640625 c 0,0.554 0.446,1 1,1 0.554,0 1,-0.446 1,-1 V 2.1738281 C 13.311725,2.3905225 13.900391,3.11278 13.900391,4 v 9 c 0,1.062113 -0.838278,1.900391 -1.900391,1.900391 H 3 C 1.9378864,14.900391 1.0996094,14.062113 1.0996094,13 V 4 C 1.0996094,3.11278 1.6882747,2.3905225 2.5,2.1738281 v 0.5664063 c 0,0.554 0.446,1 1,1 0.554,0 1,-0.446 1,-1 z\"/></svg>')}.btn-open-cal{outline:none;border:none;background-color:unset;cursor:pointer}.btn-open-cal:disabled{cursor:not-allowed}.btn-open-cal::after{content:'';display:flex;background-color:var(--text--primary, #008561);width:15px;height:16px;-webkit-mask-image:var(--ez-date-input__calendar-image);mask-image:var(--ez-date-input__calendar-image)}.btn-open-cal:disabled:after{background-color:var(--text--disable, #AFB6C0)}.btn-open-cal:enabled:hover::after{background-color:var(--color--primary, #4e4e4e)}ez-text-input{--text-input__input--background-color:var(--ez-date-input__input--background-color, #FFFFFF);--text-input__input--border-color:var(--ez-date-input__input--border-color, #DCE0E8)}";

const EzDateInput = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.ezChange = createEvent(this, "ezChange", 7);
    this.ezStartChange = createEvent(this, "ezStartChange", 7);
    this.ezCancelWaitingChange = createEvent(this, "ezCancelWaitingChange", 7);
    this._changePending = false;
    this._focused = false;
    this._valuePromiseCallbacks = [];
    this.label = undefined;
    this.value = undefined;
    this.enabled = true;
    this.errorMessage = undefined;
    this.mode = "regular";
    this.canShowError = true;
    this.autoFocus = false;
    this.alternativePlaceholder = undefined;
  }
  observeLabel() {
    if (this._textInput) {
      this._textInput.label = this.label;
    }
  }
  observeErrorMessage() {
    var _a;
    if (this._textInput) {
      this._textInput.errorMessage = this.errorMessage;
      if (!((_a = this.errorMessage) === null || _a === void 0 ? void 0 : _a.trim())) {
        this.setInputValue();
      }
    }
  }
  observeValue(newValue, oldValue) {
    if (this._textInput && newValue != oldValue) {
      const newValueValidated = DateUtils.validateDate(newValue);
      const oldValueValidated = DateUtils.validateDate(oldValue);
      if ((newValueValidated === null || newValueValidated === void 0 ? void 0 : newValueValidated.getTime()) !== (oldValueValidated === null || oldValueValidated === void 0 ? void 0 : oldValueValidated.getTime())) {
        const textValue = this.getTextValue(newValueValidated) || '';
        const parsedDate = DateUtils.strToDate(textValue);
        if ((parsedDate || !this._focused) && (this._textInput.value || '') !== textValue) {
          this._textInput.value = textValue;
          this._focused = false;
          this.errorMessage = "";
        }
        const valueEmitted = newValueValidated === null ? undefined : newValueValidated;
        this.ezChange.emit(valueEmitted);
      }
    }
    this._changePending = false;
  }
  /**
   * Aplica o foco no campo.
   */
  async setFocus(options) {
    this._textInput.setFocus(options);
  }
  /**
   * Remove o foco do campo.
   */
  async setBlur() {
    this._textInput.setBlur();
  }
  /**
   * Retorna se o conteúdo é inválido.
   */
  async isInvalid() {
    return typeof this.errorMessage === "string" && this.errorMessage.trim() !== "";
  }
  /*
  * Retorna uma promise com o valor da opção selecionada,
  * que será resolvida quando o backend devolver este dado.
  */
  async getValueAsync() {
    if (this._changePending) {
      return new Promise(accept => {
        this._valuePromiseCallbacks.push(accept);
      });
    }
    return Promise.resolve(this.value);
  }
  changeValue(newValue) {
    const currentValue = DateUtils.validateDate(this.value);
    const newValueValidated = DateUtils.validateDate(newValue);
    if ((currentValue === null || currentValue === void 0 ? void 0 : currentValue.getTime()) !== (newValueValidated === null || newValueValidated === void 0 ? void 0 : newValueValidated.getTime())) {
      this.value = newValueValidated;
    }
    else {
      this._changePending = false;
    }
  }
  hideCalendar() {
    this.changeValue(this._calendar.value);
    this._popover.hide();
  }
  handleBlur() {
    if (!this._changePending) {
      return;
    }
    try {
      const strValue = this._textInput.value;
      const newValue = DateUtils.strToDate(strValue);
      if (newValue || !strValue) {
        this.errorMessage = "";
        const currentValue = DateUtils.validateDate(this.value);
        const newValueValidated = DateUtils.validateDate(newValue);
        if ((currentValue === null || currentValue === void 0 ? void 0 : currentValue.getTime()) === (newValueValidated === null || newValueValidated === void 0 ? void 0 : newValueValidated.getTime())) {
          if (newValueValidated) {
            this._textInput.value = this.getTextValue(newValueValidated) || '';
            this._focused = false;
          }
          this.ezCancelWaitingChange.emit();
        }
        else {
          this._focused = false;
          this.changeValue(newValueValidated);
        }
      }
      else {
        this.changeValue(undefined);
        this.ezCancelWaitingChange.emit();
        this.errorMessage = this.i18n("ez-date-input.invalidDate");
      }
    }
    finally {
      for (const callback of this._valuePromiseCallbacks) {
        callback(this.value);
      }
      this._valuePromiseCallbacks = [];
    }
  }
  getTextValue(d) {
    return d ? DateUtils.formatDate(d) : undefined;
  }
  handleInput(event) {
    const strValue = this._textInput.value;
    const newValue = DateUtils.strToDate(strValue);
    const currentValue = DateUtils.validateDate(this.value);
    const newValueValidated = DateUtils.validateDate(newValue);
    if ((currentValue === null || currentValue === void 0 ? void 0 : currentValue.getTime()) !== (newValueValidated === null || newValueValidated === void 0 ? void 0 : newValueValidated.getTime())) {
      this._changePending = true;
      this._focused = true;
      this.ezStartChange.emit({ waitmessage: "", blocking: false });
    }
    if (event['data'] === "H" || event['data'] === "h") {
      this.changeValue(new Date());
    }
  }
  setInputValue() {
    const textValue = this.getTextValue(this.value) || '';
    if ((this._textInput.value || '') !== textValue) {
      this._textInput.value = textValue;
    }
  }
  disconnectedCallback() {
    this._popover.hide();
  }
  async componentWillLoad() {
    this.i18n = await initI18n();
  }
  componentDidLoad() {
    CSSVarsUtils.applyVarsTextInput(this._elem, this._textInput);
    this.setInputValue();
    if (this.autoFocus) {
      requestAnimationFrame(() => {
        this.setFocus({ selectText: true });
      });
    }
  }
  render() {
    ElementIDUtils.addIDInfoIfNotExists(this._elem, 'input');
    return (h(Host, null, h("ez-text-input", { "data-element-id": ElementIDUtils.getInternalIDInfo("textInput"), ref: elem => this._textInput = elem, "data-slave-mode": "true", label: this.label, onBlur: () => this.handleBlur(), onInput: (evt) => this.handleInput(evt), restrict: "0123456789/", enabled: this.enabled, errorMessage: this.errorMessage, mode: this.mode, canShowError: this.canShowError, alternativePlaceholder: this.alternativePlaceholder }, h("button", { disabled: !this.enabled, tabindex: -1, class: "btn-open-cal", onClick: () => { var _a; return (_a = this._popover) === null || _a === void 0 ? void 0 : _a.showUnder(this._textInput); }, slot: "leftIcon" })), h("ez-popover-plus", { ref: elem => this._popover = elem, anchorElement: this._textInput, autoClose: true, boxWidth: 'fit-content', overlayType: 'none', "data-element-id": this._elem.getAttribute("data-element-id"), onEzVisibilityChange: (event) => event.stopPropagation() }, h("ez-calendar", { ref: elem => this._calendar = elem, "data-element-id": ElementIDUtils.getInternalIDInfo("calendar"), onEzChange: (event) => { this.hideCalendar(); event.stopPropagation(); } }))));
  }
  get _elem() { return getElement(this); }
  static get watchers() { return {
    "label": ["observeLabel"],
    "errorMessage": ["observeErrorMessage"],
    "value": ["observeValue"]
  }; }
};
EzDateInput.style = ezDateInputCss;

export { EzDateInput as ez_date_input };
