export const deepClone = <T>(obj: T): T => {
  if (obj === null || typeof obj !== 'object') {
    return obj
  }
  if (Array.isArray(obj)) {
    return obj.map((item) => deepClone(item)) as T
  }
  const clonedObj = {} as T
  for (const key in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, key)) {
      clonedObj[key] = deepClone(obj[key])
    }
  }
  return clonedObj
}

// 深度比较两个对象的值是否相等
export const deepEqual = (a: any, b: any) => {
  if (!a || !b) return false
  if (a === b) return true
  if (a !== a && b !== b) return true // 处理 NaN 的特殊情况
  if (typeof a !== 'object' || a === null || typeof b !== 'object' || b === null) {
    return false
  }

  const isArrayA = Array.isArray(a)
  const isArrayB = Array.isArray(b)
  if (isArrayA !== isArrayB) return false
  if (isArrayA) {
    console.log(a.length, b.length, a, b)

    if (a.length !== b.length) return false
    for (let i = 0; i < a.length; i++) {
      if (!deepEqual(a[i], b[i])) return false
    }
    return true
  }
  const keysA = Object.keys(a)
  const keysB = Object.keys(b)
  if (keysA.length !== keysB.length) return false
  for (const key of keysA) {
    if (!b.hasOwnProperty(key)) return false // 确保存在该属性
    if (!deepEqual(a[key], b[key])) return false // 递归比较值
  }
  return true
}

export const formatDateTime = (date: string | Date | number, joinStr = ' ') => {
  if (!date) return
  const dateObj = new Date(date)
  const year = dateObj.getFullYear()
  const month = String(dateObj.getMonth() + 1).padStart(2, '0')
  const day = String(dateObj.getDate()).padStart(2, '0')
  const hours = String(dateObj.getHours()).padStart(2, '0')
  const minutes = String(dateObj.getMinutes()).padStart(2, '0')
  const seconds = String(dateObj.getSeconds()).padStart(2, '0')

  return `${year}-${month}-${day}${joinStr}${hours}:${minutes}:${seconds}`
}

export const requireImgUrl = (src: string) => {
  return new URL(`../assets/images/${src}`, import.meta.url).href
}