// packages/block-editor/src/hooks/align.js
import clsx from "clsx";
import { addFilter } from "@wordpress/hooks";
import {
  getBlockSupport,
  getBlockType,
  hasBlockSupport
} from "@wordpress/blocks";
import { BlockControls, BlockAlignmentControl } from "../components";
import useAvailableAlignments from "../components/block-alignment-control/use-available-alignments";
import { useBlockEditingMode } from "../components/block-editing-mode";
import { jsx } from "react/jsx-runtime";
var ALL_ALIGNMENTS = ["left", "center", "right", "wide", "full"];
var WIDE_ALIGNMENTS = ["wide", "full"];
function getValidAlignments(blockAlign, hasWideBlockSupport = true, hasWideEnabled = true) {
  let validAlignments;
  if (Array.isArray(blockAlign)) {
    validAlignments = ALL_ALIGNMENTS.filter(
      (value) => blockAlign.includes(value)
    );
  } else if (blockAlign === true) {
    validAlignments = [...ALL_ALIGNMENTS];
  } else {
    validAlignments = [];
  }
  if (!hasWideEnabled || blockAlign === true && !hasWideBlockSupport) {
    return validAlignments.filter(
      (alignment) => !WIDE_ALIGNMENTS.includes(alignment)
    );
  }
  return validAlignments;
}
function addAttribute(settings) {
  if ("type" in (settings.attributes?.align ?? {})) {
    return settings;
  }
  if (hasBlockSupport(settings, "align")) {
    settings.attributes = {
      ...settings.attributes,
      align: {
        type: "string",
        // Allow for '' since it is used by the `updateAlignment` function
        // in toolbar controls for special cases with defined default values.
        enum: [...ALL_ALIGNMENTS, ""]
      }
    };
  }
  return settings;
}
function BlockEditAlignmentToolbarControlsPure({
  name: blockName,
  align,
  setAttributes
}) {
  const blockAllowedAlignments = getValidAlignments(
    getBlockSupport(blockName, "align"),
    hasBlockSupport(blockName, "alignWide", true)
  );
  const validAlignments = useAvailableAlignments(
    blockAllowedAlignments
  ).map(({ name }) => name);
  const blockEditingMode = useBlockEditingMode();
  if (!validAlignments.length || blockEditingMode !== "default") {
    return null;
  }
  const updateAlignment = (nextAlign) => {
    if (!nextAlign) {
      const blockType = getBlockType(blockName);
      const blockDefaultAlign = blockType?.attributes?.align?.default;
      if (blockDefaultAlign) {
        nextAlign = "";
      }
    }
    setAttributes({ align: nextAlign });
  };
  return /* @__PURE__ */ jsx(BlockControls, { group: "block", __experimentalShareWithChildBlocks: true, children: /* @__PURE__ */ jsx(
    BlockAlignmentControl,
    {
      value: align,
      onChange: updateAlignment,
      controls: validAlignments
    }
  ) });
}
var align_default = {
  shareWithChildBlocks: true,
  edit: BlockEditAlignmentToolbarControlsPure,
  useBlockProps,
  addSaveProps: addAssignedAlign,
  attributeKeys: ["align"],
  hasSupport(name) {
    return hasBlockSupport(name, "align", false);
  }
};
function useBlockProps({ name, align }) {
  const blockAllowedAlignments = getValidAlignments(
    getBlockSupport(name, "align"),
    hasBlockSupport(name, "alignWide", true)
  );
  const validAlignments = useAvailableAlignments(blockAllowedAlignments);
  if (validAlignments.some((alignment) => alignment.name === align)) {
    return { "data-align": align };
  }
  return {};
}
function addAssignedAlign(props, blockType, attributes) {
  const { align } = attributes;
  const blockAlign = getBlockSupport(blockType, "align");
  const hasWideBlockSupport = hasBlockSupport(blockType, "alignWide", true);
  const isAlignValid = getValidAlignments(
    blockAlign,
    hasWideBlockSupport
  ).includes(align);
  if (isAlignValid) {
    props.className = clsx(`align${align}`, props.className);
  }
  return props;
}
addFilter(
  "blocks.registerBlockType",
  "core/editor/align/addAttribute",
  addAttribute
);
export {
  addAssignedAlign,
  addAttribute,
  align_default as default,
  getValidAlignments
};
//# sourceMappingURL=align.js.map
