// packages/block-editor/src/hooks/font-size.js
import { addFilter } from "@wordpress/hooks";
import { hasBlockSupport } from "@wordpress/blocks";
import TokenList from "@wordpress/token-list";
import { getTypographyFontSizeValue } from "@wordpress/global-styles-engine";
import {
  getFontSize,
  getFontSizeClass,
  getFontSizeObjectByValue,
  FontSizePicker
} from "../components/font-sizes";
import { TYPOGRAPHY_SUPPORT_KEY } from "./typography";
import {
  cleanEmptyObject,
  transformStyles,
  shouldSkipSerialization
} from "./utils";
import { useSettings } from "../components/use-settings";
import { jsx } from "react/jsx-runtime";
var FONT_SIZE_SUPPORT_KEY = "typography.fontSize";
function addAttributes(settings) {
  if (!hasBlockSupport(settings, FONT_SIZE_SUPPORT_KEY)) {
    return settings;
  }
  if (!settings.attributes.fontSize) {
    Object.assign(settings.attributes, {
      fontSize: {
        type: "string"
      }
    });
  }
  return settings;
}
function addSaveProps(props, blockNameOrType, attributes) {
  if (!hasBlockSupport(blockNameOrType, FONT_SIZE_SUPPORT_KEY)) {
    return props;
  }
  if (shouldSkipSerialization(
    blockNameOrType,
    TYPOGRAPHY_SUPPORT_KEY,
    "fontSize"
  )) {
    return props;
  }
  const classes = new TokenList(props.className);
  classes.add(getFontSizeClass(attributes.fontSize));
  const newClassName = classes.value;
  props.className = newClassName ? newClassName : void 0;
  return props;
}
function FontSizeEdit(props) {
  const {
    attributes: { fontSize, style, fitText },
    setAttributes
  } = props;
  const [fontSizes] = useSettings("typography.fontSizes");
  if (fitText) {
    return null;
  }
  const onChange = (value, selectedItem) => {
    const fontSizeSlug = selectedItem?.slug || getFontSizeObjectByValue(fontSizes, value).slug;
    setAttributes({
      style: cleanEmptyObject({
        ...style,
        typography: {
          ...style?.typography,
          fontSize: fontSizeSlug ? void 0 : value
        }
      }),
      fontSize: fontSizeSlug
    });
  };
  const fontSizeObject = getFontSize(
    fontSizes,
    fontSize,
    style?.typography?.fontSize
  );
  const fontSizeValue = fontSizeObject?.size || style?.typography?.fontSize || fontSize;
  return /* @__PURE__ */ jsx(
    FontSizePicker,
    {
      onChange,
      value: fontSize || fontSizeValue,
      valueMode: fontSize ? "slug" : "literal",
      withReset: false,
      withSlider: true,
      size: "__unstable-large"
    }
  );
}
function useIsFontSizeDisabled({ name: blockName } = {}) {
  const [fontSizes] = useSettings("typography.fontSizes");
  const hasFontSizes = !!fontSizes?.length;
  return !hasBlockSupport(blockName, FONT_SIZE_SUPPORT_KEY) || !hasFontSizes;
}
function useBlockProps({ name, fontSize, style }) {
  const [fontSizes, fluidTypographySettings, layoutSettings] = useSettings(
    "typography.fontSizes",
    "typography.fluid",
    "layout"
  );
  if (!hasBlockSupport(name, FONT_SIZE_SUPPORT_KEY) || shouldSkipSerialization(name, TYPOGRAPHY_SUPPORT_KEY, "fontSize") || !fontSize && !style?.typography?.fontSize) {
    return;
  }
  let props;
  if (style?.typography?.fontSize) {
    props = {
      style: {
        fontSize: getTypographyFontSizeValue(
          { size: style.typography.fontSize },
          {
            typography: {
              fluid: fluidTypographySettings
            },
            layout: layoutSettings
          }
        )
      }
    };
  }
  if (fontSize) {
    props = {
      style: {
        fontSize: getFontSize(
          fontSizes,
          fontSize,
          style?.typography?.fontSize
        ).size
      }
    };
  }
  if (!props) {
    return;
  }
  return addSaveProps(props, name, { fontSize });
}
var font_size_default = {
  useBlockProps,
  addSaveProps,
  attributeKeys: ["fontSize", "style", "fitText"],
  hasSupport(name) {
    return hasBlockSupport(name, FONT_SIZE_SUPPORT_KEY);
  }
};
var MIGRATION_PATHS = {
  fontSize: [["fontSize"], ["style", "typography", "fontSize"]]
};
function addTransforms(result, source, index, results) {
  const destinationBlockType = result.name;
  const activeSupports = {
    fontSize: hasBlockSupport(
      destinationBlockType,
      FONT_SIZE_SUPPORT_KEY
    )
  };
  return transformStyles(
    activeSupports,
    MIGRATION_PATHS,
    result,
    source,
    index,
    results
  );
}
addFilter(
  "blocks.registerBlockType",
  "core/font/addAttribute",
  addAttributes
);
addFilter(
  "blocks.switchToBlockType.transformedBlock",
  "core/font-size/addTransforms",
  addTransforms
);
export {
  FONT_SIZE_SUPPORT_KEY,
  FontSizeEdit,
  font_size_default as default,
  useIsFontSizeDisabled
};
//# sourceMappingURL=font-size.js.map
