function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import React from "react";
import { Text as RNText } from "react-native";
import responsiveSize from "../../../utils/responsive-size";
import { textStyleFunctions } from "../../../theme/src/style-functions";
import { useTheme } from "../../../hooks/useTheme";
import { pearl } from "../../../pearl";
/**
 * Builds a font configuration object based on the provided text style and whether font scaling is allowed.
 *
 * @param textStyle - The text style to build the font configuration from.
 * @param allowFontScaling - Whether font scaling is allowed.
 * @returns A font configuration object.
 */
export const buildFontConfig = (textStyle, allowFontScaling, theme) => {
  let fontWeight = textStyle.fontWeight ?? "400";
  if (!!theme.fontWeights[fontWeight]) fontWeight = theme.fontWeights[fontWeight];
  const fontStyle = textStyle.fontStyle ?? "normal";
  let fontSize = textStyle.fontSize;
  let lineHeight = textStyle.lineHeight;
  if (allowFontScaling) {
    fontSize = responsiveSize(fontSize);
    lineHeight = responsiveSize(lineHeight);
  }
  const initialFontFamily = textStyle.fontFamily;
  if (!initialFontFamily || !theme.fontConfig[initialFontFamily]) {
    throw new Error(`Font family "${initialFontFamily}" does not exist in the theme.fontConfig`);
  }
  const finalFontFamily = theme.fontConfig[initialFontFamily][fontWeight][fontStyle];
  return {
    fontSize,
    fontWeight,
    fontFamily: finalFontFamily,
    fontStyle
  };
};

/**
 * CustomText is a forwardRef component that applies a font configuration to the provided text style.
 *
 * @param scaleFontSize - Whether to slightly scale the font size based on the screen dimensions.
 * @param props - The props to apply the font configuration to.
 * @param ref - The ref to forward.
 * @returns A <RNText /> component with the font configuration applied.
 */
const CustomText = /*#__PURE__*/React.forwardRef(({
  scaleFontSize = true,
  ...props
}, ref) => {
  const {
    theme
  } = useTheme();

  // Apply the font configuration to the provided text style.
  props.style = React.useMemo(() => {
    return {
      includeFontPadding: false,
      ...props.style,
      ...buildFontConfig(props.style, scaleFontSize, theme)
    };
  }, [props.style, scaleFontSize, theme]);
  return /*#__PURE__*/React.createElement(RNText, _extends({
    ref: ref,
    accessible: true,
    accessibilityRole: "text",
    allowFontScaling: scaleFontSize
  }, props), props.children);
});

/**
 * Text is the component which controls the typography across your app. By default, it renders a <Text /> component
 */
const Text = pearl(CustomText, {
  componentName: "Text",
  type: "atom",
  animatable: true
}, textStyleFunctions);
Text.displayName = "Text";
export default Text;
//# sourceMappingURL=text.js.map