import { parseCandidate, type Candidate, type Variant } from '../../../../tailwindcss/src/candidate'
import type { Config } from '../../../../tailwindcss/src/compat/plugin-api'
import type { DesignSystem } from '../../../../tailwindcss/src/design-system'
import { isPositiveInteger } from '../../../../tailwindcss/src/utils/infer-data-type'
import { segment } from '../../../../tailwindcss/src/utils/segment'
import { printCandidate } from '../candidates'
export function arbitraryValueToBareValue(
designSystem: DesignSystem,
_userConfig: Config,
rawCandidate: string,
): string {
for (let candidate of parseCandidate(rawCandidate, designSystem)) {
let clone = structuredClone(candidate)
let changed = false
if (
clone.kind === 'functional' &&
clone.value?.kind === 'arbitrary' &&
clone.value.value === 'subgrid' &&
(clone.root === 'grid-cols' || clone.root == 'grid-rows')
) {
changed = true
clone.value = {
kind: 'named',
value: 'subgrid',
fraction: null,
}
}
if (
clone.kind === 'functional' &&
clone.value?.kind === 'arbitrary' &&
clone.value.dataType === null &&
(clone.root === 'from' ||
clone.root === 'via' ||
clone.root === 'to' ||
clone.root === 'font-stretch')
) {
if (clone.value.value.endsWith('%') && isPositiveInteger(clone.value.value.slice(0, -1))) {
let percentage = parseInt(clone.value.value)
if (
clone.root === 'from' ||
clone.root === 'via' ||
clone.root === 'to' ||
(clone.root === 'font-stretch' && percentage >= 50 && percentage <= 200)
) {
changed = true
clone.value = {
kind: 'named',
value: clone.value.value,
fraction: null,
}
}
}
}
else if (
clone.kind === 'functional' &&
clone.value?.kind === 'arbitrary' &&
clone.value.dataType === null
) {
let parts = segment(clone.value.value, '/')
if (parts.every((part) => isPositiveInteger(part))) {
changed = true
let currentValue = clone.value
let currentModifier = clone.modifier
if (parts.length === 1) {
clone.value = {
kind: 'named',
value: clone.value.value,
fraction: null,
}
}
else {
clone.value = {
kind: 'named',
value: parts[0],
fraction: clone.value.value,
}
clone.modifier = {
kind: 'named',
value: parts[1],
}
}
if (designSystem.compileAstNodes(clone).length === 0) {
clone.value = currentValue
clone.modifier = currentModifier
changed = false
}
}
}
for (let variant of variants(clone)) {
if (
variant.kind === 'functional' &&
variant.root === 'data' &&
variant.value?.kind === 'arbitrary' &&
!variant.value.value.includes('=')
) {
changed = true
variant.value = {
kind: 'named',
value: variant.value.value,
}
}
else if (
variant.kind === 'functional' &&
variant.root === 'aria' &&
variant.value?.kind === 'arbitrary' &&
(variant.value.value.endsWith('=true') ||
variant.value.value.endsWith('="true"') ||
variant.value.value.endsWith("='true'"))
) {
let [key, _value] = segment(variant.value.value, '=')
if (
key[key.length - 1] === '~' ||
key[key.length - 1] === '|' ||
key[key.length - 1] === '^' ||
key[key.length - 1] === '$' ||
key[key.length - 1] === '*'
) {
continue
}
changed = true
variant.value = {
kind: 'named',
value: variant.value.value.slice(0, variant.value.value.indexOf('=')),
}
}
else if (
variant.kind === 'functional' &&
variant.root === 'supports' &&
variant.value?.kind === 'arbitrary' &&
/^[a-z-][a-z0-9-]*$/i.test(variant.value.value)
) {
changed = true
variant.value = {
kind: 'named',
value: variant.value.value,
}
}
}
return changed ? printCandidate(designSystem, clone) : rawCandidate
}
return rawCandidate
}
function* variants(candidate: Candidate) {
function* inner(variant: Variant): Iterable<Variant> {
yield variant
if (variant.kind === 'compound') {
yield* inner(variant.variant)
}
}
for (let variant of candidate.variants) {
yield* inner(variant)
}
}