///|
pub struct AttachmentInfo {
  filename : String
  content_type : String
  encoding : TransferEncoding
  size : Int
} derive(Eq, Debug)

///|
pub fn attachment_infos(message : MailMessage) -> Array[AttachmentInfo] {
  let out : Array[AttachmentInfo] = []
  collect_attachment_infos(message.root, out)
  out
}

///|
fn collect_attachment_infos(
  part : MailPart,
  out : Array[AttachmentInfo],
) -> Unit {
  if is_attachment(part) {
    out.push({
      filename: part.filename,
      content_type: part.content_type.media_type +
      "/" +
      part.content_type.subtype,
      encoding: part.encoding,
      size: part.decoded_body.length(),
    })
  }
  for child in part.children {
    collect_attachment_infos(child, out)
  }
}

///|
pub fn attachment_names(message : MailMessage) -> Array[String] {
  let infos = attachment_infos(message)
  let out : Array[String] = []
  for item in infos {
    out.push(item.filename)
  }
  out
}

///|
pub fn has_attachment_named(message : MailMessage, filename : String) -> Bool {
  for item in attachment_infos(message) {
    if item.filename == filename {
      return true
    }
  }
  false
}

///|
pub fn total_attachment_size(message : MailMessage) -> Int {
  let mut total = 0
  for item in attachment_infos(message) {
    total = total + item.size
  }
  total
}