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 ChangeManager from '../../../../helpers/change-manager/change-manager';
import { SHADOW_ROOT_ELEMENT_SCROLLING } from '../../../../helpers/change-manager/subject/static-subject/static-subject-title';
/**
 * Modal scroll controller
 */
export class VegaModalScrollController extends VegaSlimmer {
  constructor() {
    super(...arguments);
    this.scrollEventListenerAdded = false;
  }
  /**
   * Component lifecycle - [connectedCallback]
   */
  componentDidLoad() {
    if (this.isContentScrollable()) {
      this.listenContentScroll();
    }
  }
  /**
   * Listen watchContentMaxHeight method to be called
   */
  watchContentMaxHeight() {
    if (!this.scrollEventListenerAdded && this.isContentScrollable()) {
      this.listenContentScroll();
    }
  }
  /**
   * Is modal content scrollable.
   *
   * @returns {boolean} - Is scrollable.
   */
  isContentScrollable() {
    return Boolean(this.contentMaxHeight);
  }
  /*
   * The below method is e2e-test covered in
   * @see{module:vega-modal-content-scroll-to-bottom}
   */
  /* istanbul ignore next */
  /**
   * Listen content scroll
   */
  listenContentScroll() {
    const modalContent = this.renderer.getModalContentRef();
    const modalFooter = this.renderer.getModalFooterRef();
    modalContent.addEventListener('scroll', () => {
      if (modalContent.scrollHeight - modalContent.scrollTop <= modalContent.clientHeight) {
        modalFooter.classList.add('vega-modal-footer-with-content-scroll-to-bottom');
      }
      else {
        modalFooter.classList.remove('vega-modal-footer-with-content-scroll-to-bottom');
      }
      this.notifyContentScrolling();
    });
    this.scrollEventListenerAdded = true;
  }
  /*
   * The below method is e2e-test covered in
   * @see{module:vega-modal-shadow-root-scrolling-e2e}
   */
  /* istanbul ignore next */
  /**
   * Notifies the `ChangeManager` about scrolling in a specific element within a shadow root.
   */
  notifyContentScrolling() {
    ChangeManager.notify(SHADOW_ROOT_ELEMENT_SCROLLING, {
      host: this.host,
    });
  }
}
__decorate([
  MapToComponentField()
], VegaModalScrollController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaModalScrollController.prototype, "contentMaxHeight", void 0);
__decorate([
  MapToComponentField()
], VegaModalScrollController.prototype, "renderer", void 0);
__decorate([
  MapToComponentMethod('componentDidLoad')
], VegaModalScrollController.prototype, "componentDidLoad", null);
__decorate([
  MapToComponentMethod('watchContentMaxHeight')
], VegaModalScrollController.prototype, "watchContentMaxHeight", null);
