// packages/block-editor/src/components/global-styles/background-panel.js
import {
  __experimentalToolsPanel as ToolsPanel,
  __experimentalToolsPanelItem as ToolsPanelItem
} from "@wordpress/components";
import { useCallback } from "@wordpress/element";
import { __ } from "@wordpress/i18n";
import BackgroundImageControl from "../background-image-control/index.mjs";
import ColorGradientDropdownItem from "./color-gradient-dropdown-item.mjs";
import { useHasBackgroundColorPanel } from "./color-panel.mjs";
import { useColorGradientSettings } from "./hooks.mjs";
import { useToolsPanelDropdownMenuProps } from "./utils.mjs";
import { setImmutably } from "../../utils/object.mjs";
import {
  extractPresetSlug,
  encodeColorValueWithPalette
} from "../../utils/color-values.mjs";
import { jsx, jsxs } from "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 = 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 = useToolsPanelDropdownMenuProps();
  const resetAll = () => {
    const updatedValue = resetAllFilter(value);
    onChange(updatedValue);
  };
  return /* @__PURE__ */ jsx(
    ToolsPanel,
    {
      label: headerLabel,
      resetAll,
      panelId,
      hasInnerWrapper: true,
      className: "background-block-support-panel",
      __experimentalFirstVisibleItemClass: "first",
      __experimentalLastVisibleItemClass: "last",
      dropdownMenuProps,
      children: /* @__PURE__ */ 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 = __("Background"),
  contrastWarning
}) {
  const {
    colors,
    gradients,
    allColors,
    areCustomSolidsEnabled,
    areCustomGradientsEnabled,
    hasSolidColors,
    hasGradientColors,
    decodeValue,
    encodeGradientValue
  } = useColorGradientSettings(settings);
  const hasBackgroundGradientControl = useHasBackgroundControl(
    settings,
    "gradient"
  );
  const hasColorPanelBackgroundSupport = useHasBackgroundColorPanel(settings);
  const showBackgroundColorControl = hasColorPanelBackgroundSupport && hasSolidColors;
  const showBackgroundGradientControl = hasGradientColors && hasBackgroundGradientControl;
  const showLegacyColorGradientControl = hasColorPanelBackgroundSupport && hasGradientColors && !hasBackgroundGradientControl;
  const showBackgroundImageControl = useHasBackgroundControl(
    settings,
    "backgroundImage"
  );
  const resetAllFilter = 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(
    setImmutably(
      value,
      ["background", "backgroundImage"],
      void 0
    )
  );
  const resetGradient = () => {
    let newValue = setImmutably(
      value,
      ["background", "gradient"],
      void 0
    );
    newValue = 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 = setImmutably(
      value,
      ["color", "background"],
      encodeColorValueWithPalette(allColors, newColor, newSlug)
    );
    if (showLegacyColorGradientControl) {
      newValue.color.gradient = void 0;
    }
    onChange(newValue);
  };
  const resetBackgroundColor = () => {
    const newValue = 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 = setImmutably(
      value,
      ["color", "gradient"],
      encodeGradientValue(newGradient)
    );
    newValue.color.background = void 0;
    onChange(newValue);
  };
  const resetLegacyColorGradient = () => onChange(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 = setImmutably(
      value,
      ["background", "gradient"],
      encodeGradientValue(newGradient)
    );
    newValue = setImmutably(newValue, ["color", "gradient"], void 0);
    onChange(newValue);
  };
  return /* @__PURE__ */ jsxs(
    Wrapper,
    {
      resetAllFilter,
      value,
      onChange,
      panelId,
      headerLabel,
      children: [
        showBackgroundImageControl && /* @__PURE__ */ jsx(
          ToolsPanelItem,
          {
            className: "block-editor-color-gradient-item",
            hasValue: () => hasBackgroundImageValue(value),
            label: __("Image"),
            onDeselect: resetBackground,
            isShownByDefault: defaultControls.backgroundImage,
            panelId,
            children: /* @__PURE__ */ jsx(
              BackgroundImageControl,
              {
                value,
                onChange,
                settings,
                inheritedValue,
                defaultControls,
                defaultValues
              }
            )
          }
        ),
        showBackgroundColorControl && /* @__PURE__ */ jsx(
          ColorGradientDropdownItem,
          {
            label: __("Color"),
            hasValue: () => hasBackgroundColorValue(value),
            resetValue: resetBackgroundColor,
            isShownByDefault: defaultControls.backgroundColor,
            indicators: [userBackgroundColor ?? backgroundColor],
            contrastWarning,
            tabs: [
              {
                key: "background",
                label: __("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: extractPresetSlug(
                  value?.color?.background,
                  "color"
                ) ?? extractPresetSlug(
                  inheritedValue?.color?.background,
                  "color"
                ),
                setValue: setBackgroundColor,
                userValue: userBackgroundColor
              }
            ],
            colorGradientControlSettings: {
              colors,
              disableCustomColors: !areCustomSolidsEnabled
            },
            panelId
          }
        ),
        showBackgroundGradientControl && /* @__PURE__ */ jsx(
          ColorGradientDropdownItem,
          {
            label: __("Gradient"),
            hasValue: () => hasBackgroundGradientValue(value),
            resetValue: resetGradient,
            isShownByDefault: defaultControls.gradient,
            indicators: [currentGradient],
            tabs: [
              {
                key: "gradient",
                label: __("Gradient"),
                inheritedValue: currentGradient ?? inheritedGradient,
                setValue: setGradient,
                userValue: currentGradient,
                isGradient: true
              }
            ],
            colorGradientControlSettings: {
              gradients,
              disableCustomGradients: !areCustomGradientsEnabled
            },
            panelId
          }
        ),
        showLegacyColorGradientControl && /* @__PURE__ */ jsx(
          ColorGradientDropdownItem,
          {
            label: __("Gradient"),
            hasValue: () => hasLegacyColorGradientValue(value),
            resetValue: resetLegacyColorGradient,
            isShownByDefault: defaultControls.gradient,
            indicators: [
              userLegacyColorGradient ?? legacyColorGradient
            ],
            tabs: [
              {
                key: "gradient",
                label: __("Gradient"),
                inheritedValue: userLegacyColorGradient ?? legacyColorGradient,
                setValue: setLegacyColorGradient,
                userValue: userLegacyColorGradient,
                isGradient: true
              }
            ],
            colorGradientControlSettings: {
              gradients,
              disableCustomGradients: !areCustomGradientsEnabled
            },
            panelId
          }
        )
      ]
    }
  );
}
export {
  BackgroundToolsPanel,
  BackgroundImagePanel as default,
  hasBackgroundColorValue,
  hasBackgroundGradientValue,
  hasBackgroundImageValue,
  hasBackgroundSizeValue,
  hasLegacyColorGradientValue,
  useHasBackgroundControl,
  useHasBackgroundPanel
};
//# sourceMappingURL=background-panel.mjs.map
