import { r as registerInstance, e as createEvent, h, g as getElement } from './index-e9c7ceaa.js';
import { ElementIDUtils } from '@sankhyalabs/core';
import { F as FilterItemType } from './filter-item-type.enum-d45e026f.js';
import { E as EPresentationMode } from './presentationMode-783bbf9d.js';

const SnkFilterPeriod = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.valueChanged = createEvent(this, "valueChanged", 7);
    this._startDateLabel = 'Inicial';
    this._endDateLabel = 'Final';
    this._toLabel = 'até';
    this._defaultEmptyValue = {
      start: null,
      end: null
    };
    this.internalChange = {
      start: false,
      end: false
    };
    this.config = undefined;
    this.getMessage = undefined;
    this.value = this._defaultEmptyValue;
    this.presentationMode = EPresentationMode.CHIP;
    this.errorMessage = undefined;
  }
  configChanged(newValue, oldValue) {
    if (this.internalChange.end) {
      this.internalChange.end = false;
      return;
    }
    if (this.internalChange.start) {
      this.internalChange.start = false;
      return;
    }
    if ((!newValue && oldValue) || (!(newValue === null || newValue === void 0 ? void 0 : newValue.start) && !(newValue === null || newValue === void 0 ? void 0 : newValue.end))) {
      this.clearValue();
      return;
    }
    if (newValue && (newValue.start !== (oldValue === null || oldValue === void 0 ? void 0 : oldValue.start) || newValue.end !== (oldValue === null || oldValue === void 0 ? void 0 : oldValue.end))) {
      this.valueStart = this.parseDate(newValue.start);
      this.valueEnd = this.parseDate(newValue.end);
    }
  }
  ezChangeListener(event, field) {
    this.errorMessage = undefined;
    this.internalChange[field] = true;
    this.value = Object.assign(Object.assign({}, (this.value || {})), { [field]: event.detail ? this.parseDate(event.detail) : null });
    this.valueChanged.emit(this.value);
  }
  /**
   * Define o foco no componente snk-filter-period.
   */
  async setFocus() {
    this._startDate.setFocus();
  }
  /**
   * Limpa o valor do componente restaurando o valor original da configuração.
   */
  async clearValue() {
    this.value = this.config.value || this._defaultEmptyValue;
    this.valueStart = this.getDate("start");
    this.valueEnd = this.getDate("end");
    this._startDate.value = this.valueStart;
    this._endDate.value = this.valueEnd;
  }
  componentDidLoad() {
    if (this._element) {
      ElementIDUtils.addIDInfo(this._element, 'filterContentEditor');
      this._element.querySelectorAll('ez-date-input').forEach(input => {
        const textInput = input.shadowRoot.querySelector('ez-text-input');
        textInput.style.setProperty('--ez-text-input__min-width', '120px');
      });
    }
  }
  parseDate(date) {
    if (date instanceof Date) {
      return date;
    }
    if (typeof date === "string") {
      date = new Date(date);
      date.setMinutes(date.getMinutes() + date.getTimezoneOffset());
      return date;
    }
    return null;
  }
  getDate(prop) {
    const objValue = this.value ? this.value[prop] : null;
    return this.parseDate(objValue);
  }
  /**
   * Exibe o componente snk-filter-period
   */
  async show() {
    this._startDate.setFocus();
  }
  buildLabel() {
    if (this.presentationMode === EPresentationMode.CHIP) {
      return h("label", { class: "ez-text ez-text--medium ez-text--primary ez-margin--medium" }, this._toLabel);
    }
  }
  componentWillLoad() {
    if (this.getMessage) {
      this._startDateLabel = this.getMessage('snkFilterBar.labelStart');
      this._endDateLabel = this.getMessage('snkFilterBar.labelEnd');
      this._toLabel = this.getMessage('snkFilterBar.labelTo');
    }
    this.valueStart = this.getDate("start");
    this.valueEnd = this.getDate("end");
  }
  render() {
    if (!this.config || this.config.type !== FilterItemType.PERIOD) {
      return undefined;
    }
    return (h("ez-tooltip", { active: this.config.enabled === false, message: this.config.disabledMessage, type: 'warning' }, h("div", { class: "ez-col ez-col--nowrap" }, h("ez-date-input", { id: `${this.config.id}_start`, class: this.presentationMode === EPresentationMode.MODAL ? 'ez-padding-right--medium' : '', label: this._startDateLabel, ref: ref => this._startDate = ref, enabled: this.config.enabled, value: this.valueStart, errorMessage: this.errorMessage, onEzInput: (event) => this.ezChangeListener(event, 'start') }), this.buildLabel(), h("ez-date-input", { id: `${this.config.id}_end`, label: this._endDateLabel, ref: ref => this._endDate = ref, enabled: this.config.enabled, value: this.valueEnd, errorMessage: this.errorMessage, onEzInput: (event) => this.ezChangeListener(event, 'end') }))));
  }
  get _element() { return getElement(this); }
  static get watchers() { return {
    "value": ["configChanged"]
  }; }
};

export { SnkFilterPeriod as snk_filter_period };
