import { presetUni } from '@uni-helper/unocss-preset-uni'
import { defineConfig, transformerDirectives } from 'unocss'

const remRE = /(-?[\\.\d]+)rem/g

function transformUniPreset() {
  return {
    name: 'preset-transform-uni-rem-to-rpx',
    postprocess: (util) => {
      util.entries.forEach((i) => {
        const [, value] = i
        if (typeof value === 'string' && remRE.test(value)) {
          i[1] = value.replace(remRE, (_, p1) => `${+p1 * 4}rpx`)
        }
        if (
          typeof value === 'string' &&
          ['rgba'].some((x) => value.startsWith(x))
        ) {
          i[1] = value.replace(/,var\(.+\)\)$/, ')').replace('rgba', 'rgb')
        }
      })
    }
  }
}

export default defineConfig({
  presets: [transformUniPreset(), presetUni()],
  transformers: [transformerDirectives()],
  shortcuts: [
    ['wh-full', 'w-full h-full'],
    ['f-c-c', 'flex justify-center items-center'],
    ['f-s-c', 'flex justify-start items-center'],
    ['f-s-s', 'flex justify-start items-start'],
    ['flex-col', 'flex flex-col'],
    ['pointer-select', 'select-none cursor-pointer'],
    ['text-ellipsis', 'truncate']
  ],
  rules: [
    ['pre-wrap', { 'white-space': 'pre-wrap' }],
    ['no-scrollbar', { 'overflow-x': 'scroll', 'scrollbar-width': 'none' }],
    [/^bc-(.+)$/, ([, color]) => ({ 'border-color': `#${color}` })],
    [
      'card-shadow',
      {
        'box-shadow': '0 4px 10px 0 rgba(0, 0, 0, 0.14)'
      }
    ]
  ]
})
