"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.ScrollState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _events = require("@wix/bex-core/events");
var _mobx = require("mobx");
class ScrollState {
  constructor(params) {
    (0, _defineProperty2.default)(this, "events", new _events.EventEmitter());
    (0, _defineProperty2.default)(this, "scrollListener", void 0);
    (0, _defineProperty2.default)(this, "_container", void 0);
    (0, _defineProperty2.default)(this, "_scrollPosition", void 0);
    (0, _defineProperty2.default)(this, "lodash", void 0);
    (0, _defineProperty2.default)(this, "handleScroll", async () => {
      const {
        clientHeight,
        scrollHeight,
        scrollTop
      } = this._container;
      this._scrollPosition = {
        clientHeight,
        scrollHeight,
        scrollTop
      };
      this.events.emit('scroll');
    });
    this.lodash = params.lodash;
    this._container = params.container;
    this._scrollPosition = this._container ? {
      scrollHeight: this._container.scrollHeight,
      clientHeight: this._container.clientHeight,
      scrollTop: this._container.scrollTop
    } : {
      scrollHeight: 0,
      clientHeight: 0,
      scrollTop: 0
    };
    const delay = Math.max(params.delay || 0, 0);
    this.scrollListener = this.lodash.throttle(this.handleScroll, delay);
    (0, _mobx.makeObservable)(this, {
      _scrollPosition: _mobx.observable.ref
    });
  }
  get scrollPosition() {
    return this._scrollPosition;
  }
  init(params) {
    this._container = params.container ?? this._container;
    if (this._container == null) {
      return;
    }
    this._container.addEventListener('scroll', this.scrollListener);
    return () => {
      this._container.removeEventListener('scroll', this.scrollListener);
    };
  }
}
exports.ScrollState = ScrollState;
//# sourceMappingURL=ScrollState.js.map