"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-editor/src/components/inserter/media-tab/hooks.js
var hooks_exports = {};
__export(hooks_exports, {
  useMediaCategories: () => useMediaCategories,
  useMediaResults: () => useMediaResults
});
module.exports = __toCommonJS(hooks_exports);
var import_element = require("@wordpress/element");
var import_data = require("@wordpress/data");
var import_store = require("../../../store");
var import_lock_unlock = require("../../../lock-unlock");
function useMediaResults(category, query = {}) {
  const [mediaList, setMediaList] = (0, import_element.useState)();
  const [isLoading, setIsLoading] = (0, import_element.useState)(false);
  const lastRequestRef = (0, import_element.useRef)();
  (0, import_element.useEffect)(() => {
    (async () => {
      const key = JSON.stringify({
        category: category.name,
        ...query
      });
      lastRequestRef.current = key;
      setIsLoading(true);
      setMediaList([]);
      const _media = await category.fetch?.(query);
      if (key === lastRequestRef.current) {
        setMediaList(_media);
        setIsLoading(false);
      }
    })();
  }, [category.name, ...Object.values(query)]);
  return { mediaList, isLoading };
}
function useMediaCategories(rootClientId) {
  const [categories, setCategories] = (0, import_element.useState)([]);
  const inserterMediaCategories = (0, import_data.useSelect)(
    (select) => (0, import_lock_unlock.unlock)(select(import_store.store)).getInserterMediaCategories(),
    []
  );
  const { canInsertImage, canInsertVideo, canInsertAudio } = (0, import_data.useSelect)(
    (select) => {
      const { canInsertBlockType } = select(import_store.store);
      return {
        canInsertImage: canInsertBlockType(
          "core/image",
          rootClientId
        ),
        canInsertVideo: canInsertBlockType(
          "core/video",
          rootClientId
        ),
        canInsertAudio: canInsertBlockType(
          "core/audio",
          rootClientId
        )
      };
    },
    [rootClientId]
  );
  (0, import_element.useEffect)(() => {
    (async () => {
      const _categories = [];
      if (!inserterMediaCategories) {
        return;
      }
      const categoriesHaveMedia = new Map(
        await Promise.all(
          inserterMediaCategories.map(async (category) => {
            if (category.isExternalResource) {
              return [category.name, true];
            }
            let results = [];
            try {
              results = await category.fetch({
                per_page: 1
              });
            } catch (e) {
            }
            return [category.name, !!results.length];
          })
        )
      );
      const canInsertMediaType = {
        image: canInsertImage,
        video: canInsertVideo,
        audio: canInsertAudio
      };
      inserterMediaCategories.forEach((category) => {
        if (canInsertMediaType[category.mediaType] && categoriesHaveMedia.get(category.name)) {
          _categories.push(category);
        }
      });
      if (!!_categories.length) {
        setCategories(_categories);
      }
    })();
  }, [
    canInsertImage,
    canInsertVideo,
    canInsertAudio,
    inserterMediaCategories
  ]);
  return categories;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useMediaCategories,
  useMediaResults
});
//# sourceMappingURL=hooks.js.map
