// packages/block-editor/src/components/colors-gradients/dropdown.js
import clsx from "clsx";
import {
  Button,
  ColorIndicator,
  Dropdown,
  FlexItem,
  __experimentalDropdownContentWrapper as DropdownContentWrapper,
  __experimentalHStack as HStack,
  __experimentalToolsPanelItem as ToolsPanelItem
} from "@wordpress/components";
import { useRef } from "@wordpress/element";
import { __ } from "@wordpress/i18n";
import { reset as resetIcon } from "@wordpress/icons";
import ColorGradientControl from "./control";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var WithToolsPanelItem = ({ setting, children, panelId, ...props }) => {
  const clearValue = () => {
    if (setting.colorValue) {
      setting.onColorChange();
    } else if (setting.gradientValue) {
      setting.onGradientChange();
    }
  };
  return /* @__PURE__ */ jsx(
    ToolsPanelItem,
    {
      hasValue: () => {
        return !!setting.colorValue || !!setting.gradientValue;
      },
      label: setting.label,
      onDeselect: clearValue,
      isShownByDefault: setting.isShownByDefault !== void 0 ? setting.isShownByDefault : true,
      ...props,
      className: "block-editor-tools-panel-color-gradient-settings__item",
      panelId,
      resetAllFilter: setting.resetAllFilter,
      children
    }
  );
};
var LabeledColorIndicator = ({ colorValue, label }) => /* @__PURE__ */ jsxs(HStack, { justify: "flex-start", children: [
  /* @__PURE__ */ jsx(
    ColorIndicator,
    {
      className: "block-editor-panel-color-gradient-settings__color-indicator",
      colorValue
    }
  ),
  /* @__PURE__ */ jsx(
    FlexItem,
    {
      className: "block-editor-panel-color-gradient-settings__color-name",
      title: label,
      children: label
    }
  )
] });
var renderToggle = (settings) => ({ onToggle, isOpen }) => {
  const {
    clearable,
    colorValue,
    gradientValue,
    onColorChange,
    onGradientChange,
    label
  } = settings;
  const colorButtonRef = useRef(void 0);
  const toggleProps = {
    onClick: onToggle,
    className: clsx(
      "block-editor-panel-color-gradient-settings__dropdown",
      { "is-open": isOpen }
    ),
    "aria-expanded": isOpen,
    ref: colorButtonRef
  };
  const clearValue = () => {
    if (colorValue) {
      onColorChange();
    } else if (gradientValue) {
      onGradientChange();
    }
  };
  const value = colorValue ?? gradientValue;
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx(Button, { __next40pxDefaultSize: true, ...toggleProps, children: /* @__PURE__ */ jsx(
      LabeledColorIndicator,
      {
        colorValue: value,
        label
      }
    ) }),
    clearable && value && /* @__PURE__ */ jsx(
      Button,
      {
        __next40pxDefaultSize: true,
        label: __("Reset"),
        className: "block-editor-panel-color-gradient-settings__reset",
        size: "small",
        icon: resetIcon,
        onClick: () => {
          clearValue();
          if (isOpen) {
            onToggle();
          }
          colorButtonRef.current?.focus();
        }
      }
    )
  ] });
};
function ColorGradientSettingsDropdown({
  colors,
  disableCustomColors,
  disableCustomGradients,
  enableAlpha,
  gradients,
  settings,
  __experimentalIsRenderedInSidebar,
  ...props
}) {
  let popoverProps;
  if (__experimentalIsRenderedInSidebar) {
    popoverProps = {
      placement: "left-start",
      offset: 36,
      shift: true
    };
  }
  return /* @__PURE__ */ jsx(Fragment, { children: settings.map((setting, index) => {
    const controlProps = {
      clearable: false,
      colorValue: setting.colorValue,
      colors,
      disableCustomColors,
      disableCustomGradients,
      enableAlpha,
      gradientValue: setting.gradientValue,
      gradients,
      label: setting.label,
      onColorChange: setting.onColorChange,
      onGradientChange: setting.onGradientChange,
      showTitle: false,
      __experimentalIsRenderedInSidebar,
      ...setting
    };
    const toggleSettings = {
      clearable: setting.clearable,
      label: setting.label,
      colorValue: setting.colorValue,
      gradientValue: setting.gradientValue,
      onColorChange: setting.onColorChange,
      onGradientChange: setting.onGradientChange
    };
    return setting && // If not in an `ItemGroup` wrap the dropdown in a
    // `ToolsPanelItem`
    /* @__PURE__ */ jsx(
      WithToolsPanelItem,
      {
        setting,
        ...props,
        children: /* @__PURE__ */ jsx(
          Dropdown,
          {
            popoverProps,
            className: "block-editor-tools-panel-color-gradient-settings__dropdown",
            renderToggle: renderToggle(toggleSettings),
            renderContent: () => /* @__PURE__ */ jsx(DropdownContentWrapper, { paddingSize: "none", children: /* @__PURE__ */ jsx("div", { className: "block-editor-panel-color-gradient-settings__dropdown-content", children: /* @__PURE__ */ jsx(
              ColorGradientControl,
              {
                ...controlProps
              }
            ) }) })
          }
        )
      },
      index
    );
  }) });
}
export {
  ColorGradientSettingsDropdown as default
};
//# sourceMappingURL=dropdown.js.map
