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 { GridLayoutProvider } from "recyclerlistview";
import { AverageWindow } from "./utils/AverageWindow";
import { applyContentContainerInsetForLayoutManager } from "./utils/ContentContainerUtils";
export default class GridLayoutProviderWithProps extends GridLayoutProvider {
  constructor(maxSpan, getLayoutType, getSpan, getHeightOrWidth, props, acceptableRelayoutDelta) {
    var _props$estimatedItemS;
    super(maxSpan, i => {
      return getLayoutType(i, this.props, this.getCleanLayoutObj());
    }, i => {
      return getSpan(i, this.props, this.getCleanLayoutObj());
    }, i => {
      var _getHeightOrWidth;
      return (// Using average item size if no override has been provided by the developer
        (_getHeightOrWidth = getHeightOrWidth(i, this.props, this.getCleanLayoutObj())) !== null && _getHeightOrWidth !== void 0 ? _getHeightOrWidth : this.averageItemSize
      );
    }, acceptableRelayoutDelta);
    _defineProperty(this, "props", void 0);
    _defineProperty(this, "layoutObject", {
      span: undefined,
      size: undefined
    });
    _defineProperty(this, "averageWindow", void 0);
    _defineProperty(this, "renderWindowInsets", {
      width: 0,
      height: 0
    });
    _defineProperty(this, "_hasExpired", false);
    _defineProperty(this, "defaultEstimatedItemSize", 100);
    this.props = props;
    this.averageWindow = new AverageWindow(1, (_props$estimatedItemS = props.estimatedItemSize) !== null && _props$estimatedItemS !== void 0 ? _props$estimatedItemS : this.defaultEstimatedItemSize);
    this.renderWindowInsets = this.getAdjustedRenderWindowSize(this.renderWindowInsets);
  }
  updateProps(props) {
    this._hasExpired = this.props.numColumns !== props.numColumns;
    const newInsetValues = applyContentContainerInsetForLayoutManager({
      height: 0,
      width: 0
    }, props.contentContainerStyle, Boolean(props.horizontal));
    this._hasExpired = this._hasExpired || newInsetValues.height !== this.renderWindowInsets.height || newInsetValues.width !== this.renderWindowInsets.width;
    this.renderWindowInsets = newInsetValues;
    this.props = props;
    return this;
  }

  /**
   * This methods returns true if the layout provider has expired and needs to be recreated.
   * This can happen if the number of columns has changed or the render window size has changed in a way that cannot be handled by the layout provider internally.
   */
  get hasExpired() {
    return this._hasExpired;
  }

  /**
   * Calling this method will help the layout provider track average item sizes on its own
   * Overriding layout manager can help achieve the same thing without relying on this method being called however, it will make implementation very complex for a simple use case
   * @param index Index of the item being reported
   */
  reportItemLayout(index) {
    var _this$getLayoutManage;
    const layout = (_this$getLayoutManage = this.getLayoutManager()) === null || _this$getLayoutManage === void 0 ? void 0 : _this$getLayoutManage.getLayouts()[index];
    if (layout) {
      // For the same index we can now return different estimates because average is updated in realtime
      // Marking the layout as overridden will help layout manager avoid using the average after initial measurement
      layout.isOverridden = true;
      this.averageWindow.addValue(this.props.horizontal ? layout.width : layout.height);
    }
  }
  get averageItemSize() {
    return this.averageWindow.currentValue;
  }
  newLayoutManager(renderWindowSize, isHorizontal, cachedLayouts) {
    var _this$props$estimated;
    // Average window is updated whenever a new layout manager is created. This is because old values are not relevant anymore.
    const estimatedItemCount = Math.max(3, Math.round((this.props.horizontal ? renderWindowSize.width : renderWindowSize.height) / ((_this$props$estimated = this.props.estimatedItemSize) !== null && _this$props$estimated !== void 0 ? _this$props$estimated : this.defaultEstimatedItemSize)));
    this.averageWindow = new AverageWindow(2 * (this.props.numColumns || 1) * estimatedItemCount, this.averageWindow.currentValue);
    const newLayoutManager = super.newLayoutManager(this.getAdjustedRenderWindowSize(renderWindowSize), isHorizontal, cachedLayouts);
    if (cachedLayouts) {
      this.updateCachedDimensions(cachedLayouts, newLayoutManager);
    }
    return newLayoutManager;
  }
  updateCachedDimensions(cachedLayouts, layoutManager) {
    const layoutCount = cachedLayouts.length;
    for (let i = 0; i < layoutCount; i++) {
      cachedLayouts[i] = {
        ...cachedLayouts[i],
        // helps in updating the fixed dimension of layouts e.g, width in case of horizontal list
        // updating them in advance will make sure layout manager won't try to fit more items in the same row or column
        ...layoutManager.getStyleOverridesForIndex(i)
      };
    }
  }
  getCleanLayoutObj() {
    this.layoutObject.size = undefined;
    this.layoutObject.span = undefined;
    return this.layoutObject;
  }
  getAdjustedRenderWindowSize(renderWindowSize) {
    return applyContentContainerInsetForLayoutManager({
      ...renderWindowSize
    }, this.props.contentContainerStyle, Boolean(this.props.horizontal));
  }
}
//# sourceMappingURL=GridLayoutProviderWithProps.js.map