// packages/block-editor/src/hooks/color.js
import clsx from "clsx";
import { addFilter } from "@wordpress/hooks";
import { getBlockSupport } from "@wordpress/blocks";
import { useMemo } from "@wordpress/element";
import {
  getColorClassName,
  getColorObjectByAttributeValues
} from "../components/colors/index.mjs";
import { __experimentalGetGradientClass } from "../components/gradients/index.mjs";
import { transformStyles, shouldSkipSerialization } from "./utils.mjs";
import { getBackgroundImageClasses } from "./background.mjs";
import { useSettings } from "../components/use-settings/index.mjs";
var COLOR_SUPPORT_KEY = "color";
var hasColorSupport = (blockNameOrType) => {
  const colorSupport = getBlockSupport(blockNameOrType, COLOR_SUPPORT_KEY);
  return colorSupport && (colorSupport.link === true || colorSupport.gradient === true || colorSupport.background !== false || colorSupport.text !== false);
};
var hasLinkColorSupport = (blockType) => {
  const colorSupport = getBlockSupport(blockType, COLOR_SUPPORT_KEY);
  return colorSupport !== null && typeof colorSupport === "object" && !!colorSupport.link;
};
var hasGradientSupport = (blockNameOrType) => {
  const colorSupport = getBlockSupport(blockNameOrType, COLOR_SUPPORT_KEY);
  return colorSupport !== null && typeof colorSupport === "object" && !!colorSupport.gradients;
};
var hasBackgroundColorSupport = (blockType) => {
  const colorSupport = getBlockSupport(blockType, COLOR_SUPPORT_KEY);
  return colorSupport && colorSupport.background !== false;
};
var hasTextColorSupport = (blockType) => {
  const colorSupport = getBlockSupport(blockType, COLOR_SUPPORT_KEY);
  return colorSupport && colorSupport.text !== false;
};
function addAttributes(settings) {
  if (!hasColorSupport(settings)) {
    return settings;
  }
  if (!settings.attributes.backgroundColor) {
    Object.assign(settings.attributes, {
      backgroundColor: {
        type: "string"
      }
    });
  }
  if (!settings.attributes.textColor) {
    Object.assign(settings.attributes, {
      textColor: {
        type: "string"
      }
    });
  }
  if (hasGradientSupport(settings) && !settings.attributes.gradient) {
    Object.assign(settings.attributes, {
      gradient: {
        type: "string"
      }
    });
  }
  return settings;
}
function addSaveProps(props, blockNameOrType, attributes) {
  if (!hasColorSupport(blockNameOrType) || shouldSkipSerialization(blockNameOrType, COLOR_SUPPORT_KEY)) {
    return props;
  }
  const hasGradient = hasGradientSupport(blockNameOrType);
  const { backgroundColor, textColor, gradient, style } = attributes;
  const shouldSerialize = (feature) => !shouldSkipSerialization(
    blockNameOrType,
    COLOR_SUPPORT_KEY,
    feature
  );
  const textClass = shouldSerialize("text") ? getColorClassName("color", textColor) : void 0;
  const gradientClass = shouldSerialize("gradients") ? __experimentalGetGradientClass(gradient) : void 0;
  const backgroundClass = shouldSerialize("background") ? getColorClassName("background-color", backgroundColor) : void 0;
  const serializeHasBackground = shouldSerialize("background") || shouldSerialize("gradients");
  const hasBackground = backgroundColor || style?.color?.background || hasGradient && (gradient || style?.color?.gradient);
  const newClassName = clsx(props.className, textClass, gradientClass, {
    // Don't apply the background class if there's a custom gradient.
    [backgroundClass]: (!hasGradient || !style?.color?.gradient) && !!backgroundClass,
    "has-text-color": shouldSerialize("text") && (textColor || style?.color?.text),
    "has-background": serializeHasBackground && hasBackground,
    "has-link-color": shouldSerialize("link") && style?.elements?.link?.color
  });
  props.className = newClassName ? newClassName : void 0;
  return props;
}
function useBlockProps({
  name,
  backgroundColor,
  textColor,
  gradient,
  style
}) {
  const [userPalette, themePalette, defaultPalette] = useSettings(
    "color.palette.custom",
    "color.palette.theme",
    "color.palette.default"
  );
  const colors = useMemo(
    () => [
      ...userPalette || [],
      ...themePalette || [],
      ...defaultPalette || []
    ],
    [userPalette, themePalette, defaultPalette]
  );
  if (!hasColorSupport(name) || shouldSkipSerialization(name, COLOR_SUPPORT_KEY)) {
    return {};
  }
  const extraStyles = {};
  if (textColor && !shouldSkipSerialization(name, COLOR_SUPPORT_KEY, "text")) {
    extraStyles.color = getColorObjectByAttributeValues(
      colors,
      textColor
    )?.color;
  }
  if (backgroundColor && !shouldSkipSerialization(name, COLOR_SUPPORT_KEY, "background")) {
    extraStyles.backgroundColor = getColorObjectByAttributeValues(
      colors,
      backgroundColor
    )?.color;
  }
  const saveProps = addSaveProps({ style: extraStyles }, name, {
    textColor,
    backgroundColor,
    gradient,
    style
  });
  const hasBackgroundValue = backgroundColor || style?.color?.background || gradient || style?.color?.gradient;
  return {
    ...saveProps,
    className: clsx(
      saveProps.className,
      // Add background image classes in the editor, if not already handled by background color values.
      !hasBackgroundValue && getBackgroundImageClasses(style)
    )
  };
}
var color_default = {
  useBlockProps,
  addSaveProps,
  attributeKeys: ["backgroundColor", "textColor", "gradient", "style"],
  hasSupport: hasColorSupport
};
var MIGRATION_PATHS = {
  linkColor: [["style", "elements", "link", "color", "text"]],
  textColor: [["textColor"], ["style", "color", "text"]],
  backgroundColor: [
    ["backgroundColor"],
    ["style", "color", "background"]
  ],
  gradient: [["gradient"], ["style", "color", "gradient"]]
};
function addTransforms(result, source, index, results) {
  const destinationBlockType = result.name;
  const activeSupports = {
    linkColor: hasLinkColorSupport(destinationBlockType),
    textColor: hasTextColorSupport(destinationBlockType),
    backgroundColor: hasBackgroundColorSupport(destinationBlockType),
    gradient: hasGradientSupport(destinationBlockType)
  };
  return transformStyles(
    activeSupports,
    MIGRATION_PATHS,
    result,
    source,
    index,
    results
  );
}
addFilter(
  "blocks.registerBlockType",
  "core/color/addAttribute",
  addAttributes
);
addFilter(
  "blocks.switchToBlockType.transformedBlock",
  "core/color/addTransforms",
  addTransforms
);
export {
  COLOR_SUPPORT_KEY,
  addSaveProps,
  addTransforms,
  color_default as default
};
//# sourceMappingURL=color.mjs.map
