#!/bin/bash

# 批量更新组件中的图片路径以使用 getAssetUrl 函数

echo "🔧 修复静态资源路径..."

# 需要更新的组件列表
components=(
  "components/HowItWorksSection.tsx"
  "components/WhyAssetXSection.tsx"
  "components/Navbar.tsx"
  "components/Footer.tsx"
  "components/ResourceMenu.tsx"
  "components/ProductMenu.tsx"
  "components/SecuritySection.tsx"
)

for component in "${components[@]}"; do
  if [ -f "$component" ]; then
    echo "  处理: $component"

    # 添加 import 语句（如果不存在）
    if ! grep -q "getAssetUrl" "$component"; then
      # 在 'use client' 之后或第一个 import 之前添加
      if grep -q "'use client'" "$component"; then
        sed -i "/'use client';/a import { getAssetUrl } from '@/lib/assetUrl';" "$component"
      else
        sed -i "1i import { getAssetUrl } from '@/lib/assetUrl';" "$component"
      fi
    fi

    # 替换 src="/xxx.svg" 为 src={getAssetUrl("/xxx.svg")}
    sed -i 's/src="\/\([^"]*\.svg\)"/src={getAssetUrl("\/\1")}/g' "$component"

    # 替换 icon: '/xxx.svg' 为 icon: getAssetUrl('/xxx.svg')
    sed -i "s/icon: '\/\([^']*\.svg\)'/icon: getAssetUrl('\/\1')/g" "$component"

    # 替换 icon: "/xxx.svg" 为 icon: getAssetUrl("/xxx.svg")
    sed -i 's/icon: "\/\([^"]*\.svg\)"/icon: getAssetUrl("\/\1")/g' "$component"
  fi
done

echo "✅ 完成！"
