"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/color.js
var color_exports = {};
__export(color_exports, {
  COLOR_SUPPORT_KEY: () => COLOR_SUPPORT_KEY,
  addSaveProps: () => addSaveProps,
  addTransforms: () => addTransforms,
  default: () => color_default
});
module.exports = __toCommonJS(color_exports);
var import_clsx = __toESM(require("clsx"));
var import_hooks = require("@wordpress/hooks");
var import_blocks = require("@wordpress/blocks");
var import_element = require("@wordpress/element");
var import_colors = require("../components/colors/index.cjs");
var import_gradients = require("../components/gradients/index.cjs");
var import_utils = require("./utils.cjs");
var import_background = require("./background.cjs");
var import_use_settings = require("../components/use-settings/index.cjs");
var COLOR_SUPPORT_KEY = "color";
var hasColorSupport = (blockNameOrType) => {
  const colorSupport = (0, import_blocks.getBlockSupport)(blockNameOrType, COLOR_SUPPORT_KEY);
  return colorSupport && (colorSupport.link === true || colorSupport.gradient === true || colorSupport.background !== false || colorSupport.text !== false);
};
var hasLinkColorSupport = (blockType) => {
  const colorSupport = (0, import_blocks.getBlockSupport)(blockType, COLOR_SUPPORT_KEY);
  return colorSupport !== null && typeof colorSupport === "object" && !!colorSupport.link;
};
var hasGradientSupport = (blockNameOrType) => {
  const colorSupport = (0, import_blocks.getBlockSupport)(blockNameOrType, COLOR_SUPPORT_KEY);
  return colorSupport !== null && typeof colorSupport === "object" && !!colorSupport.gradients;
};
var hasBackgroundColorSupport = (blockType) => {
  const colorSupport = (0, import_blocks.getBlockSupport)(blockType, COLOR_SUPPORT_KEY);
  return colorSupport && colorSupport.background !== false;
};
var hasTextColorSupport = (blockType) => {
  const colorSupport = (0, import_blocks.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) || (0, import_utils.shouldSkipSerialization)(blockNameOrType, COLOR_SUPPORT_KEY)) {
    return props;
  }
  const hasGradient = hasGradientSupport(blockNameOrType);
  const { backgroundColor, textColor, gradient, style } = attributes;
  const shouldSerialize = (feature) => !(0, import_utils.shouldSkipSerialization)(
    blockNameOrType,
    COLOR_SUPPORT_KEY,
    feature
  );
  const textClass = shouldSerialize("text") ? (0, import_colors.getColorClassName)("color", textColor) : void 0;
  const gradientClass = shouldSerialize("gradients") ? (0, import_gradients.__experimentalGetGradientClass)(gradient) : void 0;
  const backgroundClass = shouldSerialize("background") ? (0, import_colors.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 = (0, import_clsx.default)(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] = (0, import_use_settings.useSettings)(
    "color.palette.custom",
    "color.palette.theme",
    "color.palette.default"
  );
  const colors = (0, import_element.useMemo)(
    () => [
      ...userPalette || [],
      ...themePalette || [],
      ...defaultPalette || []
    ],
    [userPalette, themePalette, defaultPalette]
  );
  if (!hasColorSupport(name) || (0, import_utils.shouldSkipSerialization)(name, COLOR_SUPPORT_KEY)) {
    return {};
  }
  const extraStyles = {};
  if (textColor && !(0, import_utils.shouldSkipSerialization)(name, COLOR_SUPPORT_KEY, "text")) {
    extraStyles.color = (0, import_colors.getColorObjectByAttributeValues)(
      colors,
      textColor
    )?.color;
  }
  if (backgroundColor && !(0, import_utils.shouldSkipSerialization)(name, COLOR_SUPPORT_KEY, "background")) {
    extraStyles.backgroundColor = (0, import_colors.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: (0, import_clsx.default)(
      saveProps.className,
      // Add background image classes in the editor, if not already handled by background color values.
      !hasBackgroundValue && (0, import_background.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 (0, import_utils.transformStyles)(
    activeSupports,
    MIGRATION_PATHS,
    result,
    source,
    index,
    results
  );
}
(0, import_hooks.addFilter)(
  "blocks.registerBlockType",
  "core/color/addAttribute",
  addAttributes
);
(0, import_hooks.addFilter)(
  "blocks.switchToBlockType.transformedBlock",
  "core/color/addTransforms",
  addTransforms
);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  COLOR_SUPPORT_KEY,
  addSaveProps,
  addTransforms
});
//# sourceMappingURL=color.cjs.map
