"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/background-panel.js
var background_panel_exports = {};
__export(background_panel_exports, {
  BackgroundToolsPanel: () => BackgroundToolsPanel,
  default: () => BackgroundImagePanel,
  hasBackgroundColorValue: () => hasBackgroundColorValue,
  hasBackgroundGradientValue: () => hasBackgroundGradientValue,
  hasBackgroundImageValue: () => hasBackgroundImageValue,
  hasBackgroundSizeValue: () => hasBackgroundSizeValue,
  hasLegacyColorGradientValue: () => hasLegacyColorGradientValue,
  useHasBackgroundControl: () => useHasBackgroundControl,
  useHasBackgroundPanel: () => useHasBackgroundPanel
});
module.exports = __toCommonJS(background_panel_exports);
var import_components = require("@wordpress/components");
var import_element = require("@wordpress/element");
var import_i18n = require("@wordpress/i18n");
var import_background_image_control = __toESM(require("../background-image-control/index.cjs"));
var import_color_gradient_dropdown_item = __toESM(require("./color-gradient-dropdown-item.cjs"));
var import_color_panel = require("./color-panel.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");
var DEFAULT_CONTROLS = {
  backgroundImage: true,
  backgroundColor: true,
  gradient: true
};
function useHasBackgroundControl(settings, feature) {
  return settings?.background?.[feature];
}
function useHasBackgroundPanel(settings) {
  const hasBackgroundColor = (0, import_color_panel.useHasBackgroundColorPanel)(settings);
  const { backgroundImage, gradient } = settings?.background || {};
  return backgroundImage || gradient || hasBackgroundColor;
}
function hasBackgroundSizeValue(style) {
  return style?.background?.backgroundPosition !== void 0 || style?.background?.backgroundSize !== void 0;
}
function hasBackgroundImageValue(style) {
  return !!style?.background?.backgroundImage?.id || // Supports url() string values in theme.json.
  "string" === typeof style?.background?.backgroundImage || !!style?.background?.backgroundImage?.url;
}
function hasBackgroundGradientValue(style) {
  return "string" === typeof style?.background?.gradient && style?.background?.gradient !== "";
}
function hasBackgroundColorValue(style) {
  return !!style?.color?.background;
}
function hasLegacyColorGradientValue(style) {
  return !!style?.color?.gradient;
}
function BackgroundToolsPanel({
  resetAllFilter,
  onChange,
  value,
  panelId,
  children,
  headerLabel
}) {
  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: headerLabel,
      resetAll,
      panelId,
      hasInnerWrapper: true,
      className: "background-block-support-panel",
      __experimentalFirstVisibleItemClass: "first",
      __experimentalLastVisibleItemClass: "last",
      dropdownMenuProps,
      children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "background-block-support-panel__inner-wrapper", children })
    }
  );
}
function BackgroundImagePanel({
  as: Wrapper = BackgroundToolsPanel,
  value,
  onChange,
  inheritedValue = value,
  settings,
  panelId,
  defaultControls = DEFAULT_CONTROLS,
  defaultValues = {},
  headerLabel = (0, import_i18n.__)("Background"),
  contrastWarning
}) {
  const {
    colors,
    gradients,
    allColors,
    areCustomSolidsEnabled,
    areCustomGradientsEnabled,
    hasSolidColors,
    hasGradientColors,
    decodeValue,
    encodeGradientValue
  } = (0, import_hooks.useColorGradientSettings)(settings);
  const hasBackgroundGradientControl = useHasBackgroundControl(
    settings,
    "gradient"
  );
  const hasColorPanelBackgroundSupport = (0, import_color_panel.useHasBackgroundColorPanel)(settings);
  const showBackgroundColorControl = hasColorPanelBackgroundSupport && hasSolidColors;
  const showBackgroundGradientControl = hasGradientColors && hasBackgroundGradientControl;
  const showLegacyColorGradientControl = hasColorPanelBackgroundSupport && hasGradientColors && !hasBackgroundGradientControl;
  const showBackgroundImageControl = useHasBackgroundControl(
    settings,
    "backgroundImage"
  );
  const resetAllFilter = (0, import_element.useCallback)(
    (previousValue) => {
      const clearsColorBackground = showBackgroundColorControl;
      const clearsColorGradient = hasBackgroundGradientControl || showLegacyColorGradientControl;
      if (!clearsColorBackground && !clearsColorGradient) {
        return { ...previousValue, background: {} };
      }
      return {
        ...previousValue,
        background: {},
        color: {
          ...previousValue?.color,
          ...clearsColorBackground && { background: void 0 },
          ...clearsColorGradient && { gradient: void 0 }
        }
      };
    },
    [
      hasBackgroundGradientControl,
      showBackgroundColorControl,
      showLegacyColorGradientControl
    ]
  );
  if (!showBackgroundImageControl && !showBackgroundColorControl && !showBackgroundGradientControl && !showLegacyColorGradientControl) {
    return null;
  }
  const resetBackground = () => onChange(
    (0, import_object.setImmutably)(
      value,
      ["background", "backgroundImage"],
      void 0
    )
  );
  const resetGradient = () => {
    let newValue = (0, import_object.setImmutably)(
      value,
      ["background", "gradient"],
      void 0
    );
    newValue = (0, import_object.setImmutably)(newValue, ["color", "gradient"], void 0);
    onChange(newValue);
  };
  const backgroundColor = decodeValue(inheritedValue?.color?.background);
  const userBackgroundColor = decodeValue(value?.color?.background);
  const setBackgroundColor = (newColor, newSlug) => {
    const newValue = (0, import_object.setImmutably)(
      value,
      ["color", "background"],
      (0, import_color_values.encodeColorValueWithPalette)(allColors, newColor, newSlug)
    );
    if (showLegacyColorGradientControl) {
      newValue.color.gradient = void 0;
    }
    onChange(newValue);
  };
  const resetBackgroundColor = () => {
    const newValue = (0, import_object.setImmutably)(
      value,
      ["color", "background"],
      void 0
    );
    if (showLegacyColorGradientControl) {
      newValue.color.gradient = void 0;
    }
    onChange(newValue);
  };
  const legacyColorGradient = decodeValue(inheritedValue?.color?.gradient);
  const userLegacyColorGradient = decodeValue(value?.color?.gradient);
  const setLegacyColorGradient = (newGradient) => {
    const newValue = (0, import_object.setImmutably)(
      value,
      ["color", "gradient"],
      encodeGradientValue(newGradient)
    );
    newValue.color.background = void 0;
    onChange(newValue);
  };
  const resetLegacyColorGradient = () => onChange((0, import_object.setImmutably)(value, ["color", "gradient"], void 0));
  const currentGradient = decodeValue(
    value?.background?.gradient ?? value?.color?.gradient
  );
  const inheritedGradient = decodeValue(
    inheritedValue?.background?.gradient ?? inheritedValue?.color?.gradient
  );
  const setGradient = (newGradient) => {
    let newValue = (0, import_object.setImmutably)(
      value,
      ["background", "gradient"],
      encodeGradientValue(newGradient)
    );
    newValue = (0, import_object.setImmutably)(newValue, ["color", "gradient"], void 0);
    onChange(newValue);
  };
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
    Wrapper,
    {
      resetAllFilter,
      value,
      onChange,
      panelId,
      headerLabel,
      children: [
        showBackgroundImageControl && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import_components.__experimentalToolsPanelItem,
          {
            className: "block-editor-color-gradient-item",
            hasValue: () => hasBackgroundImageValue(value),
            label: (0, import_i18n.__)("Image"),
            onDeselect: resetBackground,
            isShownByDefault: defaultControls.backgroundImage,
            panelId,
            children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
              import_background_image_control.default,
              {
                value,
                onChange,
                settings,
                inheritedValue,
                defaultControls,
                defaultValues
              }
            )
          }
        ),
        showBackgroundColorControl && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import_color_gradient_dropdown_item.default,
          {
            label: (0, import_i18n.__)("Color"),
            hasValue: () => hasBackgroundColorValue(value),
            resetValue: resetBackgroundColor,
            isShownByDefault: defaultControls.backgroundColor,
            indicators: [userBackgroundColor ?? backgroundColor],
            contrastWarning,
            tabs: [
              {
                key: "background",
                label: (0, import_i18n.__)("Color"),
                inheritedValue: userBackgroundColor ?? backgroundColor,
                // Resolve the slug from the same source as the
                // displayed value (user value first, then the
                // inherited fallback). For a block instance the
                // selection lives in `value` while `inheritedValue`
                // only holds the global styles fallback, so reading
                // the slug from `inheritedValue` alone would miss it
                // and two same-hex presets would both appear selected.
                inheritedSlug: (0, import_color_values.extractPresetSlug)(
                  value?.color?.background,
                  "color"
                ) ?? (0, import_color_values.extractPresetSlug)(
                  inheritedValue?.color?.background,
                  "color"
                ),
                setValue: setBackgroundColor,
                userValue: userBackgroundColor
              }
            ],
            colorGradientControlSettings: {
              colors,
              disableCustomColors: !areCustomSolidsEnabled
            },
            panelId
          }
        ),
        showBackgroundGradientControl && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import_color_gradient_dropdown_item.default,
          {
            label: (0, import_i18n.__)("Gradient"),
            hasValue: () => hasBackgroundGradientValue(value),
            resetValue: resetGradient,
            isShownByDefault: defaultControls.gradient,
            indicators: [currentGradient],
            tabs: [
              {
                key: "gradient",
                label: (0, import_i18n.__)("Gradient"),
                inheritedValue: currentGradient ?? inheritedGradient,
                setValue: setGradient,
                userValue: currentGradient,
                isGradient: true
              }
            ],
            colorGradientControlSettings: {
              gradients,
              disableCustomGradients: !areCustomGradientsEnabled
            },
            panelId
          }
        ),
        showLegacyColorGradientControl && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import_color_gradient_dropdown_item.default,
          {
            label: (0, import_i18n.__)("Gradient"),
            hasValue: () => hasLegacyColorGradientValue(value),
            resetValue: resetLegacyColorGradient,
            isShownByDefault: defaultControls.gradient,
            indicators: [
              userLegacyColorGradient ?? legacyColorGradient
            ],
            tabs: [
              {
                key: "gradient",
                label: (0, import_i18n.__)("Gradient"),
                inheritedValue: userLegacyColorGradient ?? legacyColorGradient,
                setValue: setLegacyColorGradient,
                userValue: userLegacyColorGradient,
                isGradient: true
              }
            ],
            colorGradientControlSettings: {
              gradients,
              disableCustomGradients: !areCustomGradientsEnabled
            },
            panelId
          }
        )
      ]
    }
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  BackgroundToolsPanel,
  hasBackgroundColorValue,
  hasBackgroundGradientValue,
  hasBackgroundImageValue,
  hasBackgroundSizeValue,
  hasLegacyColorGradientValue,
  useHasBackgroundControl,
  useHasBackgroundPanel
});
//# sourceMappingURL=background-panel.cjs.map
