"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-library/src/utils/hooks.js
var hooks_exports = {};
__export(hooks_exports, {
  useCanEditEntity: () => useCanEditEntity,
  useDefaultAvatar: () => useDefaultAvatar,
  useToolsPanelDropdownMenuProps: () => useToolsPanelDropdownMenuProps,
  useUploadMediaFromBlobURL: () => useUploadMediaFromBlobURL
});
module.exports = __toCommonJS(hooks_exports);
var import_data = require("@wordpress/data");
var import_element = require("@wordpress/element");
var import_blob = require("@wordpress/blob");
var import_block_editor = require("@wordpress/block-editor");
var import_core_data = require("@wordpress/core-data");
var import_compose = require("@wordpress/compose");
function useCanEditEntity(kind, name, recordId) {
  return (0, import_data.useSelect)(
    (select) => select(import_core_data.store).canUser("update", {
      kind,
      name,
      id: recordId
    }),
    [kind, name, recordId]
  );
}
function useUploadMediaFromBlobURL(args = {}) {
  const latestArgsRef = (0, import_element.useRef)(args);
  const hasUploadStartedRef = (0, import_element.useRef)(false);
  const { getSettings } = (0, import_data.useSelect)(import_block_editor.store);
  (0, import_element.useLayoutEffect)(() => {
    latestArgsRef.current = args;
  });
  (0, import_element.useEffect)(() => {
    if (hasUploadStartedRef.current) {
      return;
    }
    if (!latestArgsRef.current.url || !(0, import_blob.isBlobURL)(latestArgsRef.current.url)) {
      return;
    }
    const file = (0, import_blob.getBlobByURL)(latestArgsRef.current.url);
    if (!file) {
      return;
    }
    const { url, allowedTypes, onChange, onError } = latestArgsRef.current;
    const { mediaUpload } = getSettings();
    if (!mediaUpload) {
      return;
    }
    hasUploadStartedRef.current = true;
    mediaUpload({
      filesList: [file],
      allowedTypes,
      onFileChange: ([media]) => {
        if ((0, import_blob.isBlobURL)(media?.url)) {
          return;
        }
        (0, import_blob.revokeBlobURL)(url);
        onChange(media);
        hasUploadStartedRef.current = false;
      },
      onError: (message) => {
        (0, import_blob.revokeBlobURL)(url);
        onError(message);
        hasUploadStartedRef.current = false;
      }
    });
  }, [getSettings]);
}
function useDefaultAvatar() {
  const { avatarURL: defaultAvatarUrl } = (0, import_data.useSelect)((select) => {
    const { getSettings } = select(import_block_editor.store);
    const { __experimentalDiscussionSettings } = getSettings();
    return __experimentalDiscussionSettings;
  });
  return defaultAvatarUrl;
}
function useToolsPanelDropdownMenuProps() {
  const isMobile = (0, import_compose.useViewportMatch)("medium", "<");
  return !isMobile ? {
    popoverProps: {
      placement: "left-start",
      // For non-mobile, inner sidebar width (248px) - button width (24px) - border (1px) + padding (16px) + spacing (20px)
      offset: 259
    }
  } : {};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useCanEditEntity,
  useDefaultAvatar,
  useToolsPanelDropdownMenuProps,
  useUploadMediaFromBlobURL
});
//# sourceMappingURL=hooks.js.map
