var global = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof self !== "undefined" ? self : {};
import {
  isWithinPaddingHorizontally,
  isWithinPaddingVertically
} from "./chunk-ANOFRNLZ.mjs";
import {
  constants_default,
  windowWrapper_default
} from "./chunk-D3Y2XBAR.mjs";
import {
  init_define_process_env
} from "./chunk-FOPGQED5.mjs";

// ../../node_modules/@wix/showcase-organize-media/node_modules/pro-gallery/dist/esm/components/helpers/videoScrollHelper.js
init_define_process_env();
import { __spreadArray } from "tslib";
var VIDEO_EVENTS = {
  SCROLL: "SCROLL",
  CLICKED: "CLICKED",
  HOVERED: "HOVERED",
  ENDED: "ENDED",
  INIT_SCROLL: "INIT_SCROLL"
};
var VideoScrollHelper = (
  /** @class */
  function() {
    function VideoScrollHelper2(config) {
      var _this = this;
      this.scrollBase = 0;
      this.videoItems = [];
      this.currentPlayingIdx = -1;
      this.currentItemCount = 0;
      this.playing = false;
      this.updateGalleryStructure = this.updateGalleryStructure.bind(this);
      this.initializePlayState = this.initializePlayState.bind(this);
      this.onScroll = this.onScroll.bind(this);
      this.handleEvent = this.handleEvent.bind(this);
      this.play = this.play.bind(this);
      this.stop = this.stop.bind(this);
      this.isVisible = this.isVisible.bind(this);
      this.isCurrentVideoStillVisible = this.isCurrentVideoStillVisible.bind(this);
      this.videoPlay = void 0;
      this.itemClick = void 0;
      this.setPlayingVideos = config.setPlayingVideos;
      this.lastVideoPlayed = -1;
      this.videoRatingMap = /* @__PURE__ */ new Map();
      this.trigger = Object.assign.apply(Object, __spreadArray([{}], Object.keys(VIDEO_EVENTS).map(function(key) {
        var _a;
        return _a = {}, _a[key] = function(args) {
          return _this.handleEvent({ eventName: key, eventData: args });
        }, _a;
      }), false));
    }
    VideoScrollHelper2.prototype.updateGalleryStructure = function(_a) {
      var _this = this;
      var galleryStructure = _a.galleryStructure, galleryWidth = _a.galleryWidth, scrollBase = _a.scrollBase, videoPlay = _a.videoPlay, videoLoop = _a.videoLoop, itemClick = _a.itemClick, scrollDirection = _a.scrollDirection;
      this.galleryWidth = galleryWidth;
      this.scrollBase = scrollBase;
      this.videoPlay = videoPlay;
      this.videoLoop = videoLoop;
      this.itemClick = itemClick;
      this.scrollDirection = scrollDirection;
      this.currentItemCount = galleryStructure.galleryItems.length;
      this.videoItems = [];
      galleryStructure.galleryItems.forEach(function(item) {
        if (item.type === "video" || item.type === "image" && (item.id.includes("_placeholder") || item.isVideoPlaceholder)) {
          if (!_this.videoRatingMap.has(item.id)) {
            _this.videoRatingMap.set(item.id, item.idx);
          }
          _this.videoItems.push(item);
        }
      });
    };
    VideoScrollHelper2.prototype.handleEvent = function(_a) {
      var eventName = _a.eventName, eventData = _a.eventData;
      switch (eventName) {
        case VIDEO_EVENTS.SCROLL:
          this.onScroll(eventData);
          break;
        case constants_default.events.ITEM_ACTION_TRIGGERED:
          this.itemClicked(eventData.idx);
          break;
        case constants_default.events.VIDEO_PAUSED:
          if (this.currentPlayingIdx === eventData.idx) {
            this.stop(eventData.idx);
          }
          break;
        case constants_default.events.HOVER_SET:
          this.itemHovered(eventData);
          break;
        case constants_default.events.HOVER_UNSET:
          this.itemHoveredUnset(eventData);
          break;
        case constants_default.events.VIDEO_ENDED:
          this.videoEnded(eventData.idx);
          break;
        case constants_default.events.VIDEO_PLAYED:
          this.videoPlayed(eventData.idx);
          break;
        case constants_default.events.VIDEO_ERROR:
          this.videoErrorReported();
          break;
        case VIDEO_EVENTS.INIT_SCROLL:
          break;
        default:
      }
    };
    VideoScrollHelper2.prototype.itemHovered = function(idx) {
      if (this.videoPlay !== "hover")
        return;
      if (this.IdxExistsInVideoItems(idx)) {
        this.play(idx);
      } else {
      }
    };
    VideoScrollHelper2.prototype.itemHoveredUnset = function(idx) {
      if (this.videoPlay !== "hover")
        return;
      if (this.IdxExistsInVideoItems(idx)) {
        if (this.currentPlayingIdx === idx) {
          this.stop(idx);
        }
      }
    };
    VideoScrollHelper2.prototype.itemClicked = function(idx) {
      if (this.videoPlay !== "onClick")
        return;
      if (this.IdxExistsInVideoItems(idx)) {
        if (this.currentPlayingIdx === idx) {
          this.stop();
        } else {
          this.play(idx);
        }
      } else {
      }
    };
    VideoScrollHelper2.prototype.onScroll = function(_a) {
      var _this = this;
      var top = _a.top, left = _a.left;
      this.top = top >= 0 ? top : this.top;
      this.left = left >= 0 ? left : this.left;
      if (this.currentPlayingIdx === -1) {
        this.autoPlayNextVideoByRating({ top: this.top, left: this.left });
      } else {
        if (!this.isCurrentVideoStillVisible({ top: this.top, left: this.left })) {
          this.stop(this.videoItems.findIndex(function(item) {
            return item.idx === _this.currentPlayingIdx;
          }));
        }
        this.autoPlayNextVideoByRating({ top: this.top, left: this.left });
      }
    };
    VideoScrollHelper2.prototype.videoEnded = function(idx) {
      var indexInVideoItems = this.videoItems.findIndex(function(item) {
        return item.idx === idx;
      });
      this.stop(indexInVideoItems);
      var scroll = { top: this.top, left: this.left };
      this.autoPlayNextVideoByRating(scroll);
    };
    VideoScrollHelper2.prototype.videoPlayed = function(idx) {
      if (this.currentPlayingIdx !== idx) {
        this.play(idx);
      }
      this.lastVideoPlayed = idx;
    };
    VideoScrollHelper2.prototype.videoErrorReported = function() {
      this.stop();
    };
    VideoScrollHelper2.prototype.initializePlayState = function() {
      this.autoPlayNextVideoByRating({ top: this.top, left: this.left });
    };
    VideoScrollHelper2.prototype.autoPlayNextVideoByRating = function(_a) {
      var _this = this;
      var top = _a.top, left = _a.left;
      if (!this.shouldAutoPlay()) {
        return;
      }
      var secondBestRating = {
        idx: -1,
        rating: Infinity
      };
      var bestRating = {
        idx: -1,
        rating: Infinity
      };
      this.videoItems.some(function(item) {
        if (_this.isVisible(item, { top, left })) {
          var itemRating = _this.videoRatingMap.get(item.id);
          if (itemRating <= bestRating.rating) {
            secondBestRating.idx = bestRating.idx;
            secondBestRating.rating = bestRating.rating;
            bestRating.idx = item.idx;
            bestRating.rating = itemRating;
          } else if (itemRating <= secondBestRating.rating) {
            secondBestRating.idx = item.idx;
            secondBestRating.rating = itemRating;
          }
          return false;
        } else {
          if (bestRating.idx >= 0) {
            return true;
          }
          return false;
        }
      });
      if (bestRating.idx >= 0) {
        if (!this.allowedLoop() && bestRating.idx === this.lastVideoPlayed) {
          if (secondBestRating.idx >= 0) {
            this.play(secondBestRating.idx);
          } else {
            return;
          }
        } else {
          this.play(bestRating.idx);
        }
      } else {
        this.lastVideoPlayed = -2;
      }
    };
    VideoScrollHelper2.prototype.calculateCurrentItemPlacement = function() {
      var _this = this;
      return this.videoItems.findIndex(function(item) {
        return item.idx === _this.currentPlayingIdx;
      });
    };
    VideoScrollHelper2.prototype.play = function(idx) {
      this.setPlayingIdx(idx);
      this.playing = true;
    };
    VideoScrollHelper2.prototype.stop = function(indexInVideoItems) {
      var _a;
      if (indexInVideoItems >= 0) {
        var currentVideoId = (_a = this.videoItems.find(function(item) {
          return item.idx === indexInVideoItems;
        })) === null || _a === void 0 ? void 0 : _a.id;
        var newRating = this.videoRatingMap.get(currentVideoId) + this.currentItemCount;
        this.videoRatingMap.set(currentVideoId, newRating);
      }
      this.setPlayingIdx(-1);
      this.playing = false;
    };
    VideoScrollHelper2.prototype.onPlayingIdxChange = function() {
      this.setPlayingVideos(this.currentPlayingIdx);
    };
    VideoScrollHelper2.prototype.setPlayingIdx = function(idx) {
      if (this.currentPlayingIdx !== idx) {
        this.currentPlayingIdx = idx;
        this.onPlayingIdxChange();
      }
    };
    VideoScrollHelper2.prototype.isCurrentVideoStillVisible = function(_a) {
      var top = _a.top, left = _a.left;
      var currentItemPlacement = this.calculateCurrentItemPlacement();
      var item = this.videoItems[currentItemPlacement];
      return item ? this.isVisible(item, { top, left }) : false;
    };
    VideoScrollHelper2.prototype.isVisible = function(item, _a) {
      var top = _a.top, left = _a.left;
      var target = {
        offsetTop: this.scrollBase || 0,
        scrollY: top,
        scrollLeft: left
      };
      var videoPlayVerticalTolerance = (item.offset.top - item.offset.bottom) / 2;
      var videoPlayHorizontalTolerance = (item.offset.left - item.offset.right) / 2;
      var visibleVertically = isWithinPaddingVertically({
        target,
        scrollBase: this.scrollBase,
        top: item.offset.top,
        bottom: item.offset.top + item.style.height,
        screenHeight: windowWrapper_default && windowWrapper_default.innerHeight,
        padding: videoPlayVerticalTolerance
      });
      var visibleHorizontally;
      if (this.scrollDirection === constants_default.scrollDirection.VERTICAL) {
        visibleHorizontally = true;
      } else {
        visibleHorizontally = isWithinPaddingHorizontally({
          target,
          left: item.offset.left,
          right: item.offset.left + item.style.width,
          screenWidth: this.galleryWidth || windowWrapper_default && windowWrapper_default.innerWidth,
          padding: videoPlayHorizontalTolerance
        });
      }
      return visibleVertically && visibleHorizontally;
    };
    VideoScrollHelper2.prototype.shouldAutoPlay = function() {
      return this.videoPlay === "auto";
    };
    VideoScrollHelper2.prototype.allowedLoop = function() {
      return this.videoLoop === true;
    };
    VideoScrollHelper2.prototype.IdxExistsInVideoItems = function(idx) {
      return this.videoItems.some(function(item) {
        return item.idx === idx;
      });
    };
    return VideoScrollHelper2;
  }()
);
var videoScrollHelper_default = VideoScrollHelper;
export {
  videoScrollHelper_default as default
};
//# sourceMappingURL=videoScrollHelper-HX35Z6JI.mjs.map