function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
// @ts-nocheck

import ViewabilityHelper from "./ViewabilityHelper";
/**
 * Manager for viewability tracking. It holds multiple viewability callback pairs and keeps them updated.
 */
export default class ViewabilityManager {
  constructor(flashListRef) {
    var _flashListRef$props$v;
    _defineProperty(this, "flashListRef", void 0);
    _defineProperty(this, "viewabilityHelpers", []);
    _defineProperty(this, "hasInteracted", false);
    _defineProperty(this, "dispose", () => {
      this.viewabilityHelpers.forEach(viewabilityHelper => viewabilityHelper.dispose());
    });
    _defineProperty(this, "onVisibleIndicesChanged", all => {
      this.updateViewableItems(all);
    });
    _defineProperty(this, "recordInteraction", () => {
      if (this.hasInteracted) {
        return;
      }
      this.hasInteracted = true;
      this.viewabilityHelpers.forEach(viewabilityHelper => {
        viewabilityHelper.hasInteracted = true;
      });
      this.updateViewableItems();
    });
    _defineProperty(this, "updateViewableItems", newViewableIndices => {
      var _this$flashListRef$re, _this$flashListRef$re2, _this$flashListRef$re3, _this$flashListRef$re4;
      const listSize = (_this$flashListRef$re = (_this$flashListRef$re2 = this.flashListRef.recyclerlistview_unsafe) === null || _this$flashListRef$re2 === void 0 ? void 0 : _this$flashListRef$re2.getRenderedSize()) !== null && _this$flashListRef$re !== void 0 ? _this$flashListRef$re : this.flashListRef.props.estimatedListSize;
      if (listSize === undefined || !this.shouldListenToVisibleIndices) {
        return;
      }
      const scrollOffset = ((_this$flashListRef$re3 = (_this$flashListRef$re4 = this.flashListRef.recyclerlistview_unsafe) === null || _this$flashListRef$re4 === void 0 ? void 0 : _this$flashListRef$re4.getCurrentScrollOffset()) !== null && _this$flashListRef$re3 !== void 0 ? _this$flashListRef$re3 : 0) - this.flashListRef.firstItemOffset;
      this.viewabilityHelpers.forEach(viewabilityHelper => {
        var _this$flashListRef$pr;
        viewabilityHelper.updateViewableItems((_this$flashListRef$pr = this.flashListRef.props.horizontal) !== null && _this$flashListRef$pr !== void 0 ? _this$flashListRef$pr : false, scrollOffset, listSize, index => {
          var _this$flashListRef$re5;
          return (_this$flashListRef$re5 = this.flashListRef.recyclerlistview_unsafe) === null || _this$flashListRef$re5 === void 0 ? void 0 : _this$flashListRef$re5.getLayout(index);
        }, newViewableIndices);
      });
    });
    /**
     * Creates a new `ViewabilityHelper` instance with `onViewableItemsChanged` callback and `ViewabilityConfig`
     * @returns `ViewabilityHelper` instance
     */
    _defineProperty(this, "createViewabilityHelper", (viewabilityConfig, onViewableItemsChanged) => {
      const mapViewToken = (index, isViewable) => {
        var _this$flashListRef$pr2;
        const item = (_this$flashListRef$pr2 = this.flashListRef.props.data) === null || _this$flashListRef$pr2 === void 0 ? void 0 : _this$flashListRef$pr2[index];
        const key = item === undefined || this.flashListRef.props.keyExtractor === undefined ? index.toString() : this.flashListRef.props.keyExtractor(item, index);
        return {
          index,
          isViewable,
          item,
          key,
          timestamp: Date.now()
        };
      };
      return new ViewabilityHelper(viewabilityConfig, (indices, newlyVisibleIndices, newlyNonvisibleIndices) => {
        onViewableItemsChanged === null || onViewableItemsChanged === void 0 ? void 0 : onViewableItemsChanged({
          viewableItems: indices.map(index => mapViewToken(index, true)),
          changed: [...newlyVisibleIndices.map(index => mapViewToken(index, true)), ...newlyNonvisibleIndices.map(index => mapViewToken(index, false))]
        });
      });
    });
    this.flashListRef = flashListRef;
    if (flashListRef.props.onViewableItemsChanged !== null && flashListRef.props.onViewableItemsChanged !== undefined) {
      this.viewabilityHelpers.push(this.createViewabilityHelper(flashListRef.props.viewabilityConfig, flashListRef.props.onViewableItemsChanged));
    }
    ((_flashListRef$props$v = flashListRef.props.viewabilityConfigCallbackPairs) !== null && _flashListRef$props$v !== void 0 ? _flashListRef$props$v : []).forEach(pair => {
      this.viewabilityHelpers.push(this.createViewabilityHelper(pair.viewabilityConfig, pair.onViewableItemsChanged));
    });
  }

  /**
   * @returns true if the viewability manager has any viewability callback pairs registered.
   */
  get shouldListenToVisibleIndices() {
    return this.viewabilityHelpers.length > 0;
  }
}
//# sourceMappingURL=ViewabilityManager.js.map