import { ColorBucket } from "@wix/wix-ui-icons-common";
import React, { useContext, useEffect, useMemo, useState } from "react";
import { RicosContext } from "./../../../ricos-context/index.js";
import { useModalService } from "./../../../ricos-modals/modal-service-provider.js";
import { mapColorPaletteToCustomStyles } from "./../../../ricos-styles/themeStrategy/generators/palette.js";
import { ColorSwatch } from "./../../../_shared/plugin-toolbar/color-swatch.js";
import { ToolbarButton as ToolbarButtonComponent } from "./../../../_shared/toolbar-button/toolbar-button.js";
import { DATA_HOOKS } from "../../data-hooks.js";
import { getCellColorAttr } from "../../utils/variant-features.js";
const SmartBlockColorButton = ({ toolbarItem }) => {
  const [referenceElement, setReferenceElement] = useState(null);
  const [isActive, setIsActive] = useState(toolbarItem.attributes.active);
  const modalService = useModalService();
  const { t, theme } = useContext(RicosContext);
  useEffect(() => {
    const handler = () => setIsActive(Boolean(toolbarItem.attributes.active));
    const openedSub = modalService.onModalOpened(handler);
    const closedSub = modalService.onModalClosed(handler);
    return () => {
      openedSub.cancel();
      closedSub.cancel();
    };
  }, [modalService, toolbarItem.attributes.active]);
  const variant = toolbarItem.attributes.variant;
  const themeSmartBlock = useMemo(() => {
    if (!variant) {
      return void 0;
    }
    return theme?.customStyles?.smartBlock ?? (theme?.colorPalette ? mapColorPaletteToCustomStyles(theme.colorPalette).smartBlock : void 0);
  }, [theme?.customStyles?.smartBlock, theme?.colorPalette, variant]);
  const colors = toolbarItem.attributes.colors ?? [];
  const effectiveColors = useMemo(() => {
    if (colors.length > 0) {
      return colors;
    }
    if (!variant || !themeSmartBlock) {
      return [];
    }
    const colorAttr = getCellColorAttr(variant);
    const themeColor = themeSmartBlock[colorAttr];
    return themeColor ? [themeColor] : [];
  }, [colors, variant, themeSmartBlock]);
  const hasColors = effectiveColors.length > 0;
  return /* @__PURE__ */ React.createElement(
    ToolbarButtonComponent,
    {
      ref: setReferenceElement,
      prefix: hasColors ? () => /* @__PURE__ */ React.createElement(ColorSwatch, { colors: effectiveColors }) : ColorBucket,
      active: isActive,
      onClick: () => toolbarItem.commands.click({ referenceElement }),
      dataHook: DATA_HOOKS.COLOR_BUTTON,
      tooltip: t("SmartBlock_Toolbar_Color_Tooltip"),
      tabIndex: toolbarItem.presentation.tabIndex
    }
  );
};
export {
  SmartBlockColorButton
};
//# sourceMappingURL=color-button.js.map
