import { GALLERY_CONSTS } from "pro-gallery";
import { migrateOptions } from "pro-gallery-lib";
import { useContext, useMemo } from "react";
import { RicosContext } from "./../../ricos-context/index.js";
import { GalleryOptions_Layout_Type as GALLERY_LAYOUT } from "./../../ricos-schema/legacy/wix/rich_content/v1/index.js";
import { getFixedStyles } from "../defaults.js";
import {
  GALLERY_LAYOUT_TO_NUMBER,
  SCROLL_DIRECTION_TO_NUMBER,
  isHorizontalLayout
} from "./utils.js";
const convertToGalleryOptions = (options) => {
  const galleryOptions = {};
  const { layout, thumbnails, item } = options;
  if (layout) {
    const { type, horizontalScroll, orientation, numberOfColumns, mobileNumberOfColumns } = layout;
    type && (galleryOptions.galleryLayout = parseInt(GALLERY_LAYOUT_TO_NUMBER[type].toString()));
    horizontalScroll && (galleryOptions.scrollDirection = SCROLL_DIRECTION_TO_NUMBER[GALLERY_CONSTS.layoutParams_structure_scrollDirection.HORIZONTAL]);
    orientation && (galleryOptions.isVertical = orientation === "COLUMNS");
    typeof numberOfColumns === "number" && (galleryOptions.numberOfImagesPerRow = numberOfColumns);
    typeof mobileNumberOfColumns === "number" && (galleryOptions.m_numberOfImagesPerRow = mobileNumberOfColumns);
  }
  const { targetSize, ratio, crop, spacing } = item || {};
  if (typeof targetSize === "number") {
    galleryOptions.gallerySizePx = targetSize;
  } else {
    galleryOptions.gallerySizePx = 300;
  }
  typeof ratio === "number" && (galleryOptions.cubeRatio = ratio);
  crop && (galleryOptions.cubeType = crop.toLowerCase());
  typeof spacing === "number" && (galleryOptions.imageMargin = spacing);
  if (thumbnails) {
    const { placement, spacing: spacing2 } = thumbnails;
    placement && (galleryOptions.galleryThumbnailsAlignment = placement.toLowerCase());
    typeof spacing2 === "number" && (galleryOptions.thumbnailSpacings = spacing2 / 2);
  }
  return galleryOptions;
};
function useOptions(ricosOptions, disableDownload, items) {
  const { isMobile } = useContext(RicosContext);
  const galleryOptions = useMemo(() => {
    const isHorizontal = isHorizontalLayout(ricosOptions);
    const calculatedOptions = {
      ...getFixedStyles(ricosOptions.layout?.type || GALLERY_LAYOUT.GRID),
      allowContextMenu: !disableDownload,
      showArrows: isHorizontal,
      responsive: true,
      ...convertToGalleryOptions(ricosOptions)
    };
    if (isMobile) {
      if (calculatedOptions.m_numberOfImagesPerRow) {
        calculatedOptions.numberOfImagesPerRow = calculatedOptions.m_numberOfImagesPerRow;
      }
      if (isHorizontal) {
        calculatedOptions.arrowsSize = 20;
        !calculatedOptions.imageMargin && (calculatedOptions.imageMargin = 0);
        if (calculatedOptions.galleryLayout === GALLERY_LAYOUT_TO_NUMBER[GALLERY_LAYOUT.THUMBNAIL]) {
          calculatedOptions.thumbnailSize = 90;
        }
      }
    }
    const options = migrateOptions(calculatedOptions);
    if (options.layoutParams_structure_galleryLayout === GALLERY_LAYOUT_TO_NUMBER[GALLERY_LAYOUT.SLIDER]) {
      const galleryRatio = calculatedOptions.cubeRatio ? 1 / calculatedOptions.cubeRatio : null;
      const minItemRatio = Math.min(
        ...items.map((item) => item.metaData.height / item.metaData.width)
      );
      options.layoutParams_structure_galleryRatio_value = // Keep itemRatio within range [16:9 ... 1:1]:
      galleryRatio || Math.max(9 / 16, Math.min(1, minItemRatio));
    }
    return { options, calculatedOptions };
  }, [items, ricosOptions]);
  return galleryOptions;
}
export {
  useOptions
};
//# sourceMappingURL=use-options.js.map
