// packages/block-editor/src/components/list-view/use-list-view-images.js
import { useMemo } from "@wordpress/element";
import { useSelect } from "@wordpress/data";
import { store as blockEditorStore } from "../../store";
var MAX_IMAGES = 3;
var IMAGE_GETTERS = {
  "core/image": ({ clientId, attributes }) => {
    if (attributes.url) {
      return {
        url: attributes.url,
        alt: attributes.alt || "",
        clientId
      };
    }
  },
  "core/cover": ({ clientId, attributes }) => {
    if (attributes.backgroundType === "image" && attributes.url) {
      return {
        url: attributes.url,
        alt: attributes.alt || "",
        clientId
      };
    }
  },
  "core/media-text": ({ clientId, attributes }) => {
    if (attributes.mediaType === "image" && attributes.mediaUrl) {
      return {
        url: attributes.mediaUrl,
        alt: attributes.mediaAlt || "",
        clientId
      };
    }
  },
  "core/gallery": ({ innerBlocks }) => {
    const images = [];
    const getValues = !!innerBlocks?.length ? IMAGE_GETTERS[innerBlocks[0].name] : void 0;
    if (!getValues) {
      return images;
    }
    for (const innerBlock of innerBlocks) {
      const img = getValues(innerBlock);
      if (img) {
        images.push(img);
      }
      if (images.length >= MAX_IMAGES) {
        return images;
      }
    }
    return images;
  }
};
function getImagesFromBlock(block, isExpanded) {
  const getImages = IMAGE_GETTERS[block.name];
  const images = !!getImages ? getImages(block) : void 0;
  if (!images) {
    return [];
  }
  if (!Array.isArray(images)) {
    return [images];
  }
  return isExpanded ? [] : images;
}
function useListViewImages({ clientId, isExpanded }) {
  const { block } = useSelect(
    (select) => {
      return { block: select(blockEditorStore).getBlock(clientId) };
    },
    [clientId]
  );
  const images = useMemo(() => {
    return getImagesFromBlock(block, isExpanded);
  }, [block, isExpanded]);
  return images;
}
export {
  useListViewImages as default
};
//# sourceMappingURL=use-list-view-images.js.map
