"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var use_height_exports = {};
__export(use_height_exports, {
  useHeight: () => useHeight
});
module.exports = __toCommonJS(use_height_exports);
var import_lodash = __toESM(require("lodash"));
var import_react = __toESM(require("react"));
var import_ricos_context = require("./../../ricos-context/index.js");
var import_v1 = require("./../../ricos-schema/legacy/wix/rich_content/v1/index.js");
var import_viewer_scss = __toESM(require("../statics/styles/viewer-scss.js"));
var import_use_prerender = require("./use-prerender.js");
var import_use_resize_observer = require("./use-resize-observer.js");
const THUMBNAIL_GALLERY_HEIGHT = 620;
const RESIZE_OBSERVER_DELAY = 100;
const defaultContainers = {
  desktop: {
    width: 980,
    height: 500
  },
  mobile: {
    width: 320,
    height: 500
  }
};
const useHeight = (props) => {
  const [isMeasuredContainer, setIsMeasuredContainer] = import_react.default.useState(false);
  const isPrerender = (0, import_use_prerender.useIsPrerender)();
  const { options, layout } = props;
  const { responsive } = options;
  const { mobile, desktop } = defaultContainers;
  const { isMobile, jsdomMode } = (0, import_react.useContext)(import_ricos_context.RicosContext);
  const containerWidthResponsive = isMobile ? mobile.width : desktop.width;
  const containerHeightResponsive = isMobile ? mobile.height : desktop.height;
  const defaultContainer = {
    isDefaultContainer: true,
    width: containerWidthResponsive,
    height: containerHeightResponsive
  };
  const [container, setContainer] = (0, import_react.useState)(defaultContainer);
  const [height, setHeight] = (0, import_react.useState)(container.height);
  const [containerRef, setContainerRef] = (0, import_react.useState)(null);
  import_react.default.useEffect(() => {
    if (isMeasuredContainer) {
      setIsMeasuredContainer(false);
      setContainer(defaultContainer);
    }
  }, [layout]);
  const resizeGallery = (0, import_react.useCallback)(() => {
    setContainer((prevState) => {
      if (containerRef?.clientHeight && prevState.height && Math.abs(prevState.height - containerRef?.clientHeight) <= 1 && Math.abs(prevState.width - containerRef?.clientWidth) <= 1) {
        return prevState;
      }
      return {
        width: containerRef?.clientWidth,
        height: containerRef?.clientHeight
      };
    });
    setIsMeasuredContainer(true);
  }, [containerRef, setIsMeasuredContainer]);
  const resizeObserverCallback = (0, import_react.useMemo)(
    () => import_lodash.default.debounce(resizeGallery, RESIZE_OBSERVER_DELAY),
    [resizeGallery]
  );
  (0, import_use_resize_observer.useResizeObserver)({
    element: containerRef,
    callback: resizeObserverCallback
  });
  const heightStyle = layout === import_v1.GalleryOptions_Layout_Type.THUMBNAIL && !isMobile ? { height: THUMBNAIL_GALLERY_HEIGHT } : responsive ? {} : { height };
  const setHeightImp = (newHeight) => {
    setHeight(newHeight);
  };
  const containerClassName = isPrerender ? import_viewer_scss.default.prerender : "";
  return {
    isMeasuredContainer: jsdomMode ? !isPrerender : isMeasuredContainer,
    setHeight: setHeightImp,
    container,
    setContainerRef,
    containerRef,
    containerStyle: heightStyle,
    containerClassName
  };
};
//# sourceMappingURL=use-height.js.map
