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 { MapToComponentField, MapToComponentMethod, VegaSlimmer } from 'vega-slimmer/core';
import { h } from '@stencil/core';
import { FeatureFlag } from '../../../../helpers/feature-control/feature-flag-controller';
import { DomNodeSubjectObserverFactory } from '../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-observer-factory';
import { VegaClear, VegaClose, VegaOpen, VegaMonthYearChange, VegaSelectionChange, } from '../../../../helpers/event-manager/event-id/vega-event-id';
import { tryGetDocument } from '../../../../utils/try-get-document';
import { generateUUID } from '../../../../utils/misc';
export class VegaDatePickerCalendarRenderer extends VegaSlimmer {
  constructor() {
    super(...arguments);
    this.calendarId = generateUUID();
  }
  componentDidLoad() {
    this.appendCalendarToBodyIfMobile();
  }
  getCalendarContentBoxRef() {
    return this.calendarContentBoxRef;
  }
  getCalendarRef() {
    return this.calendarRef;
  }
  getCalendarId() {
    return this.calendarId;
  }
  getMobileModalRef() {
    return this.mobileModalRef;
  }
  async triggerRepick(type) {
    if (this.allowRepick && this.mode === 'range') {
      await this.calendarRef.triggerRepick(type);
    }
  }
  render() {
    return this.isMobileDevice ? this.mobileRender() : this.renderCalendar();
  }
  mobileRender() {
    return (h("vega-modal", { showCloseButton: false, size: VegaDatePickerCalendarRenderer.calendarModalSize, padding: "0", isVerticallyCentered: true, backdropColor: "semi", ref: (ref) => {
        this.mobileModalRef = ref;
        this.addModalObserver(ref);
      } }, h("div", { slot: "modal-content" }, this.renderCalendar())));
  }
  renderCalendar() {
    const label = this.label ? `${this.label}, ` : '';
    const picker = `${this.translationSlimmer.t('date picker')}`;
    const ariaAttributes = this.isMobileDevice
      ? {}
      : {
        'role': 'dialog',
        'aria-modal': 'true',
        'aria-label': `${label}${picker}`,
      };
    return (h("vega-box", Object.assign({}, ariaAttributes, { id: this.calendarId, padding: {
        default: 'size-12',
        S: 'size-32',
      }, corners: "rounded-12", backgroundColor: "bg-primary", shadow: "shadow-dropdown-menu", border: "border-input-field", ref: (ref) => {
        this.calendarContentBoxRef = ref;
      }, class: "date-picker-content-box" }),
      h("vega-box", { width: this.getDefaultCalendarDimension('width'), height: this.getDefaultCalendarDimension('height') },
        h("vega-date-picker-calendar", { selection: this.value, mode: this.mode, minDate: this.minDate, maxDate: this.maxDate, clearButton: this.clearButton, format: this.format, staticHeight: this.dropdownConfig.staticHeight !== false, showYearMonthDropdowns: this.showYearMonthDropdowns, timezone: this.timezone, yearMonthDropdownProps: {
            isScreenPosition: FeatureFlag.isEnabled('VEGA_DATE_PICKER.ENABLE_CALCULATION_POSITION_BY_SCREEN'),
          }, ref: (ref) => {
            this.calendarRef = ref;
            this.addCalendarObserver(ref);
          }, 
          /* eslint-disable-next-line react/jsx-no-bind */
          isDateDisabled: (date) => this.isDateDisabled(date) }))));
  }
  addCalendarObserver(calendarRef) {
    DomNodeSubjectObserverFactory.addUniqueObserverToNode(calendarRef, VegaSelectionChange, async (payload) => {
      await this.onCalendarSelected(payload.detail);
    });
    DomNodeSubjectObserverFactory.addUniqueObserverToNode(calendarRef, VegaClear, async () => {
      await this.calendarOpenStateController.hideCalendar();
    });
    DomNodeSubjectObserverFactory.addUniqueObserverToNode(calendarRef, VegaMonthYearChange, async (payload) => {
      this.monthYearChangeEventEmitter.emit(payload.detail);
    });
  }
  addModalObserver(modalRef) {
    DomNodeSubjectObserverFactory.addUniqueObserverToNode(modalRef, VegaOpen, () => {
      this.calendarOpenStateController.updateIsCalendarShowState(true);
    });
    DomNodeSubjectObserverFactory.addUniqueObserverToNode(modalRef, VegaClose, () => {
      this.calendarOpenStateController.updateIsCalendarShowState(false);
    });
  }
  async onCalendarSelected(value) {
    if (this.mode === 'single') {
      this.vegaDatePickerValueController.setValue(value);
    }
    else if (this.mode === 'range') {
      this.vegaDatePickerValueController.setValue(value);
    }
    if (this.isCalendarShow) {
      await this.calendarOpenStateController.hideCalendar();
    }
  }
  appendCalendarToBodyIfMobile() {
    const document = tryGetDocument();
    if (this.isMobileDevice && document) {
      document.body.append(this.mobileModalRef);
    }
  }
  getDefaultCalendarDimension(type) {
    const dimensions = this.mode === 'range' && !this.isMobileDevice
      ? VegaDatePickerCalendarRenderer.regularDatePickerCalendarDimensionsForRange
      : VegaDatePickerCalendarRenderer.regularDatePickerCalendarDimensionsForSingle;
    // Height can be dynamic
    if (type === 'height' && this.dropdownConfig.staticHeight === false) {
      return 'auto';
    }
    let value = dimensions[type];
    // Extra space for footer when height is static
    if (type === 'height' && this.clearButton) {
      value += 40; // 24px footer + 16px margin
    }
    return `${value}px`;
  }
}
VegaDatePickerCalendarRenderer.calendarModalSize = 344;
VegaDatePickerCalendarRenderer.regularDatePickerCalendarDimensionsForSingle = {
  width: 280,
  height: 320,
};
VegaDatePickerCalendarRenderer.regularDatePickerCalendarDimensionsForRange = {
  width: 608,
  height: 358,
};
__decorate([
  MapToComponentField()
], VegaDatePickerCalendarRenderer.prototype, "format", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerCalendarRenderer.prototype, "value", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerCalendarRenderer.prototype, "mode", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerCalendarRenderer.prototype, "minDate", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerCalendarRenderer.prototype, "maxDate", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerCalendarRenderer.prototype, "clearButton", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerCalendarRenderer.prototype, "dropdownConfig", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerCalendarRenderer.prototype, "showYearMonthDropdowns", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerCalendarRenderer.prototype, "timezone", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerCalendarRenderer.prototype, "isMobileDevice", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerCalendarRenderer.prototype, "isCalendarShow", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerCalendarRenderer.prototype, "isDateDisabled", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerCalendarRenderer.prototype, "allowRepick", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerCalendarRenderer.prototype, "disabled", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerCalendarRenderer.prototype, "label", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerCalendarRenderer.prototype, "monthYearChangeEventEmitter", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerCalendarRenderer.prototype, "translationSlimmer", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerCalendarRenderer.prototype, "vegaDatePickerValueController", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerCalendarRenderer.prototype, "calendarOpenStateController", void 0);
__decorate([
  MapToComponentMethod('componentDidLoad')
], VegaDatePickerCalendarRenderer.prototype, "componentDidLoad", null);
