import { colord, extend } from "colord";
import a11yPlugin from "colord/plugins/a11y";
import { store as blocksStore } from "@wordpress/blocks";
import { privateApis as blockEditorPrivateApis } from "@wordpress/block-editor";
import { unlock } from "../../lock-unlock";
import { useSelect } from "@wordpress/data";
const { useGlobalSetting, useGlobalStyle } = unlock(blockEditorPrivateApis);
extend([a11yPlugin]);
function useColorRandomizer(name) {
  const [themeColors, setThemeColors] = useGlobalSetting(
    "color.palette.theme",
    name
  );
  function randomizeColors() {
    const randomRotationValue = Math.floor(Math.random() * 225);
    const newColors = themeColors.map((colorObject) => {
      const { color } = colorObject;
      const newColor = colord(color).rotate(randomRotationValue).toHex();
      return {
        ...colorObject,
        color: newColor
      };
    });
    setThemeColors(newColors);
  }
  return window.__experimentalEnableColorRandomizer ? [randomizeColors] : [];
}
function useStylesPreviewColors() {
  const [textColor = "black"] = useGlobalStyle("color.text");
  const [backgroundColor = "white"] = useGlobalStyle("color.background");
  const [headingColor = textColor] = useGlobalStyle(
    "elements.h1.color.text"
  );
  const [linkColor = headingColor] = useGlobalStyle(
    "elements.link.color.text"
  );
  const [buttonBackgroundColor = linkColor] = useGlobalStyle(
    "elements.button.color.background"
  );
  const [coreColors] = useGlobalSetting("color.palette.core");
  const [themeColors] = useGlobalSetting("color.palette.theme");
  const [customColors] = useGlobalSetting("color.palette.custom");
  const paletteColors = (themeColors ?? []).concat(customColors ?? []).concat(coreColors ?? []);
  const textColorObject = paletteColors.filter(
    ({ color }) => color === textColor
  );
  const buttonBackgroundColorObject = paletteColors.filter(
    ({ color }) => color === buttonBackgroundColor
  );
  const highlightedColors = textColorObject.concat(buttonBackgroundColorObject).concat(paletteColors).filter(
    // we exclude these background color because it is already visible in the preview.
    ({ color }) => color !== backgroundColor
  ).slice(0, 2);
  return {
    paletteColors,
    highlightedColors
  };
}
function useSupportedStyles(name, element) {
  const { supportedPanels } = useSelect(
    (select) => {
      return {
        supportedPanels: unlock(
          select(blocksStore)
        ).getSupportedStyles(name, element)
      };
    },
    [name, element]
  );
  return supportedPanels;
}
export {
  useColorRandomizer,
  useStylesPreviewColors,
  useSupportedStyles
};
//# sourceMappingURL=hooks.js.map
