// packages/block-editor/src/hooks/elements.js
import { getBlockSupport } from "@wordpress/blocks";
import { useMemo, useCallback } from "@wordpress/element";
import { useSelect } from "@wordpress/data";
import { __ } from "@wordpress/i18n";
import InspectorControls from "../components/inspector-controls/index.mjs";
import {
  useHasColorPanel,
  default as StylesColorPanel
} from "../components/global-styles/color-panel.mjs";
import { cleanEmptyObject } from "./utils.mjs";
import { store as blockEditorStore } from "../store/index.mjs";
import { COLOR_SUPPORT_KEY } from "./color.mjs";
import useBlockColorContrastWarning from "./contrast-checker.mjs";
import {
  getStyleForState,
  isDefaultBlockStyleState,
  setStyleForState,
  useBlockStyleState
} from "./block-style-state.mjs";
import { jsx } from "react/jsx-runtime";
function ElementsInspectorControl({ children, resetAllFilter }) {
  const attributesResetAllFilter = useCallback(
    (attributes) => {
      const updatedStyle = resetAllFilter(attributes.style);
      return {
        ...attributes,
        style: cleanEmptyObject(updatedStyle)
      };
    },
    [resetAllFilter]
  );
  return /* @__PURE__ */ jsx(
    InspectorControls,
    {
      group: "elements",
      resetAllFilter: attributesResetAllFilter,
      children
    }
  );
}
function ElementsEdit({
  clientId,
  name,
  setAttributes,
  settings,
  asWrapper,
  label,
  defaultControls
}) {
  const selectedState = useBlockStyleState();
  const isEnabled = useHasColorPanel(settings);
  const style = useSelect(
    (select) => {
      if (!isEnabled) {
        return void 0;
      }
      const attributes = select(blockEditorStore).getBlockAttributes(clientId);
      return attributes?.style;
    },
    [clientId, isEnabled]
  );
  const isStateSelected = !isDefaultBlockStyleState(selectedState);
  const value = useMemo(() => {
    if (isStateSelected) {
      return getStyleForState(style, selectedState);
    }
    return style;
  }, [isStateSelected, selectedState, style]);
  const onChange = isStateSelected ? (newStyle) => {
    setAttributes({
      style: setStyleForState(style, selectedState, newStyle)
    });
  } : (newStyle) => {
    setAttributes({ style: cleanEmptyObject(newStyle) });
  };
  const enableContrastChecking = !isStateSelected && !!value?.elements?.link?.color?.text && settings?.color?.link && false !== getBlockSupport(name, [
    COLOR_SUPPORT_KEY,
    "enableContrastChecker"
  ]);
  const contrastWarning = useBlockColorContrastWarning({
    clientId,
    name,
    enabled: !!enableContrastChecking,
    checkTextColor: false,
    messageOverride: __(
      "This link color has poor contrast against the background. Consider increasing contrast."
    )
  });
  if (!isEnabled) {
    return null;
  }
  defaultControls = defaultControls ? defaultControls : getBlockSupport(name, [
    COLOR_SUPPORT_KEY,
    "__experimentalDefaultControls"
  ]);
  const Wrapper = asWrapper || ElementsInspectorControl;
  return /* @__PURE__ */ jsx(
    StylesColorPanel,
    {
      as: Wrapper,
      panelId: clientId,
      settings,
      value,
      onChange,
      defaultControls,
      label,
      contrastWarning
    }
  );
}
export {
  ElementsEdit
};
//# sourceMappingURL=elements.mjs.map
