var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { Fragment, h } from '@stencil/core';
import { MapToComponentField } from 'vega-slimmer/core';
import { LogUtility } from '../../../../utils/log';
import { VegaDatePickerInputRendererBase } from './vega-date-picker-input-renderer-base.abstract';
import { CalendarDate } from '../../../../helpers/calendar';
export class VegaDatePickerRangeInputRenderer extends VegaDatePickerInputRendererBase {
  constructor() {
    super(...arguments);
    this.render = () => {
      const label = this.label ? `, ${this.label}` : '';
      const picker = `, ${this.translationSlimmer.t('date picker')}`;
      const startAriaLabel = `${this.translationSlimmer.t('Start Date')}${label}${picker}`;
      const endAriaLabel = `${this.translationSlimmer.t('End Date')}${label}${picker}`;
      return (h(Fragment, null,
        h("div", { class: "date-picker-input" },
          this.vegaDatePickerRenderer.renderInputPrefix(),
          h("input", { readOnly: !this.vegaDatePickerRenderer.isInputEnabled(), placeholder: this.getRangePlaceholder()[0], value: this.vegaDatePickerValueController.getValue()[0], ref: (ref) => (this.startDateInputRef = ref), onChange: this.onStartDateChange, disabled: this.disabled, role: "combobox", "aria-label": startAriaLabel, "aria-haspopup": "dialog", "aria-expanded": `${this.isCalendarShow}`, "aria-controls": this.vegaDatePickerCalendarRenderer.getCalendarId(), 
            // eslint-disable-next-line react/jsx-no-bind
            onClick: async () => await this.onInputClicked('start') })),
        h("span", { class: {
            'range-input-divider': true,
          } }),
        h("div", { class: "date-picker-input" },
          h("input", { readOnly: !this.vegaDatePickerRenderer.isInputEnabled(), placeholder: this.getRangePlaceholder()[1], value: this.vegaDatePickerValueController.getValue()[1], ref: (ref) => (this.endDateInputRef = ref), onChange: this.onEndDateChange, disabled: this.disabled, role: "combobox", "aria-label": endAriaLabel, "aria-haspopup": "dialog", "aria-expanded": `${this.isCalendarShow}`, "aria-controls": this.vegaDatePickerCalendarRenderer.getCalendarId(), 
            // eslint-disable-next-line react/jsx-no-bind
            onClick: async () => await this.onInputClicked('end') }),
          this.renderClearIcon())));
    };
    this.getRangePlaceholder = () => {
      if (!this.placeholder) {
        return ['Start Date', 'End Date'].map((item) => this.translationSlimmer.t(item));
      }
      if (!Array.isArray(this.placeholder) || this.placeholder.length !== 2) {
        LogUtility.warn('The placeholder should be an array, including start and end placeholder');
        return ['Start Date', 'End Date'].map((item) => this.translationSlimmer.t(item));
      }
      return this.placeholder;
    };
    /**
     * The below method is e2e-test covered in
     * @see{module:vega-date-picker-start-end-date-change}
     */
    this.onStartDateChange = (e) => {
      const currentTarget = e.currentTarget;
      const date = currentTarget.value;
      if (CalendarDate.fromString(date, this.format)) {
        this.vegaDatePickerValueController.setValue([
          this.vegaDatePickerFormatController.formatDate(date),
          this.vegaDatePickerValueController.getValue()[1],
        ]);
      }
      else {
        this.vegaDatePickerValueController.setValue([
          '',
          this.vegaDatePickerValueController.getValue()[1],
        ]);
        this.startDateInputRef.value = '';
        LogUtility.warn('The date value of ${date} is invalid');
      }
    };
    /**
     * The below method is e2e-test covered in
     * @see{module:vega-date-picker-start-end-date-change}
     */
    this.onEndDateChange = (e) => {
      const currentTarget = e.currentTarget;
      const date = currentTarget.value;
      if (CalendarDate.fromString(date, this.format)) {
        this.vegaDatePickerValueController.setValue([
          this.vegaDatePickerValueController.getValue()[0],
          this.vegaDatePickerFormatController.formatDate(date),
        ]);
      }
      else {
        this.vegaDatePickerValueController.setValue([
          this.vegaDatePickerValueController.getValue()[0],
          '',
        ]);
        this.endDateInputRef.value = '';
        LogUtility.warn('The date value of ${date} is invalid');
      }
    };
  }
  getStartDateInputRef() {
    return this.startDateInputRef;
  }
  getEndDateInputRef() {
    return this.endDateInputRef;
  }
  focusInput() {
    this.startDateInputRef.focus();
  }
  async onInputClicked(type) {
    await this.calendarOpenStateController.showCalendar();
    await this.vegaDatePickerCalendarRenderer.triggerRepick(type);
  }
}
__decorate([
  MapToComponentField()
], VegaDatePickerRangeInputRenderer.prototype, "placeholder", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerRangeInputRenderer.prototype, "label", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerRangeInputRenderer.prototype, "isCalendarShow", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerRangeInputRenderer.prototype, "vegaDatePickerFormatController", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerRangeInputRenderer.prototype, "vegaDatePickerRenderer", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerRangeInputRenderer.prototype, "calendarOpenStateController", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerRangeInputRenderer.prototype, "translationSlimmer", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerRangeInputRenderer.prototype, "allowRepick", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerRangeInputRenderer.prototype, "format", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerRangeInputRenderer.prototype, "vegaDatePickerCalendarRenderer", void 0);
