File size: 1,179 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { type Dict, isObject } from "../utils"
import { sortAtParams } from "./sort-at-params"

type Query = string
type QueryValue = Dict

function sortQueries(queries: [Query, QueryValue][]): [Query, QueryValue][] {
  return queries.sort(([a], [b]) => sortAtParams(a, b))
}

export function sortAtRules(obj: Dict): Dict {
  const mediaQueries: [Query, QueryValue][] = []
  const containerQueries: [Query, QueryValue][] = []
  const rest: Dict = {}

  // Separate media queries, container queries, and other properties
  for (const [key, value] of Object.entries(obj)) {
    if (key.startsWith("@media")) {
      mediaQueries.push([key, value])
    } else if (key.startsWith("@container")) {
      containerQueries.push([key, value])
    } else if (isObject(value)) {
      rest[key] = sortAtRules(value)
    } else {
      rest[key] = value
    }
  }

  // Sort queries
  const sortedMediaQueries = sortQueries(mediaQueries)
  const sortedContainerQueries = sortQueries(containerQueries)

  // Combine sorted queries with the rest of the properties
  return {
    ...rest,
    ...Object.fromEntries(sortedMediaQueries),
    ...Object.fromEntries(sortedContainerQueries),
  }
}