"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/components/global-styles/color-panel.js
var color_panel_exports = {};
__export(color_panel_exports, {
  ColorToolsPanel: () => ColorToolsPanel,
  default: () => ColorPanel,
  useHasBackgroundColorPanel: () => useHasBackgroundColorPanel,
  useHasButtonPanel: () => useHasButtonPanel,
  useHasCaptionPanel: () => useHasCaptionPanel,
  useHasColorPanel: () => useHasColorPanel,
  useHasHeadingPanel: () => useHasHeadingPanel,
  useHasLinkPanel: () => useHasLinkPanel,
  useHasTextPanel: () => useHasTextPanel
});
module.exports = __toCommonJS(color_panel_exports);
var import_components = require("@wordpress/components");
var import_element = require("@wordpress/element");
var import_i18n = require("@wordpress/i18n");
var import_color_gradient_dropdown_item = __toESM(require("./color-gradient-dropdown-item.cjs"));
var import_hooks = require("./hooks.cjs");
var import_utils = require("./utils.cjs");
var import_object = require("../../utils/object.cjs");
var import_color_values = require("../../utils/color-values.cjs");
var import_jsx_runtime = require("react/jsx-runtime");
function useHasColorPanel(settings) {
  const hasLinkPanel = useHasLinkPanel(settings);
  const hasHeadingPanel = useHasHeadingPanel(settings);
  const hasButtonPanel = useHasButtonPanel(settings);
  const hasCaptionPanel = useHasCaptionPanel(settings);
  return hasLinkPanel || hasHeadingPanel || hasButtonPanel || hasCaptionPanel;
}
function useHasTextPanel(settings) {
  const colors = (0, import_hooks.useColorsPerOrigin)(settings);
  return settings?.color?.text && (colors?.length > 0 || settings?.color?.custom);
}
function useHasLinkPanel(settings) {
  const colors = (0, import_hooks.useColorsPerOrigin)(settings);
  return settings?.color?.link && (colors?.length > 0 || settings?.color?.custom);
}
function useHasCaptionPanel(settings) {
  const colors = (0, import_hooks.useColorsPerOrigin)(settings);
  return settings?.color?.caption && (colors?.length > 0 || settings?.color?.custom);
}
function useHasHeadingPanel(settings) {
  const colors = (0, import_hooks.useColorsPerOrigin)(settings);
  const gradients = (0, import_hooks.useGradientsPerOrigin)(settings);
  return settings?.color?.heading && (colors?.length > 0 || settings?.color?.custom || gradients?.length > 0 || settings?.color?.customGradient);
}
function useHasButtonPanel(settings) {
  const colors = (0, import_hooks.useColorsPerOrigin)(settings);
  const gradients = (0, import_hooks.useGradientsPerOrigin)(settings);
  return settings?.color?.button && (colors?.length > 0 || settings?.color?.custom || gradients?.length > 0 || settings?.color?.customGradient);
}
function useHasBackgroundColorPanel(settings) {
  const colors = (0, import_hooks.useColorsPerOrigin)(settings);
  const gradients = (0, import_hooks.useGradientsPerOrigin)(settings);
  return settings?.color?.background && (colors?.length > 0 || settings?.color?.custom || gradients?.length > 0 || settings?.color?.customGradient);
}
function ColorToolsPanel({
  resetAllFilter,
  onChange,
  value,
  panelId,
  children,
  label
}) {
  const dropdownMenuProps = (0, import_utils.useToolsPanelDropdownMenuProps)();
  const resetAll = () => {
    const updatedValue = resetAllFilter(value);
    onChange(updatedValue);
  };
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    import_components.__experimentalToolsPanel,
    {
      label: label || (0, import_i18n.__)("Elements"),
      resetAll,
      panelId,
      hasInnerWrapper: true,
      headingLevel: 3,
      className: "color-block-support-panel",
      __experimentalFirstVisibleItemClass: "first",
      __experimentalLastVisibleItemClass: "last",
      dropdownMenuProps,
      children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "color-block-support-panel__inner-wrapper", children })
    }
  );
}
var DEFAULT_CONTROLS = {
  link: true,
  heading: true,
  button: true,
  caption: true
};
function ColorPanel({
  as: Wrapper = ColorToolsPanel,
  value,
  onChange,
  inheritedValue = value,
  settings,
  panelId,
  defaultControls = DEFAULT_CONTROLS,
  label,
  children,
  contrastWarning
}) {
  const {
    colors,
    gradients,
    areCustomSolidsEnabled,
    areCustomGradientsEnabled,
    hasSolidColors,
    hasGradientColors,
    allColors,
    decodeValue,
    encodeGradientValue
  } = (0, import_hooks.useColorGradientSettings)(settings);
  const showLinkPanel = useHasLinkPanel(settings);
  const linkColor = decodeValue(
    inheritedValue?.elements?.link?.color?.text
  );
  const userLinkColor = decodeValue(value?.elements?.link?.color?.text);
  const setLinkColor = (newColor, newSlug) => {
    onChange(
      (0, import_object.setImmutably)(
        value,
        ["elements", "link", "color", "text"],
        (0, import_color_values.encodeColorValueWithPalette)(allColors, newColor, newSlug)
      )
    );
  };
  const hoverLinkColor = decodeValue(
    inheritedValue?.elements?.link?.[":hover"]?.color?.text
  );
  const userHoverLinkColor = decodeValue(
    value?.elements?.link?.[":hover"]?.color?.text
  );
  const setHoverLinkColor = (newColor, newSlug) => {
    onChange(
      (0, import_object.setImmutably)(
        value,
        ["elements", "link", ":hover", "color", "text"],
        (0, import_color_values.encodeColorValueWithPalette)(allColors, newColor, newSlug)
      )
    );
  };
  const hasLink = () => !!userLinkColor || !!userHoverLinkColor;
  const resetLink = () => {
    let newValue = (0, import_object.setImmutably)(
      value,
      ["elements", "link", ":hover", "color", "text"],
      void 0
    );
    newValue = (0, import_object.setImmutably)(
      newValue,
      ["elements", "link", "color", "text"],
      void 0
    );
    onChange(newValue);
  };
  const showCaptionPanel = useHasCaptionPanel(settings);
  const showButtonPanel = useHasButtonPanel(settings);
  const showHeadingPanel = useHasHeadingPanel(settings);
  const elements = (0, import_element.useMemo)(
    () => [
      {
        name: "caption",
        label: (0, import_i18n.__)("Captions"),
        showPanel: showCaptionPanel
      },
      {
        name: "button",
        label: (0, import_i18n.__)("Button"),
        showPanel: showButtonPanel
      },
      {
        name: "heading",
        label: (0, import_i18n.__)("Heading"),
        showPanel: showHeadingPanel
      },
      {
        name: "h1",
        label: (0, import_i18n.__)("H1"),
        showPanel: showHeadingPanel
      },
      {
        name: "h2",
        label: (0, import_i18n.__)("H2"),
        showPanel: showHeadingPanel
      },
      {
        name: "h3",
        label: (0, import_i18n.__)("H3"),
        showPanel: showHeadingPanel
      },
      {
        name: "h4",
        label: (0, import_i18n.__)("H4"),
        showPanel: showHeadingPanel
      },
      {
        name: "h5",
        label: (0, import_i18n.__)("H5"),
        showPanel: showHeadingPanel
      },
      {
        name: "h6",
        label: (0, import_i18n.__)("H6"),
        showPanel: showHeadingPanel
      }
    ],
    [showCaptionPanel, showButtonPanel, showHeadingPanel]
  );
  const resetAllFilter = (0, import_element.useCallback)(
    (previousValue) => {
      return {
        ...previousValue,
        elements: {
          ...previousValue?.elements,
          link: {
            ...previousValue?.elements?.link,
            color: void 0,
            ":hover": {
              color: void 0
            }
          },
          ...elements.reduce((acc, element) => {
            return {
              ...acc,
              [element.name]: {
                ...previousValue?.elements?.[element.name],
                color: void 0
              }
            };
          }, {})
        }
      };
    },
    [elements]
  );
  const items = [
    showLinkPanel && {
      key: "link",
      label: (0, import_i18n.__)("Link"),
      hasValue: hasLink,
      resetValue: resetLink,
      isShownByDefault: defaultControls.link,
      indicators: [linkColor, hoverLinkColor],
      contrastWarning,
      tabs: [
        {
          key: "link",
          label: (0, import_i18n.__)("Default"),
          inheritedValue: linkColor,
          inheritedSlug: (0, import_color_values.extractPresetSlug)(
            inheritedValue?.elements?.link?.color?.text,
            "color"
          ),
          setValue: setLinkColor,
          userValue: userLinkColor
        },
        {
          key: "hover",
          label: (0, import_i18n.__)("Hover"),
          inheritedValue: hoverLinkColor,
          inheritedSlug: (0, import_color_values.extractPresetSlug)(
            inheritedValue?.elements?.link?.[":hover"]?.color?.text,
            "color"
          ),
          setValue: setHoverLinkColor,
          userValue: userHoverLinkColor
        }
      ]
    }
  ].filter(Boolean);
  elements.forEach(({ name, label: elementLabel, showPanel }) => {
    if (!showPanel) {
      return;
    }
    const elementBackgroundColor = decodeValue(
      inheritedValue?.elements?.[name]?.color?.background
    );
    const elementGradient = decodeValue(
      inheritedValue?.elements?.[name]?.color?.gradient
    );
    const elementTextColor = decodeValue(
      inheritedValue?.elements?.[name]?.color?.text
    );
    const elementBackgroundUserColor = decodeValue(
      value?.elements?.[name]?.color?.background
    );
    const elementGradientUserColor = decodeValue(
      value?.elements?.[name]?.color?.gradient
    );
    const elementTextUserColor = decodeValue(
      value?.elements?.[name]?.color?.text
    );
    const hasElement = () => !!(elementTextUserColor || elementBackgroundUserColor || elementGradientUserColor);
    const resetElement = () => {
      const newValue = (0, import_object.setImmutably)(
        value,
        ["elements", name, "color", "background"],
        void 0
      );
      newValue.elements[name].color.gradient = void 0;
      newValue.elements[name].color.text = void 0;
      onChange(newValue);
    };
    const setElementTextColor = (newTextColor, newSlug) => {
      onChange(
        (0, import_object.setImmutably)(
          value,
          ["elements", name, "color", "text"],
          (0, import_color_values.encodeColorValueWithPalette)(
            allColors,
            newTextColor,
            newSlug
          )
        )
      );
    };
    const setElementBackgroundColor = (newBackgroundColor, newSlug) => {
      const newValue = (0, import_object.setImmutably)(
        value,
        ["elements", name, "color", "background"],
        (0, import_color_values.encodeColorValueWithPalette)(
          allColors,
          newBackgroundColor,
          newSlug
        )
      );
      newValue.elements[name].color.gradient = void 0;
      onChange(newValue);
    };
    const setElementGradient = (newGradient) => {
      const newValue = (0, import_object.setImmutably)(
        value,
        ["elements", name, "color", "gradient"],
        encodeGradientValue(newGradient)
      );
      newValue.elements[name].color.background = void 0;
      onChange(newValue);
    };
    const supportsBackground = name !== "caption";
    items.push({
      key: name,
      label: elementLabel,
      hasValue: hasElement,
      resetValue: resetElement,
      isShownByDefault: defaultControls[name],
      indicators: supportsBackground ? [
        elementTextColor,
        elementGradient ?? elementBackgroundColor
      ] : [elementTextColor],
      tabs: [
        hasSolidColors && {
          key: "text",
          label: (0, import_i18n.__)("Text"),
          inheritedValue: elementTextColor,
          inheritedSlug: (0, import_color_values.extractPresetSlug)(
            inheritedValue?.elements?.[name]?.color?.text,
            "color"
          ),
          setValue: setElementTextColor,
          userValue: elementTextUserColor
        },
        hasSolidColors && supportsBackground && {
          key: "background",
          label: (0, import_i18n.__)("Background"),
          inheritedValue: elementBackgroundColor,
          inheritedSlug: (0, import_color_values.extractPresetSlug)(
            inheritedValue?.elements?.[name]?.color?.background,
            "color"
          ),
          setValue: setElementBackgroundColor,
          userValue: elementBackgroundUserColor
        },
        hasGradientColors && supportsBackground && {
          key: "gradient",
          label: (0, import_i18n.__)("Gradient"),
          inheritedValue: elementGradient,
          setValue: setElementGradient,
          userValue: elementGradientUserColor,
          isGradient: true
        }
      ].filter(Boolean)
    });
  });
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
    Wrapper,
    {
      resetAllFilter,
      value,
      onChange,
      panelId,
      label,
      children: [
        items.map((item) => {
          const { key, ...restItem } = item;
          return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_color_gradient_dropdown_item.default,
            {
              ...restItem,
              colorGradientControlSettings: {
                colors,
                disableCustomColors: !areCustomSolidsEnabled,
                gradients,
                disableCustomGradients: !areCustomGradientsEnabled
              },
              panelId
            },
            key
          );
        }),
        children
      ]
    }
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  ColorToolsPanel,
  useHasBackgroundColorPanel,
  useHasButtonPanel,
  useHasCaptionPanel,
  useHasColorPanel,
  useHasHeadingPanel,
  useHasLinkPanel,
  useHasTextPanel
});
//# sourceMappingURL=color-panel.cjs.map
