// packages/block-editor/src/hooks/background.js
import clsx from "clsx";
import { getBlockSupport } from "@wordpress/blocks";
import { useSelect } from "@wordpress/data";
import { __ } from "@wordpress/i18n";
import InspectorControls from "../components/inspector-controls/index.mjs";
import { cleanEmptyObject } from "./utils.mjs";
import { extractPresetSlug } from "../utils/color-values.mjs";
import { store as blockEditorStore } from "../store/index.mjs";
import useBlockColorContrastWarning from "./contrast-checker.mjs";
import {
  default as StylesBackgroundPanel,
  useHasBackgroundPanel,
  hasBackgroundImageValue,
  hasBackgroundGradientValue
} from "../components/global-styles/background-panel.mjs";
import { globalStylesDataKey } from "../store/private-keys.mjs";
import {
  getStyleForState,
  isDefaultBlockStyleState,
  setStyleForState,
  useBlockStyleState
} from "./block-style-state.mjs";
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 || !!support?.gradient;
  }
  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) || hasBackgroundGradientValue(style) ? "has-background" : "";
}
function backgroundResetAllFilter(attributes) {
  const updatedClassName = attributes.className?.includes("has-background") ? attributes.className.split(" ").filter((c) => c !== "has-background").join(" ") || void 0 : attributes.className;
  return {
    ...attributes,
    className: updatedClassName,
    backgroundColor: void 0,
    gradient: void 0,
    style: cleanEmptyObject({
      ...attributes.style,
      background: void 0,
      color: {
        ...attributes.style?.color,
        background: void 0,
        gradient: void 0
      }
    })
  };
}
function BackgroundInspectorControl({ children }) {
  return /* @__PURE__ */ jsx(
    InspectorControls,
    {
      group: "background",
      resetAllFilter: backgroundResetAllFilter,
      children
    }
  );
}
function BackgroundImagePanel({
  clientId,
  name,
  setAttributes,
  settings,
  // Allows rendering outside the `background` inspector group (e.g. section
  // blocks direct-render this panel because their support fills are gated
  // off by editing mode). Defaults to the slot-based wrapper.
  asWrapper
}) {
  const selectedState = useBlockStyleState();
  const { style, className, backgroundColor, gradient, inheritedValue } = useSelect(
    (select) => {
      const { getBlockAttributes, getSettings } = select(blockEditorStore);
      const _settings = getSettings();
      const blockAttributes = getBlockAttributes(clientId);
      return {
        style: blockAttributes?.style,
        className: blockAttributes?.className,
        backgroundColor: blockAttributes?.backgroundColor,
        gradient: blockAttributes?.gradient,
        inheritedValue: _settings[globalStylesDataKey]?.blocks?.[name]
      };
    },
    [clientId, name]
  );
  const backgroundGradientSupported = hasBackgroundSupport(
    name,
    "gradient"
  );
  const colorSupport = getBlockSupport(name, "color");
  const hasColorBackgroundSupport = colorSupport && colorSupport.background !== false;
  const hasColorGradientSupport = !!colorSupport?.gradients;
  const isStateSelected = !isDefaultBlockStyleState(selectedState);
  const styleValue = {
    ...style,
    color: {
      ...style?.color,
      background: backgroundColor ? "var:preset|color|" + backgroundColor : style?.color?.background,
      gradient: !backgroundGradientSupported && gradient ? "var:preset|gradient|" + gradient : style?.color?.gradient
    },
    ...backgroundGradientSupported && {
      background: {
        ...style?.background,
        gradient: gradient ? "var:preset|gradient|" + gradient : style?.background?.gradient ?? style?.color?.gradient
      }
    }
  };
  const enableContrastChecking = !isStateSelected && !styleValue?.color?.gradient && !styleValue?.background?.gradient && !!styleValue?.color?.background && (settings?.color?.text || settings?.color?.link) && false !== getBlockSupport(name, ["color", "enableContrastChecker"]);
  const contrastWarning = useBlockColorContrastWarning({
    clientId,
    name,
    enabled: !!enableContrastChecking,
    messageOverride: __(
      "This color combination has poor contrast. Consider increasing contrast between background and foreground."
    )
  });
  if (!useHasBackgroundPanel(settings) || !hasBackgroundSupport(name) && !hasColorBackgroundSupport && !hasColorGradientSupport) {
    return null;
  }
  const onChange = (newStyle) => {
    if (isStateSelected) {
      setAttributes({
        style: setStyleForState(style, selectedState, newStyle)
      });
      return;
    }
    const newBackgroundColorValue = newStyle?.color?.background;
    const newBackgroundColorSlug = extractPresetSlug(
      newBackgroundColorValue,
      "color"
    );
    const newGradientValue = backgroundGradientSupported ? newStyle?.background?.gradient : newStyle?.color?.gradient;
    const newGradientSlug = extractPresetSlug(
      newGradientValue,
      "gradient"
    );
    const cleanedColorGradient = newGradientSlug ? void 0 : newStyle?.color?.gradient;
    const cleanedStyle = {
      ...newStyle,
      color: {
        ...newStyle?.color,
        background: newBackgroundColorSlug ? void 0 : newBackgroundColorValue,
        gradient: backgroundGradientSupported ? void 0 : cleanedColorGradient
      }
    };
    if (backgroundGradientSupported) {
      cleanedStyle.background = {
        ...cleanedStyle.background,
        gradient: newStyle?.background?.gradient
      };
    }
    const isMigrating = backgroundGradientSupported && !!style?.color?.gradient;
    const newAttributes = {
      style: cleanEmptyObject(cleanedStyle),
      backgroundColor: newBackgroundColorSlug,
      // The legacy `gradient` attribute is only used when the block does
      // not support `background.gradient`; otherwise the gradient is
      // stored in `style.background.gradient` above.
      gradient: backgroundGradientSupported ? void 0 : newGradientSlug
    };
    const hasNewGradient = !!newGradientSlug || !!newGradientValue;
    if (isMigrating && hasNewGradient) {
      newAttributes.className = clsx(className, "has-background");
    } else if (!hasNewGradient && className?.includes("has-background")) {
      newAttributes.className = className.split(" ").filter((c) => c !== "has-background").join(" ") || void 0;
    }
    setAttributes(newAttributes);
  };
  const updatedSettings = {
    ...settings,
    background: {
      ...settings.background,
      backgroundSize: settings?.background?.backgroundSize && hasBackgroundSupport(name, "backgroundSize")
    }
  };
  const backgroundDefaultControls = getBlockSupport(name, [
    BACKGROUND_SUPPORT_KEY,
    "__experimentalDefaultControls"
  ]);
  const colorDefaultControls = getBlockSupport(name, [
    "color",
    "__experimentalDefaultControls"
  ]);
  const defaultControls = {
    ...backgroundDefaultControls,
    backgroundColor: colorDefaultControls?.background,
    // Mirror the old combined control: show the gradient item by default
    // when background color was shown by default.
    gradient: backgroundDefaultControls?.gradient ?? colorDefaultControls?.background
  };
  const Wrapper = asWrapper || BackgroundInspectorControl;
  return /* @__PURE__ */ jsx(
    StylesBackgroundPanel,
    {
      inheritedValue,
      as: Wrapper,
      panelId: clientId,
      defaultValues: BACKGROUND_BLOCK_DEFAULT_VALUES,
      settings: updatedSettings,
      onChange,
      defaultControls,
      value: isStateSelected ? getStyleForState(style, selectedState) : styleValue,
      contrastWarning
    }
  );
}
var background_default = {
  useBlockProps,
  attributeKeys: ["style"],
  hasSupport: hasBackgroundSupport
};
export {
  BACKGROUND_BLOCK_DEFAULT_VALUES,
  BACKGROUND_SUPPORT_KEY,
  BackgroundImagePanel,
  backgroundResetAllFilter,
  background_default as default,
  getBackgroundImageClasses,
  hasBackgroundSupport,
  setBackgroundStyleDefaults
};
//# sourceMappingURL=background.mjs.map
