"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.buildFontConfig = void 0;
var _react = _interopRequireDefault(require("react"));
var _reactNative = require("react-native");
var _responsiveSize = _interopRequireDefault(require("../../../utils/responsive-size"));
var _styleFunctions = require("../../../theme/src/style-functions");
var _useTheme = require("../../../hooks/useTheme");
var _pearl = require("../../../pearl");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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); }
/**
 * 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.
 */
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 = (0, _responsiveSize.default)(fontSize);
    lineHeight = (0, _responsiveSize.default)(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.
 */
exports.buildFontConfig = buildFontConfig;
const CustomText = /*#__PURE__*/_react.default.forwardRef(({
  scaleFontSize = true,
  ...props
}, ref) => {
  const {
    theme
  } = (0, _useTheme.useTheme)();

  // Apply the font configuration to the provided text style.
  props.style = _react.default.useMemo(() => {
    return {
      includeFontPadding: false,
      ...props.style,
      ...buildFontConfig(props.style, scaleFontSize, theme)
    };
  }, [props.style, scaleFontSize, theme]);
  return /*#__PURE__*/_react.default.createElement(_reactNative.Text, _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 = (0, _pearl.pearl)(CustomText, {
  componentName: "Text",
  type: "atom",
  animatable: true
}, _styleFunctions.textStyleFunctions);
Text.displayName = "Text";
var _default = exports.default = Text;
//# sourceMappingURL=text.js.map