"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.VirtualTableRowMutatedState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _bexCore = require("@wix/bex-core");
var _events = require("events");
class VirtualTableRowMutatedState {
  constructor(params) {
    (0, _defineProperty2.default)(this, "state", void 0);
    (0, _defineProperty2.default)(this, "rowRef", void 0);
    (0, _defineProperty2.default)(this, "itemKey", void 0);
    (0, _defineProperty2.default)(this, "index", void 0);
    (0, _defineProperty2.default)(this, "rowIndex", void 0);
    (0, _defineProperty2.default)(this, "events", new _events.EventEmitter());
    this.state = params.state;
    this.rowRef = params.rowRef;
    this.itemKey = params.itemKey;
    this.index = params.index;
    this.rowIndex = params.rowIndex;
  }
  _measureRowContentsHeight() {
    const {
      state,
      rowRef,
      itemKey,
      index
    } = this;
    const rowElement = rowRef.current;
    if (rowElement == null || state.calcGridProps.rowHeight != null || itemKey == null || state.setVariableRowHeight == null) {
      return;
    }
    rowElement.style.height = 'auto';
    state.setVariableRowHeight({
      key: itemKey(index),
      size: rowElement.offsetHeight
    });
    const totalHeight = state.calcGridProps.rowHeightIncludingGap({
      index,
      key: itemKey(index)
    });
    rowElement.style.height = `${totalHeight}px`;
  }
  setChildrenResizeObservers() {
    const {
      rowRef,
      state
    } = this;
    const {
      container: {
        lodash
      }
    } = state;
    const rowElement = rowRef.current;
    if (!rowElement) {
      return;
    }
    const disposers = Array.from(rowElement.childNodes).filter(td => td instanceof HTMLElement).map(td => {
      return (0, _bexCore.addResizeObserver)(td, lodash.throttle(() => this._measureRowContentsHeight(), 100));
    });
    return () => {
      disposers.forEach(disposer => disposer == null ? void 0 : disposer());
    };
  }
  _disposeResizeObservers() {
    const {
      events
    } = this;
    const disposers = events.listeners('disposeResizeObservers');
    events.removeAllListeners('disposeResizeObservers');
    disposers.forEach(disposer => disposer == null ? void 0 : disposer());
  }
  init() {
    const {
      rowRef,
      events
    } = this;
    const rowElement = rowRef.current;
    if (!rowElement) {
      return;
    }
    const resetChildrenResizeObservers = () => {
      this._disposeResizeObservers();
      events.on('disposeResizeObservers', this.setChildrenResizeObservers());
    };
    resetChildrenResizeObservers();
    const disposers = [(0, _bexCore.addMutationObserver)(rowElement, resetChildrenResizeObservers)];
    return () => {
      this._disposeResizeObservers();
      disposers.forEach(disposer => disposer == null ? void 0 : disposer());
    };
  }
}
exports.VirtualTableRowMutatedState = VirtualTableRowMutatedState;
//# sourceMappingURL=VirtualTableRowMutatedState.js.map