/*
 * @Author: LiZhiWei
 * @Date: 2025-12-29 11:59:43
 * @LastEditors: LiZhiWei
 * @LastEditTime: 2025-12-30 08:41:46
 * @Description: 
 */
import type { PptxElement } from "./pptxTypes"
import { getMediaSrc } from "./pptxMedia"

export function isShapeElement(el: PptxElement) {
  const t = String(el.type || "").toLowerCase()
  return t === "shape" || t === "rect" || t === "line"
}

export function hasImageFill(el: PptxElement) {
  if (typeof el.fill === "object" && (el.fill as any)?.type === "image") return true
  if (typeof el.fill === "object" && el.fill) {
    const v: any = (el.fill as any)?.value
    if (v && (typeof v.picBase64 === "string" || typeof v.src === "string")) return true
    if (typeof (el.fill as any).picBase64 === "string" || typeof (el.fill as any).src === "string") return true
  }
  const t = String(el.type || "").toLowerCase()
  if (t === "pic" || t === "picture") return true
  return false
}

export function isImageElement(el: PptxElement) {
  const t = String(el.type || "").toLowerCase()
  if (hasImageFill(el)) return false
  if (t === "image" || t === "pic" || t === "picture") return true
  const src = getMediaSrc(el)
  return !!src && /^data:image\//i.test(src)
}

export function isVideoElement(el: PptxElement) {
  const t = String(el.type || "").toLowerCase()
  if (t === "video") return true
  const src = getMediaSrc(el)
  return !!src && /^data:video\//i.test(src)
}

export function isAudioElement(el: PptxElement) {
  const t = String(el.type || "").toLowerCase()
  if (t === "audio") return true
  const src = getMediaSrc(el)
  return !!src && /^data:audio\//i.test(src)
}

export function isTableElement(el: PptxElement) {
  return String(el.type || "").toLowerCase() === "table"
}

export function isChartElement(el: PptxElement) {
  const t = String(el.type || "").toLowerCase()
  return t === "chart" || t === "charts"
}

export function isGroupElement(el: PptxElement) {
  const t = String(el.type || "").toLowerCase()
  if (t === "group" || t === "groupshape" || t === "grpsp" || t === "grp") return true
  const candidates = [(el as any)?.elements, (el as any)?.children, (el as any)?.childrens, (el as any)?.items, (el as any)?.spTree]
  const hasChildren = candidates.some((v) => Array.isArray(v) && v.length)
  if (hasChildren) return true
  return t.includes("group")
}

export function isMathElement(el: PptxElement) {
  const t = String(el.type || "").toLowerCase()
  return t === "math" || t === "equation"
}

export function isDiagramElement(el: PptxElement) {
  const t = String(el.type || "").toLowerCase()
  return t === "diagram" || t === "smartart" || t === "smart_art"
}
