/*! © 2026 Yahoo, Inc. UDS Tailwind and Purger v0.0.0-development */
import { textVariants } from "../../fixtures/dist/index.js";
import { FONT_SIZE_PREFIX, LINE_HEIGHT_PREFIX, TEXT_RESPONSIVE_BREAKPOINT_CLASSNAMES, TEXT_RESPONSIVE_CLASSNAME } from "../../css-tokens/dist/index.js";
import { entries } from "../../utils/entries.js";
import { RESPONSIVE_TAILWIND_CONFIG_MAP, getBaseTextVars, getResponsiveTextStyles, textVariantsSafe } from "../components/getResponsiveTextStyles.js";
import { udsToTailwindMap } from "../defaultTailwindThemeAsUdsConfig.js";
import { fromEntries } from "../../utils/fromEntries.js";
import { shouldNotMergeTokensIntoTailwindTheme } from "../utils/composeTailwindPlugins.js";
import { addFontFaceDeclarations } from "../base/addFontFaceDeclarations.js";
import { addFontVars } from "../base/addFontVars.js";
import { getGroupedTextStyles } from "../components/getGroupedTextStyles.js";
import { getFontFamilyTheme } from "../theme/getFontFamilyTheme.js";
import plugin from "tailwindcss/plugin.js";

