// packages/block-editor/src/components/global-styles/shadow-panel-components.js
import { __ } from "@wordpress/i18n";
import {
  __experimentalVStack as VStack,
  __experimentalHeading as Heading,
  __experimentalHStack as HStack,
  __experimentalDropdownContentWrapper as DropdownContentWrapper,
  Button,
  FlexItem,
  Dropdown,
  Composite,
  Tooltip
} from "@wordpress/components";
import { useMemo, useRef } from "@wordpress/element";
import { shadow as shadowIcon, Icon, check, reset } from "@wordpress/icons";
import clsx from "clsx";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var EMPTY_ARRAY = [];
function ShadowPopoverContainer({ shadow, onShadowChange, settings }) {
  const shadows = useShadowPresets(settings);
  return /* @__PURE__ */ jsx("div", { className: "block-editor-global-styles__shadow-popover-container", children: /* @__PURE__ */ jsxs(VStack, { spacing: 4, children: [
    /* @__PURE__ */ jsx(Heading, { level: 5, children: __("Drop shadow") }),
    /* @__PURE__ */ jsx(
      ShadowPresets,
      {
        presets: shadows,
        activeShadow: shadow,
        onSelect: onShadowChange
      }
    ),
    /* @__PURE__ */ jsx("div", { className: "block-editor-global-styles__clear-shadow", children: /* @__PURE__ */ jsx(
      Button,
      {
        __next40pxDefaultSize: true,
        variant: "tertiary",
        onClick: () => onShadowChange(void 0),
        disabled: !shadow,
        accessibleWhenDisabled: true,
        children: __("Clear")
      }
    ) })
  ] }) });
}
function ShadowPresets({ presets, activeShadow, onSelect }) {
  return !presets ? null : /* @__PURE__ */ jsx(
    Composite,
    {
      role: "listbox",
      className: "block-editor-global-styles__shadow__list",
      "aria-label": __("Drop shadows"),
      children: presets.map(({ name, slug, shadow }) => /* @__PURE__ */ jsx(
        ShadowIndicator,
        {
          label: name,
          isActive: shadow === activeShadow,
          type: slug === "unset" ? "unset" : "preset",
          onSelect: () => onSelect(shadow === activeShadow ? void 0 : shadow),
          shadow
        },
        slug
      ))
    }
  );
}
function ShadowIndicator({ type, label, isActive, onSelect, shadow }) {
  return /* @__PURE__ */ jsx(Tooltip, { text: label, children: /* @__PURE__ */ jsx(
    Composite.Item,
    {
      role: "option",
      "aria-label": label,
      "aria-selected": isActive,
      className: clsx("block-editor-global-styles__shadow__item", {
        "is-active": isActive
      }),
      render: /* @__PURE__ */ jsx(
        "button",
        {
          className: clsx(
            "block-editor-global-styles__shadow-indicator",
            {
              unset: type === "unset"
            }
          ),
          onClick: onSelect,
          style: { boxShadow: shadow },
          "aria-label": label,
          children: isActive && /* @__PURE__ */ jsx(Icon, { icon: check })
        }
      )
    }
  ) });
}
function ShadowPopover({ shadow, onShadowChange, settings }) {
  const popoverProps = {
    placement: "left-start",
    offset: 36,
    shift: true
  };
  return /* @__PURE__ */ jsx(
    Dropdown,
    {
      popoverProps,
      className: "block-editor-global-styles__shadow-dropdown",
      renderToggle: renderShadowToggle(shadow, onShadowChange),
      renderContent: () => /* @__PURE__ */ jsx(DropdownContentWrapper, { paddingSize: "medium", children: /* @__PURE__ */ jsx(
        ShadowPopoverContainer,
        {
          shadow,
          onShadowChange,
          settings
        }
      ) })
    }
  );
}
function renderShadowToggle(shadow, onShadowChange) {
  return ({ onToggle, isOpen }) => {
    const shadowButtonRef = useRef(void 0);
    const toggleProps = {
      onClick: onToggle,
      className: clsx(
        "block-editor-global-styles__shadow-dropdown-toggle",
        { "is-open": isOpen }
      ),
      "aria-expanded": isOpen,
      ref: shadowButtonRef
    };
    const removeButtonProps = {
      onClick: () => {
        if (isOpen) {
          onToggle();
        }
        onShadowChange(void 0);
        shadowButtonRef.current?.focus();
      },
      className: clsx(
        "block-editor-global-styles__shadow-editor__remove-button",
        { "is-open": isOpen }
      ),
      label: __("Remove")
    };
    return /* @__PURE__ */ jsxs(Fragment, { children: [
      /* @__PURE__ */ jsx(Button, { __next40pxDefaultSize: true, ...toggleProps, children: /* @__PURE__ */ jsxs(HStack, { justify: "flex-start", children: [
        /* @__PURE__ */ jsx(
          Icon,
          {
            className: "block-editor-global-styles__toggle-icon",
            icon: shadowIcon,
            size: 24
          }
        ),
        /* @__PURE__ */ jsx(FlexItem, { children: __("Drop shadow") })
      ] }) }),
      !!shadow && /* @__PURE__ */ jsx(
        Button,
        {
          __next40pxDefaultSize: true,
          size: "small",
          icon: reset,
          ...removeButtonProps
        }
      )
    ] });
  };
}
function useShadowPresets(settings) {
  return useMemo(() => {
    if (!settings?.shadow) {
      return EMPTY_ARRAY;
    }
    const defaultPresetsEnabled = settings?.shadow?.defaultPresets;
    const {
      default: defaultShadows,
      theme: themeShadows,
      custom: customShadows
    } = settings?.shadow?.presets ?? {};
    const unsetShadow = {
      name: __("Unset"),
      slug: "unset",
      shadow: "none"
    };
    const shadowPresets = [
      ...defaultPresetsEnabled && defaultShadows || EMPTY_ARRAY,
      ...themeShadows || EMPTY_ARRAY,
      ...customShadows || EMPTY_ARRAY
    ];
    if (shadowPresets.length) {
      shadowPresets.unshift(unsetShadow);
    }
    return shadowPresets;
  }, [settings]);
}
export {
  ShadowIndicator,
  ShadowPopover,
  ShadowPopoverContainer,
  ShadowPresets,
  useShadowPresets
};
//# sourceMappingURL=shadow-panel-components.js.map
