// packages/block-editor/src/components/global-styles/color-gradient-dropdown-item.js
import clsx from "clsx";
import {
  __experimentalToolsPanelItem as ToolsPanelItem,
  __experimentalHStack as HStack,
  __experimentalZStack as ZStack,
  __experimentalDropdownContentWrapper as DropdownContentWrapper,
  ColorIndicator,
  Flex,
  FlexItem,
  Dropdown,
  Button,
  privateApis as componentsPrivateApis
} from "@wordpress/components";
import { useRef } from "@wordpress/element";
import { __ } from "@wordpress/i18n";
import { reset as resetIcon, caution as cautionIcon } from "@wordpress/icons";
import ColorGradientControl from "../colors-gradients/control.mjs";
import { unlock } from "../../lock-unlock.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var { Tabs } = unlock(componentsPrivateApis);
function DropdownContent({
  tabs,
  colorGradientControlSettings,
  contrastWarning
}) {
  const { key: firstTabKey, ...firstTab } = tabs[0] ?? {};
  const defaultTabId = tabs.find(
    (tab) => tab.userValue !== void 0
  )?.key;
  return /* @__PURE__ */ jsx(DropdownContentWrapper, { paddingSize: "none", children: /* @__PURE__ */ jsxs("div", { className: "block-editor-panel-color-gradient-settings__dropdown-content", children: [
    tabs.length === 1 && /* @__PURE__ */ jsx(
      ColorGradientTab,
      {
        ...firstTab,
        colorGradientControlSettings,
        contrastWarning
      },
      firstTabKey
    ),
    tabs.length > 1 && /* @__PURE__ */ jsxs(Tabs, { defaultTabId, children: [
      /* @__PURE__ */ jsx(Tabs.TabList, { children: tabs.map((tab) => /* @__PURE__ */ jsx(Tabs.Tab, { tabId: tab.key, children: tab.label }, tab.key)) }),
      tabs.map((tab) => {
        const { key: tabKey, ...restTabProps } = tab;
        return /* @__PURE__ */ jsx(
          Tabs.TabPanel,
          {
            tabId: tabKey,
            focusable: false,
            children: /* @__PURE__ */ jsx(
              ColorGradientTab,
              {
                ...restTabProps,
                colorGradientControlSettings,
                contrastWarning
              },
              tabKey
            )
          },
          tabKey
        );
      })
    ] })
  ] }) });
}
var popoverProps = {
  placement: "left-start",
  offset: 36,
  shift: true
};
var LabeledColorIndicators = ({ indicators, label }) => /* @__PURE__ */ jsxs(HStack, { justify: "flex-start", children: [
  /* @__PURE__ */ jsx(ZStack, { isLayered: false, offset: -8, children: indicators.map((indicator, index) => /* @__PURE__ */ jsx(Flex, { expanded: false, children: /* @__PURE__ */ jsx(ColorIndicator, { colorValue: indicator }) }, index)) }),
  /* @__PURE__ */ jsx(FlexItem, { className: "block-editor-panel-color-gradient-settings__color-name", children: label })
] });
function ColorGradientTab({
  isGradient,
  inheritedValue,
  inheritedSlug,
  userValue,
  setValue,
  colorGradientControlSettings,
  contrastWarning
}) {
  return /* @__PURE__ */ jsx(
    ColorGradientControl,
    {
      ...colorGradientControlSettings,
      showTitle: false,
      enableAlpha: true,
      __experimentalIsRenderedInSidebar: true,
      colorValue: isGradient ? void 0 : inheritedValue,
      colorSlug: isGradient ? void 0 : inheritedSlug,
      gradientValue: isGradient ? inheritedValue : void 0,
      onColorChange: isGradient ? void 0 : setValue,
      onGradientChange: isGradient ? setValue : void 0,
      clearable: inheritedValue === userValue,
      headingLevel: 3,
      noticeProps: !isGradient && contrastWarning ? {
        children: contrastWarning,
        status: "warning",
        spokenMessage: null,
        className: "block-editor-panel-color-gradient-settings__contrast-notice"
      } : void 0
    }
  );
}
function ColorGradientDropdownItem({
  label,
  hasValue,
  resetValue,
  isShownByDefault,
  indicators,
  tabs,
  colorGradientControlSettings,
  panelId,
  contrastWarning,
  className = "block-editor-tools-panel-color-gradient-settings__item"
}) {
  const colorGradientDropdownButtonRef = useRef(void 0);
  return /* @__PURE__ */ jsx(
    ToolsPanelItem,
    {
      className: clsx("block-editor-color-gradient-item", className),
      hasValue,
      label,
      onDeselect: resetValue,
      isShownByDefault,
      panelId,
      children: /* @__PURE__ */ jsx(
        Dropdown,
        {
          popoverProps,
          className: "block-editor-tools-panel-color-gradient-settings__dropdown",
          renderToggle: ({ onToggle, isOpen }) => {
            const toggleProps = {
              onClick: onToggle,
              className: clsx(
                "block-editor-panel-color-gradient-settings__dropdown",
                {
                  "is-open": isOpen,
                  "has-contrast-warning": !!contrastWarning
                }
              ),
              "aria-expanded": isOpen,
              ref: colorGradientDropdownButtonRef
            };
            return /* @__PURE__ */ jsxs(Fragment, { children: [
              /* @__PURE__ */ jsx(Button, { ...toggleProps, __next40pxDefaultSize: true, children: /* @__PURE__ */ jsx(
                LabeledColorIndicators,
                {
                  indicators,
                  label
                }
              ) }),
              hasValue() && /* @__PURE__ */ jsx(
                Button,
                {
                  __next40pxDefaultSize: true,
                  label: __("Reset"),
                  className: "block-editor-panel-color-gradient-settings__reset",
                  size: "small",
                  icon: resetIcon,
                  onClick: () => {
                    resetValue();
                    if (isOpen) {
                      onToggle();
                    }
                    colorGradientDropdownButtonRef.current?.focus();
                  }
                }
              ),
              contrastWarning && // An icon-only warning that stays visible while a
              // contrast warning is in effect. It is not a menu;
              // activating it opens the color picker popover where
              // the full warning notice is shown, so the indicator
              // doubles as a shortcut to where the issue is fixed.
              /* @__PURE__ */ jsx(
                Button,
                {
                  __next40pxDefaultSize: true,
                  size: "small",
                  icon: cautionIcon,
                  label: __("Low contrast"),
                  className: "block-editor-panel-color-gradient-settings__contrast-warning",
                  "aria-expanded": isOpen,
                  onClick: onToggle
                }
              )
            ] });
          },
          renderContent: () => /* @__PURE__ */ jsx(
            DropdownContent,
            {
              tabs,
              colorGradientControlSettings,
              contrastWarning
            }
          )
        }
      )
    }
  );
}
export {
  ColorGradientDropdownItem as default
};
//# sourceMappingURL=color-gradient-dropdown-item.mjs.map
