import Taro from '@tarojs/taro'

export function wxParse(htmlText: string) {
  return htmlText
    .replace(/\<img/gi, '<img style="max-width:100%;height:auto;margin:0;padding:0;display:block;"')
    .replace(
      /style\s*?=\s*?(['"])(?!.*?text-align)[\s\S]*?\1/ig,
      'style="max-width:100%;height:auto;margin:0;padding:0;display:block;"'
    )
}

export function copyText(text: string) {
  Taro.setClipboardData({
    data: text,
    success() {
      Taro.showToast({
        title: '复制成功',
        icon: 'success',
        duration: 2000,
      })
    },
    fail() {
      Taro.showToast({
        title: '复制失败',
        icon: 'none',
        duration: 2000,
      })
    },
  })
}

export default function navTo(url: string) {
  const normalizedUrl = url.startsWith('/') ? url : `/${url}`
  Taro.navigateTo({ url: normalizedUrl })
}
