"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-editor/src/hooks/background.js
var background_exports = {};
__export(background_exports, {
  BACKGROUND_BLOCK_DEFAULT_VALUES: () => BACKGROUND_BLOCK_DEFAULT_VALUES,
  BACKGROUND_SUPPORT_KEY: () => BACKGROUND_SUPPORT_KEY,
  BackgroundImagePanel: () => BackgroundImagePanel,
  backgroundResetAllFilter: () => backgroundResetAllFilter,
  default: () => background_default,
  getBackgroundImageClasses: () => getBackgroundImageClasses,
  hasBackgroundSupport: () => hasBackgroundSupport,
  setBackgroundStyleDefaults: () => setBackgroundStyleDefaults
});
module.exports = __toCommonJS(background_exports);
var import_clsx = __toESM(require("clsx"));
var import_blocks = require("@wordpress/blocks");
var import_data = require("@wordpress/data");
var import_i18n = require("@wordpress/i18n");
var import_inspector_controls = __toESM(require("../components/inspector-controls/index.cjs"));
var import_utils = require("./utils.cjs");
var import_color_values = require("../utils/color-values.cjs");
var import_store = require("../store/index.cjs");
var import_contrast_checker = __toESM(require("./contrast-checker.cjs"));
var import_background_panel = __toESM(require("../components/global-styles/background-panel.cjs"));
var import_private_keys = require("../store/private-keys.cjs");
var import_block_style_state = require("./block-style-state.cjs");
var import_jsx_runtime = require("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 = (0, import_blocks.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 (0, import_background_panel.hasBackgroundImageValue)(style) || (0, import_background_panel.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: (0, import_utils.cleanEmptyObject)({
      ...attributes.style,
      background: void 0,
      color: {
        ...attributes.style?.color,
        background: void 0,
        gradient: void 0
      }
    })
  };
}
function BackgroundInspectorControl({ children }) {
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    import_inspector_controls.default,
    {
      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 = (0, import_block_style_state.useBlockStyleState)();
  const { style, className, backgroundColor, gradient, inheritedValue } = (0, import_data.useSelect)(
    (select) => {
      const { getBlockAttributes, getSettings } = select(import_store.store);
      const _settings = getSettings();
      const blockAttributes = getBlockAttributes(clientId);
      return {
        style: blockAttributes?.style,
        className: blockAttributes?.className,
        backgroundColor: blockAttributes?.backgroundColor,
        gradient: blockAttributes?.gradient,
        inheritedValue: _settings[import_private_keys.globalStylesDataKey]?.blocks?.[name]
      };
    },
    [clientId, name]
  );
  const backgroundGradientSupported = hasBackgroundSupport(
    name,
    "gradient"
  );
  const colorSupport = (0, import_blocks.getBlockSupport)(name, "color");
  const hasColorBackgroundSupport = colorSupport && colorSupport.background !== false;
  const hasColorGradientSupport = !!colorSupport?.gradients;
  const isStateSelected = !(0, import_block_style_state.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 !== (0, import_blocks.getBlockSupport)(name, ["color", "enableContrastChecker"]);
  const contrastWarning = (0, import_contrast_checker.default)({
    clientId,
    name,
    enabled: !!enableContrastChecking,
    messageOverride: (0, import_i18n.__)(
      "This color combination has poor contrast. Consider increasing contrast between background and foreground."
    )
  });
  if (!(0, import_background_panel.useHasBackgroundPanel)(settings) || !hasBackgroundSupport(name) && !hasColorBackgroundSupport && !hasColorGradientSupport) {
    return null;
  }
  const onChange = (newStyle) => {
    if (isStateSelected) {
      setAttributes({
        style: (0, import_block_style_state.setStyleForState)(style, selectedState, newStyle)
      });
      return;
    }
    const newBackgroundColorValue = newStyle?.color?.background;
    const newBackgroundColorSlug = (0, import_color_values.extractPresetSlug)(
      newBackgroundColorValue,
      "color"
    );
    const newGradientValue = backgroundGradientSupported ? newStyle?.background?.gradient : newStyle?.color?.gradient;
    const newGradientSlug = (0, import_color_values.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: (0, import_utils.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 = (0, import_clsx.default)(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 = (0, import_blocks.getBlockSupport)(name, [
    BACKGROUND_SUPPORT_KEY,
    "__experimentalDefaultControls"
  ]);
  const colorDefaultControls = (0, import_blocks.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__ */ (0, import_jsx_runtime.jsx)(
    import_background_panel.default,
    {
      inheritedValue,
      as: Wrapper,
      panelId: clientId,
      defaultValues: BACKGROUND_BLOCK_DEFAULT_VALUES,
      settings: updatedSettings,
      onChange,
      defaultControls,
      value: isStateSelected ? (0, import_block_style_state.getStyleForState)(style, selectedState) : styleValue,
      contrastWarning
    }
  );
}
var background_default = {
  useBlockProps,
  attributeKeys: ["style"],
  hasSupport: hasBackgroundSupport
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  BACKGROUND_BLOCK_DEFAULT_VALUES,
  BACKGROUND_SUPPORT_KEY,
  BackgroundImagePanel,
  backgroundResetAllFilter,
  getBackgroundImageClasses,
  hasBackgroundSupport,
  setBackgroundStyleDefaults
});
//# sourceMappingURL=background.cjs.map