//#region src/tailwind/plugins/typography.ts
const UDS_FONT_FAMILY_THEME_KEY = "udsFontFamily";
const UDS_FONT_SIZE_THEME_KEY = "udsFontSize";
const UDS_FONT_WEIGHT_THEME_KEY = "udsFontWeight";
const UDS_FONT_SLANT_THEME_KEY = "udsFontSlant";
const UDS_LINE_HEIGHT_THEME_KEY = "udsLineHeight";
const UDS_LETTER_SPACING_THEME_KEY = "udsLetterSpacing";
const UDS_TEXT_TRANSFORM_THEME_KEY = "udsTextTransform";
const UDS_FONT_WIDTH_THEME_KEY = "udsFontWidth";
/** @deprecated Descriptive font weights vary based on font, so this is not correct for each font we support
* This is used as a baseline for using native font weights in the system IE. <Text fontWeight="bold" />
*/
const fontWeightMap = {
	thin: "100",
	extralight: "200",
	light: "300",
	regular: "400",
	medium: "500",
	semibold: "600",
	bold: "700",
	extrabold: "800",
	black: "900"
};
const typographyPlugin = plugin.withOptions(({ config, disableOverrideTailwindTheme, disableFontFaceDeclarations = false }) => (api) => {
	const shouldMergeTypographyTokensIntoTailwindTheme = !shouldNotMergeTokensIntoTailwindTheme(disableOverrideTailwindTheme, "typography");
	const { addBase, addUtilities, addComponents, matchUtilities, theme } = api;
	const fontIds = Object.values(config.font);
	if (disableFontFaceDeclarations === false) addFontFaceDeclarations(fontIds, api);
	addFontVars(config.font, api);
	addBase(getBaseTextVars(config));
	addBase(getGroupedTextStyles());
	if ((config.globalDefaults.enableResponsiveType ?? false) && config.typography && config.breakpoints) addComponents(getResponsiveTextStyles(config));
	const namedFontWeightsFromPlugin = theme("udsFontWeight") || {};
	const fontWeightsWithDescriptives = {
		...fontWeightMap,
		...namedFontWeightsFromPlugin
	};
	Object.entries(fontWeightsWithDescriptives).forEach(([fontWeight, value]) => {
		const styles = {};
		if (textVariants.map(textVariantsSafe).includes(fontWeight)) {
			const textVariant = fontWeight;
			const slantValue = theme(`udsFontSlant.${textVariant}`);
			const widthValue = theme(`udsFontWidth.${textVariant}`);
			styles["font-weight"] = value;
			styles["font-variation-settings"] = `"wght" ${value}, "slnt" ${slantValue}, "ital" ${slantValue}, "wdth" ${widthValue}`;
			const bolderStyles = { "font-variation-settings": "normal" };
			styles["b"] = bolderStyles;
			styles["strong"] = bolderStyles;
		} else styles["font-weight"] = value;
		if (shouldMergeTypographyTokensIntoTailwindTheme) addUtilities({ [`.font-weight-${fontWeight}`]: styles });
		addUtilities({ [`.uds-font-weight-${fontWeight}`]: styles });
	});
	if (shouldMergeTypographyTokensIntoTailwindTheme) matchUtilities({ "font-size": (value) => ({ "font-size": value }) }, { values: theme(UDS_FONT_SIZE_THEME_KEY) });
	matchUtilities({ "uds-font-size": (value) => ({ "font-size": value }) }, { values: theme(UDS_FONT_SIZE_THEME_KEY) });
	matchUtilities({ "uds-font-family": (value) => ({ "font-family": value }) }, { values: theme(UDS_FONT_FAMILY_THEME_KEY) });
	matchUtilities({ "uds-line-height": (value) => ({ "line-height": value }) }, { values: theme(UDS_LINE_HEIGHT_THEME_KEY) });
	matchUtilities({ "uds-letter-spacing": (value) => ({ "letter-spacing": value }) }, { values: theme(UDS_LETTER_SPACING_THEME_KEY) });
	if (shouldMergeTypographyTokensIntoTailwindTheme) matchUtilities({ case: (value) => ({ "text-transform": value }) }, { values: theme(UDS_TEXT_TRANSFORM_THEME_KEY) });
	matchUtilities({ "uds-text-transform": (value) => {
		return { "text-transform": value };
	} }, { values: theme(UDS_TEXT_TRANSFORM_THEME_KEY) });
}, ({ disableOverrideTailwindTheme }) => {
	const shouldNotMergeTypographyTokensIntoTailwindTheme = shouldNotMergeTokensIntoTailwindTheme(disableOverrideTailwindTheme, "typography");
	/**
	* Reassign Tailwind's font size aliases to UDS's font size aliases
	* This makes it so UDS configs can power both Tailwind and UDS components
	*/
	const tailwindFontSize = fromEntries(entries(udsToTailwindMap.fontSize).map(([udsAlias, twAlias]) => {
		return [twAlias, [`var(--${FONT_SIZE_PREFIX}-${textVariantsSafe(udsAlias)})`, { lineHeight: `var(--${LINE_HEIGHT_PREFIX}-${textVariantsSafe(udsAlias)})` }]];
	}));
	const textClasses = textVariants.map((variant) => `uds-text-${textVariantsSafe(variant)}`);
	const responsiveTextClasses = [TEXT_RESPONSIVE_CLASSNAME, ...Object.keys(TEXT_RESPONSIVE_BREAKPOINT_CLASSNAMES).map((variant) => TEXT_RESPONSIVE_BREAKPOINT_CLASSNAMES[variant])];
	const fontFamily = getFontFamilyTheme();
	const { lineHeight, letterSpacing, fontSize, textTransform, fontWeight, fontSlant, fontWidth } = RESPONSIVE_TAILWIND_CONFIG_MAP;
	return {
		safelist: [...textClasses, ...responsiveTextClasses],
		theme: { extend: {
			...shouldNotMergeTypographyTokensIntoTailwindTheme ? {} : {
				fontFamily,
				fontSize: tailwindFontSize,
				lineHeight,
				letterSpacing
			},
			[UDS_FONT_FAMILY_THEME_KEY]: fontFamily,
			[UDS_FONT_SIZE_THEME_KEY]: fontSize,
			[UDS_FONT_WEIGHT_THEME_KEY]: fontWeight,
			[UDS_FONT_SLANT_THEME_KEY]: fontSlant,
			[UDS_LINE_HEIGHT_THEME_KEY]: lineHeight,
			[UDS_LETTER_SPACING_THEME_KEY]: letterSpacing,
			[UDS_TEXT_TRANSFORM_THEME_KEY]: textTransform,
			[UDS_FONT_WIDTH_THEME_KEY]: fontWidth
		} }
	};
});

//#endregion
export { typographyPlugin };
//# sourceMappingURL=typography.js.map