0 ? parts.at(0) : "", nameSection, parts.length() > 1 ? parts.at(1) : "", nameParts, !empty(nameSection) ? nameSection.split(" ") : [], firstInitial, nameParts.length() > 0 && !empty(nameParts.at(0)) ? substring(nameParts.at(0), 0, 1) + "." : "", middleInitial,"> 0 ? parts.at(0) : "", nameSection, parts.length() > 1 ? parts.at(1) : "", nameParts, !empty(nameSection) ? nameSection.split(" ") : [], firstInitial, nameParts.length() > 0 && !empty(nameParts.at(0)) ? substring(nameParts.at(0), 0, 1) + "." : "", middleInitial,"> 0 ? parts.at(0) : "", nameSection, parts.length() > 1 ? parts.at(1) : "", nameParts, !empty(nameSection) ? nameSection.split(" ") : [], firstInitial, nameParts.length() > 0 && !empty(nameParts.at(0)) ? substring(nameParts.at(0), 0, 1) + "." : "", middleInitial,">
/* APA7 Citation Formula - Refactored with 2.0 Syntax */

lets(
  /* Properties */
  itemType, prop("Item Type"),
  title, prop("Title"),
  authors, prop("Authors"),
  year, prop("Year"),
  doi, prop("DOI"),
  journalName, prop("Journal Name"),
  volume, prop("Volume"),
  issue, prop("Issue"),
  pageRange, prop("Page Range"),
  publisher, prop("Publisher"),
  bookTitle, prop("Book Title"),
  editor, prop("Editor"),

  /* Split authors */
  authorLines, !empty(authors) ? authors.split("\\n") : [],
  
  /* Process each author and store formatted results */
  formattedAuthors, 
  authorLines.filter(!empty(current)).map(
    let(
      parts, current.split(","),
      lastName, parts.length() > 0 ? parts.at(0) : "",
      nameSection, parts.length() > 1 ? parts.at(1) : "",
      nameParts, !empty(nameSection) ? nameSection.split(" ") : [],
      
      firstInitial, nameParts.length() > 0 && !empty(nameParts.at(0)) 
        ? substring(nameParts.at(0), 0, 1) + "." : "",
      middleInitial, nameParts.length() > 1 && !empty(nameParts.at(1)) 
        ? substring(nameParts.at(1), 0, 1) + "." : "",
      
      !empty(lastName) ? 
        lastName + ", " + firstInitial + (empty(middleInitial) ? "" : " " + middleInitial) 
        : ""
    )
  ),
  
  /* Join authors according to APA7 style - with comma before & in all cases */
  numAuthors, formattedAuthors.length(),
  
  joinedAuthors, ifs(
    numAuthors == 0, "",
    numAuthors == 1, formattedAuthors.at(0),
    numAuthors == 2, formattedAuthors.at(0) + ", & " + formattedAuthors.at(1),
    numAuthors >= 3, 
      formattedAuthors.slice(0, numAuthors - 1).join(", ") + 
      ", & " + formattedAuthors.at(numAuthors - 1),
    ""
  ),

  /* Format DOI properly */
  formattedDOI, empty(doi) ? "" : "<https://doi.org/>" + doi,

  /* Main citation logic based on item type */
  empty(itemType) ? "" : ifs(
    /* Journal Article format */
    itemType == "Journal Article",
      joinedAuthors + " (" + year + "). " +
      title + ". " +
      (empty(journalName) ? "" : journalName + ", ") +
      (empty(volume) ? "" : volume) +
      (empty(issue) ? "" : "(" + issue + ")") +
      (empty(pageRange) ? "" : ", " + pageRange) +
      (empty(doi) ? "." : ". " + formattedDOI),

    /* Book Section format */
    itemType == "Book Section",
      joinedAuthors + " (" + year + "). " +
      title + ". " +
      (empty(editor) ? "" : "In " + editor + " (Ed.), ") +
      (empty(bookTitle) ? "" : bookTitle + " ") +
      (empty(pageRange) ? "" : "(pp. " + pageRange + "). ") +
      (empty(publisher) ? "" : publisher + "."),

    /* Book format */
    itemType == "Book",
      joinedAuthors + " (" + year + "). " +
      title + ". " +
      (empty(publisher) ? "" : publisher + "."),

    /* Default format for other types */
    true, joinedAuthors + " (" + year + "). " + title + "."
  )
)