import { ConfigUtils } from './config-utils' ; const SPLIT_CLASSES_REGEX = /\s+/ ; const IMPORTANT_MODIFIER = '!' ; const MODIFIER_SEPARATOR_REGEX = /:(?![^[]*\])/ ; const MODIFIER_SEPARATOR = ':' ; export function mergeClassList ( classList , configUtils ) { const { getClassGroupId , getConflictingClassGroupIds } = configUtils ; example example example const classGroupsInConflict = new Set < string > ( ) ; return ( classList . trim ( ) . split ( SPLIT_CLASSES_REGEX ) . map ( ( originalClassName ) => { const modifiers = originalClassName . split ( MODIFIER_SEPARATOR_REGEX ) ; const classNameWithImportantModifier = modifiers . pop ( ) ! ; const hasImportantModifier = classNameWithImportantModifier . startsWith ( IMPORTANT_MODIFIER ) ; const className = hasImportantModifier ? classNameWithImportantModifier . substring ( 1 ) : classNameWithImportantModifier ; const classGroupId = getClassGroupId ( className ) ; if ( ! classGroupId ) { return { isTailwindClass : false as const , originalClassName , } ; } const variantModifier = modifiers . length === 0 ? '' : modifiers . sort ( ) . concat ( '' ) . join ( MODIFIER_SEPARATOR ) ; const fullModifier = hasImportantModifier ? IMPORTANT_MODIFIER + variantModifier : variantModifier ; return { isTailwindClass : true as const , modifier : fullModifier , classGroupId , originalClassName , } ; } ) . reverse ( ) . filter ( ( parsed ) => { if ( ! parsed . isTailwindClass ) { return true ; } const { modifier , classGroupId } = parsed ; const classId = `${ modifier }:${ classGroupId }` ; if ( classGroupsInConflict . has ( classId ) ) { return false ; } classGroupsInConflict . add ( classId ) ; getConflictingClassGroupIds ( classGroupId ) . forEach ( ( group ) => classGroupsInConflict . add ( `${ modifier }:${ group }` ) ) ; return true ; } ) . reverse ( ) . map ( ( parsed ) => parsed . originalClassName ) . join ( ' ' ) ) ; }