/**
 * Updates position on scroll When the target rect changes, we recompute placement/alignment.
 */
export class ScreenScrollPositionUpdate {
  constructor() {
    this.viewportWidth = window.innerWidth;
    this.viewportHeight = window.innerHeight;
  }
  /**
   * Determines whether the appendable element's position should update on scroll.
   *
   * @param {ScrollPositionUpdateStrategyContext} context - Current/previous rectangles plus viewport size.
   * @returns {ScrollPositionUpdateDecision} Decision that indicates whether to recompute position.
   */
  shouldUpdatePosition(context) {
    const { previousTargetRect, currentTargetRect, appendableRect, viewportWidth, viewportHeight, } = context;
    this.viewportWidth = viewportWidth;
    this.viewportHeight = viewportHeight;
    const targetChanged = !previousTargetRect ||
      currentTargetRect.left !== previousTargetRect.left ||
      currentTargetRect.top !== previousTargetRect.top;
    const isTargetWithinViewport = !this.isRectOutsideViewport(currentTargetRect);
    const isAppendableNotEntirelyInViewport = !this.isRectEntirelyWithinViewport(appendableRect);
    return {
      shouldUpdate: targetChanged && isTargetWithinViewport && isAppendableNotEntirelyInViewport,
    };
  }
  /**
   * Whether the rect is fully within the viewport bounds.
   *
   * @param {DOMRect} rect - The DOMRect to check.
   * @returns {boolean} True when rect is fully within the viewport.
   */
  isRectEntirelyWithinViewport(rect) {
    return (rect.left >= 0 &&
      rect.top >= 0 &&
      rect.right <= this.viewportWidth &&
      rect.bottom <= this.viewportHeight);
  }
  /**
   * Whether the element is fully outside the viewport.
   * If true, the rect has "disappeared" from the viewport.
   *
   * @param {DOMRect} rect - The DOMRect to check.
   * @returns {boolean} True when rect is completely outside viewport bounds.
   */
  isRectOutsideViewport(rect) {
    return (rect.right <= 0 ||
      rect.bottom <= 0 ||
      rect.left >= this.viewportWidth ||
      rect.top >= this.viewportHeight);
  }
}
