var global = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof self !== "undefined" ? self : {};
import {
  constants_default,
  utils_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/scrollHelper.js
init_define_process_env();

// ../../node_modules/@wix/showcase-organize-media/node_modules/pro-gallery/dist/esm/components/gallery/proGallery/galleryHelpers.js
init_define_process_env();
var Deferred = (
  /** @class */
  /* @__PURE__ */ function() {
    function Deferred2() {
      var _this = this;
      this.promise = new Promise(function(resolve, reject) {
        _this.reject = function() {
          var args = [];
          for (var _i = 0; _i < arguments.length; _i++) {
            args[_i] = arguments[_i];
          }
          _this.isPending = false;
          reject.apply(void 0, args);
        };
        _this.resolve = function() {
          var args = [];
          for (var _i = 0; _i < arguments.length; _i++) {
            args[_i] = arguments[_i];
          }
          _this.isPending = false;
          resolve.apply(void 0, args);
        };
        _this.isPending = true;
      });
    }
    return Deferred2;
  }()
);
function isGalleryInViewport(_a) {
  var container = _a.container, scrollTop = _a.scrollTop;
  try {
    var isTopVisible = container.scrollBase < scrollTop + windowWrapper_default.innerHeight;
    var isBottomVisible = container.scrollBase + container.galleryHeight > scrollTop;
    return isTopVisible && isBottomVisible;
  } catch (e) {
    console.warn("Could not calculate viewport", e);
    return true;
  }
}

// ../../node_modules/@wix/showcase-organize-media/node_modules/pro-gallery/dist/esm/components/helpers/scrollHelper.js
function scrollToItemImp(scrollParams) {
  var to, from;
  var _a = scrollParams.scrollMarginCorrection, scrollMarginCorrection = _a === void 0 ? 0 : _a, durationInMS = scrollParams.durationInMS, horizontalElement = scrollParams.horizontalElement, scrollingElement = scrollParams.scrollingElement, isRTL = scrollParams.isRTL, scrollDirection = scrollParams.scrollDirection, galleryWidth = scrollParams.galleryWidth, galleryHeight = scrollParams.galleryHeight, totalWidth = scrollParams.totalWidth, top = scrollParams.top, items = scrollParams.items, itemIdx = scrollParams.itemIdx, fixedScroll = scrollParams.fixedScroll, slideTransition = scrollParams.slideTransition, isContinuousScrolling = scrollParams.isContinuousScrolling, autoSlideshowContinuousSpeed = scrollParams.autoSlideshowContinuousSpeed, imageMargin = scrollParams.imageMargin;
  var rtlFix = isRTL ? -1 : 1;
  if (scrollDirection === constants_default.scrollDirection.HORIZONTAL) {
    from = horizontalElement.scrollLeft * rtlFix;
    to = from + itemIdx * galleryWidth / 2;
  } else {
    from = top;
    to = top + itemIdx * galleryHeight / 2;
  }
  if (fixedScroll !== true) {
    if (utils_default.isVerbose()) {
      console.log("Scrolling to items #" + itemIdx);
    }
    var item = items.find(function(itm) {
      return itm.idx === itemIdx;
    });
    to = scrollDirection === constants_default.scrollDirection.HORIZONTAL ? utils_default.get(item, "offset.left") : utils_default.get(item, "offset.top");
    if (utils_default.isVerbose()) {
      console.log("Scrolling to position " + to, item);
    }
    if (!(to >= 0)) {
      utils_default.isVerbose() && console.warn("Position not found, not scrolling");
      return new Promise(function(res) {
        return res();
      });
    }
    if (scrollDirection === constants_default.scrollDirection.HORIZONTAL) {
      var diff = (galleryWidth - item.width - imageMargin) / 2;
      to -= diff;
      to = Math.max(0, to);
      to = Math.min(to, totalWidth - galleryWidth + scrollMarginCorrection);
      to *= rtlFix;
      from *= rtlFix;
      if (utils_default.isVerbose()) {
        console.log("Scrolling to new position " + to, this);
      }
    }
  }
  if (scrollDirection === constants_default.scrollDirection.HORIZONTAL) {
    return horizontalCssScrollTo({
      scroller: horizontalElement,
      from: Math.round(from),
      to: Math.round(to),
      duration: durationInMS,
      isRTL,
      slideTransition,
      isContinuousScrolling,
      autoSlideshowContinuousSpeed
    });
  } else {
    return new Promise(function(resolve) {
      scrollingElement.vertical().scrollTo(0, to);
      resolve(to);
    });
  }
}
function scrollToGroupImp(scrollParams) {
  var to, from;
  var _a = scrollParams.scrollMarginCorrection, scrollMarginCorrection = _a === void 0 ? 0 : _a, durationInMS = scrollParams.durationInMS, horizontalElement = scrollParams.horizontalElement, scrollingElement = scrollParams.scrollingElement, isRTL = scrollParams.isRTL, scrollDirection = scrollParams.scrollDirection, galleryWidth = scrollParams.galleryWidth, galleryHeight = scrollParams.galleryHeight, totalWidth = scrollParams.totalWidth, top = scrollParams.top, groups = scrollParams.groups, groupIdx = scrollParams.groupIdx, fixedScroll = scrollParams.fixedScroll, slideTransition = scrollParams.slideTransition, isContinuousScrolling = scrollParams.isContinuousScrolling, autoSlideshowContinuousSpeed = scrollParams.autoSlideshowContinuousSpeed, imageMargin = scrollParams.imageMargin;
  var rtlFix = isRTL ? -1 : 1;
  if (scrollDirection === constants_default.scrollDirection.HORIZONTAL && horizontalElement) {
    from = horizontalElement.scrollLeft;
    to = from + groupIdx * galleryWidth / 2;
  } else {
    from = top;
    to = top + groupIdx * galleryHeight / 2;
  }
  if (fixedScroll !== true) {
    if (utils_default.isVerbose()) {
      console.log("Scrolling to groups #" + groupIdx);
    }
    var group = groups.find(function(grp) {
      return grp.idx === groupIdx;
    });
    to = scrollDirection === constants_default.scrollDirection.HORIZONTAL ? utils_default.get(group, "left") : utils_default.get(group, "top");
    if (utils_default.isVerbose()) {
      console.log("Scrolling to position " + to, group);
    }
    if (!(to >= 0)) {
      utils_default.isVerbose() && console.warn("Position not found, not scrolling");
      return new Promise(function(res) {
        return res();
      });
    }
    if (scrollDirection === constants_default.scrollDirection.HORIZONTAL) {
      var diff = (galleryWidth - group.width - imageMargin) / 2;
      to -= diff;
      to = Math.max(0, to);
      to = Math.min(to, totalWidth - galleryWidth + scrollMarginCorrection);
      to *= rtlFix;
      if (utils_default.isVerbose()) {
        console.log("Scrolling to new position " + to, this);
      }
    }
  }
  if (scrollDirection === constants_default.scrollDirection.HORIZONTAL) {
    return horizontalCssScrollTo({
      scroller: horizontalElement,
      from: Math.round(from),
      to: Math.round(to),
      duration: durationInMS,
      isRTL,
      slideTransition,
      isContinuousScrolling,
      autoSlideshowContinuousSpeed
    });
  } else {
    return new Promise(function(resolve) {
      scrollingElement.vertical().scrollTo(0, to);
      resolve(to);
    });
  }
}
function getDistanceFromScreen(_a) {
  var offset = _a.offset, scroll = _a.scroll, itemStart = _a.itemStart, itemEnd = _a.itemEnd, screenSize = _a.screenSize;
  var before = scroll - offset - itemEnd;
  var after = offset + itemStart - screenSize - scroll;
  return { before, after };
}
function isWithinPaddingVertically(_a) {
  var target = _a.target, scrollBase = _a.scrollBase, top = _a.top, bottom = _a.bottom, screenHeight = _a.screenHeight, padding = _a.padding;
  var res = getDistanceFromScreen({
    offset: scrollBase || 0,
    scroll: target.scrollY,
    itemStart: top,
    itemEnd: bottom,
    screenSize: screenHeight
  });
  return res.before < padding && res.after < padding;
}
function isWithinPaddingHorizontally(_a) {
  var target = _a.target, left = _a.left, right = _a.right, screenWidth = _a.screenWidth, padding = _a.padding;
  var res = getDistanceFromScreen({
    offset: 0,
    scroll: target.scrollLeft,
    itemStart: left,
    itemEnd: right,
    screenSize: screenWidth
  });
  return res.before < padding && res.after < padding;
}
function horizontalCssScrollTo(_a) {
  var scroller = _a.scroller, from = _a.from, to = _a.to, duration = _a.duration, isRTL = _a.isRTL, slideTransition = _a.slideTransition, isContinuousScrolling = _a.isContinuousScrolling, autoSlideshowContinuousSpeed = _a.autoSlideshowContinuousSpeed;
  var change = to - from;
  if (change === 0) {
    return new Promise(function(resolve) {
      return resolve(to);
    });
  }
  if (isContinuousScrolling) {
    duration = Math.abs(change) / autoSlideshowContinuousSpeed * 1e3;
  }
  var scrollerInner = scroller.firstChild;
  scroller.setAttribute("data-scrolling", "true");
  Object.assign(scroller.style, {
    "scroll-snap-type": "none"
  });
  Object.assign(scrollerInner.style, {
    transition: "transform ".concat(duration, "ms ").concat(slideTransition),
    "-webkit-transition": "transform ".concat(duration, "ms ").concat(slideTransition)
  }, {
    transform: "translateX(".concat(-1 * change, "px)")
  });
  var intervals = 10;
  var scrollTransitionEvent = new CustomEvent("scrollTransition", {
    detail: change / intervals
  });
  var scrollTransitionInterval = setInterval(function() {
    scroller.dispatchEvent(scrollTransitionEvent);
  }, Math.round(duration / intervals));
  var scrollDeffered = new Deferred();
  var currentScrollEndTimeout = setTimeout(function() {
    clearInterval(scrollTransitionInterval);
    Object.assign(scrollerInner.style, {
      transition: "none",
      "-webkit-transition": "none"
    }, {
      transform: "translateX(0px)"
    });
    scroller.style.removeProperty("scroll-snap-type");
    scroller.scrollLeft = to;
    scroller.setAttribute("data-scrolling", "");
    scrollDeffered.resolve(to);
  }, duration);
  return {
    scrollDeffered,
    scroller,
    from,
    to,
    duration,
    isRTL,
    slideTransition,
    isContinuousScrolling,
    autoSlideshowContinuousSpeed,
    currentScrollEndTimeout
  };
}
function animateStopScroll(_a) {
  var scroller = _a.scroller, at = _a.at, isRTL = _a.isRTL;
  Object.assign(scroller.style, {
    "scroll-snap-type": "none"
  });
  var scrollDeffered = new Deferred();
  Object.assign(scroller.firstChild.style, {
    transition: "none",
    "-webkit-transition": "none"
  }, {
    transform: "translateX(0px)"
  });
  scroller.scrollLeft = at;
  scrollDeffered.resolve(at);
  return {
    scrollDeffered,
    scroller,
    isRTL
  };
}
function haltScroll(_a) {
  var scroller = _a.scroller, from = _a.from, isRTL = _a.isRTL, currentScrollEndTimeout = _a.currentScrollEndTimeout, scrollDeffered = _a.scrollDeffered;
  clearTimeout(currentScrollEndTimeout);
  var scrollerInner = scroller.firstChild;
  var computedStyle = getComputedStyle(scrollerInner);
  var transform = computedStyle.getPropertyValue("transform");
  var matrix = new DOMMatrix(transform);
  var margins = Math.round(parseInt(matrix.m41, 10));
  from = from - margins;
  animateStopScroll({
    scroller,
    at: from,
    isRTL
  });
  scrollDeffered.resolve(from);
}

export {
  Deferred,
  isGalleryInViewport,
  scrollToItemImp,
  scrollToGroupImp,
  isWithinPaddingVertically,
  isWithinPaddingHorizontally,
  haltScroll
};
//# sourceMappingURL=chunk-ANOFRNLZ.mjs.map