// packages/block-editor/src/hooks/background.js
import { getBlockSupport } from "@wordpress/blocks";
import { useSelect } from "@wordpress/data";
import { useCallback } from "@wordpress/element";
import InspectorControls from "../components/inspector-controls";
import { cleanEmptyObject } from "./utils";
import { store as blockEditorStore } from "../store";
import {
  default as StylesBackgroundPanel,
  useHasBackgroundPanel,
  hasBackgroundImageValue
} from "../components/global-styles/background-panel";
import { globalStylesDataKey } from "../store/private-keys";
import { jsx } from "react/jsx-runtime";
var BACKGROUND_SUPPORT_KEY = "background";
var BACKGROUND_BLOCK_DEFAULT_VALUES = {
  backgroundSize: "cover",
  backgroundPosition: "50% 50%"
  // used only when backgroundSize is 'contain'.
};
function hasBackgroundSupport(blockName, feature = "any") {
  const support = getBlockSupport(blockName, BACKGROUND_SUPPORT_KEY);
  if (support === true) {
    return true;
  }
  if (feature === "any") {
    return !!support?.backgroundImage || !!support?.backgroundSize || !!support?.backgroundRepeat;
  }
  return !!support?.[feature];
}
function setBackgroundStyleDefaults(backgroundStyle) {
  if (!backgroundStyle || !backgroundStyle?.backgroundImage?.url) {
    return;
  }
  let backgroundStylesWithDefaults;
  if (!backgroundStyle?.backgroundSize) {
    backgroundStylesWithDefaults = {
      backgroundSize: BACKGROUND_BLOCK_DEFAULT_VALUES.backgroundSize
    };
  }
  if ("contain" === backgroundStyle?.backgroundSize && !backgroundStyle?.backgroundPosition) {
    backgroundStylesWithDefaults = {
      backgroundPosition: BACKGROUND_BLOCK_DEFAULT_VALUES.backgroundPosition
    };
  }
  return backgroundStylesWithDefaults;
}
function useBlockProps({ name, style }) {
  if (!hasBackgroundSupport(name) || !style?.background?.backgroundImage) {
    return;
  }
  const backgroundStyles = setBackgroundStyleDefaults(style?.background);
  if (!backgroundStyles) {
    return;
  }
  return {
    style: {
      ...backgroundStyles
    }
  };
}
function getBackgroundImageClasses(style) {
  return hasBackgroundImageValue(style) ? "has-background" : "";
}
function BackgroundInspectorControl({ children }) {
  const resetAllFilter = useCallback((attributes) => {
    return {
      ...attributes,
      style: {
        ...attributes.style,
        background: void 0
      }
    };
  }, []);
  return /* @__PURE__ */ jsx(InspectorControls, { group: "background", resetAllFilter, children });
}
function BackgroundImagePanel({
  clientId,
  name,
  setAttributes,
  settings
}) {
  const { style, inheritedValue } = useSelect(
    (select) => {
      const { getBlockAttributes, getSettings } = select(blockEditorStore);
      const _settings = getSettings();
      return {
        style: getBlockAttributes(clientId)?.style,
        /*
         * To ensure we pass down the right inherited values:
         * @TODO 1. Pass inherited value down to all block style controls,
         *   See: packages/block-editor/src/hooks/style.js
         * @TODO 2. Add support for block style variations,
         *   See implementation: packages/block-editor/src/hooks/block-style-variation.js
         */
        inheritedValue: _settings[globalStylesDataKey]?.blocks?.[name]
      };
    },
    [clientId, name]
  );
  if (!useHasBackgroundPanel(settings) || !hasBackgroundSupport(name, "backgroundImage")) {
    return null;
  }
  const onChange = (newStyle) => {
    setAttributes({
      style: cleanEmptyObject(newStyle)
    });
  };
  const updatedSettings = {
    ...settings,
    background: {
      ...settings.background,
      backgroundSize: settings?.background?.backgroundSize && hasBackgroundSupport(name, "backgroundSize")
    }
  };
  const defaultControls = getBlockSupport(name, [
    BACKGROUND_SUPPORT_KEY,
    "defaultControls"
  ]);
  return /* @__PURE__ */ jsx(
    StylesBackgroundPanel,
    {
      inheritedValue,
      as: BackgroundInspectorControl,
      panelId: clientId,
      defaultValues: BACKGROUND_BLOCK_DEFAULT_VALUES,
      settings: updatedSettings,
      onChange,
      defaultControls,
      value: style
    }
  );
}
var background_default = {
  useBlockProps,
  attributeKeys: ["style"],
  hasSupport: hasBackgroundSupport
};
export {
  BACKGROUND_BLOCK_DEFAULT_VALUES,
  BACKGROUND_SUPPORT_KEY,
  BackgroundImagePanel,
  background_default as default,
  getBackgroundImageClasses,
  hasBackgroundSupport,
  setBackgroundStyleDefaults
};
//# sourceMappingURL=background.js.map
