import React, { Fragment, Suspense } from "react";
import { extract } from "./../../../ricos-content/RicosContentAPI/extract-next.js";
import { Node_Type } from "./../../../ricos-schema/next-schema/index.js";
const loadFullscreenModal = () => import(
  /* webpackChunkName: "ricos_FullscreenModalViewer"  */
  "./fullscreen-modal.js"
);
const FullscreenModal = React.lazy(loadFullscreenModal);
const FullscreenProvider = (props) => {
  const { children, isMobile, fullscreenProps, content } = props;
  const [state, setState] = React.useState({ type: "closed" });
  const expandModeData = React.useMemo(() => {
    if (content) {
      return getImagesData(content);
    } else {
      return { images: [], imageMap: {} };
    }
  }, [props.content]);
  const addExpand = (config) => {
    const onExpand = (nodeId, innerIndex = 0) => {
      setState({
        type: "open",
        activeIndex: expandModeData ? expandModeData.imageMap[nodeId] + innerIndex : 0
      });
    };
    const newConfig = { ...config };
    const imageConfig = config["wix-draft-plugin-image"];
    const galleryConfig = config["wix-draft-plugin-gallery"];
    if (imageConfig && !imageConfig.onExpand) {
      newConfig["wix-draft-plugin-image"] = { ...imageConfig, onExpand };
    }
    if (galleryConfig && !galleryConfig.onExpand) {
      newConfig["wix-draft-plugin-gallery"] = { ...galleryConfig, onExpand };
    }
    return newConfig;
  };
  return /* @__PURE__ */ React.createElement(Fragment, null, children({ config: addExpand(props.config), onHover: () => loadFullscreenModal() }), state.type === "open" && /* @__PURE__ */ React.createElement(Suspense, { fallback: /* @__PURE__ */ React.createElement("div", null) }, /* @__PURE__ */ React.createElement(
    FullscreenModal,
    {
      images: expandModeData.images,
      onClose: () => setState({ type: "closed" }),
      index: state.activeIndex,
      isMobile,
      ...fullscreenProps,
      jsdomMode: props.jsdomMode,
      t: props.t
    }
  )));
};
const getImagesData = (content) => {
  const imagesData = {
    images: [],
    imageMap: {}
  };
  let index = 0;
  return extract(content.nodes).filter((node) => [Node_Type.IMAGE, Node_Type.GALLERY].includes(node.type)).get().reduce((items, node) => {
    const { images, imageMap } = items;
    const galleryItems = convertDataToGalleryItems(node);
    const currentIndex = index;
    index += galleryItems.length;
    return {
      images: [...images, ...galleryItems],
      imageMap: {
        ...imageMap,
        // @TODO: currently indexing doesn't work when nodes don't have ids
        [node.id ?? ""]: currentIndex
      }
    };
  }, imagesData);
};
const convertDataToGalleryItems = (node) => {
  switch (node.type) {
    case Node_Type.IMAGE:
      return node.imageData.image?.src && !node.imageData?.disableExpand ? [
        {
          image: { media: { ...node.imageData.image } },
          disableDownload: node.imageData.disableDownload,
          altText: node.imageData.altText
        }
      ] : [];
    case Node_Type.GALLERY: {
      return node.galleryData?.disableExpand ? [] : node.galleryData.items.map((item) => {
        return { ...item, disableDownload: node.galleryData.disableDownload };
      });
    }
    default:
      return [];
  }
};
export {
  FullscreenProvider
};
//# sourceMappingURL=fullscreen-provider.js.map
