//Setup
export default async function({login, q, imports, data, graphql, queries, account}, {enabled = false} = {}) {
  //Plugin execution
  try {
    //Check if plugin is enabled and requirements are met
    if ((!enabled) || (!q.sponsors))
      return null

    //Load inputs
    let {size, sections, past} = await imports.metadata.plugins.sponsors.inputs({data, account, q})

    //Query description and goal
    console.debug(`metrics/compute/${login}/plugins > sponsors > querying sponsors and goal`)
    const {[account]: {sponsorsListing: {fullDescription, activeGoal}}} = await graphql(queries.sponsors.description({login, account}))
    const about = await imports.markdown(fullDescription, {mode: "multiline"})
    const goal = activeGoal ? {progress: activeGoal.percentComplete, title: activeGoal.title, description: await imports.markdown(activeGoal.description)} : null
    const count = {total: {count: 0, user: 0, organization: 0}, active: {total: 0, user: 0, organization: 0}, past: {total: 0, user: 0, organization: 0}}
    if (!goal)
      sections = sections.filter(section => section.name !== "goal")

    //Query active sponsors
    let list = []
    {
      const fetched = []
      let cursor = null
      let pushed = 0
      do {
        console.debug(`metrics/compute/${login}/sponsors > retrieving sponsors after ${cursor}`)
        const {[account]: {sponsorshipsAsMaintainer: {edges, nodes}}} = await graphql(queries.sponsors.active({login, account, after: cursor ? `after: "${cursor}"` : "", size: Math.round(size * 1.5)}))
        cursor = edges?.[edges?.length - 1]?.cursor
        fetched.push(...nodes)
        pushed = nodes.length
        console.debug(`metrics/compute/${login}/sponsors > retrieved ${pushed} sponsors after ${cursor}`)
      } while ((pushed) && (cursor))
      list.push(...fetched.map(({privacyLevel: privacy, sponsorEntity: {login, avatarUrl, url: organization = null}, tier}) => ({login, avatarUrl, type: organization ? "organization" : "user", amount: tier?.monthlyPriceInDollars ?? null, past: false, private: privacy === "PRIVATE"})))
      await Promise.all(list.map(async user => user.avatar = await imports.imgb64(user.avatarUrl)))
      count.active.total = list.length
      count.active.user = list.filter(user => user.type === "user").length
      count.active.organization = list.filter(user => user.type === "organization").length
    }

    //Query past sponsors
    if (past) {
      console.debug(`metrics/compute/${login}/plugins > sponsors > querying past sponsors`)
      const active = new Set(list.map(({login}) => login))
      const users = []
      {
        const fetched = []
        let cursor = null
        let pushed = 0
        do {
          console.debug(`metrics/compute/${login}/sponsors > retrieving sponsors events after ${cursor}`)
          const {[account]: {sponsorsActivities: {edges, nodes}}} = await graphql(queries.sponsors.all({login, account, after: cursor ? `after: "${cursor}"` : "", size: Math.round(size * 1.5)}))
          cursor = edges?.[edges?.length - 1]?.cursor
          fetched.push(...nodes)
          pushed = nodes.length
          console.debug(`metrics/compute/${login}/sponsors > retrieved ${pushed} sponsors events after ${cursor}`)
        } while ((pushed) && (cursor))
        users.push(...fetched.map(({sponsor: {login, avatarUrl, url: organization = null}, sponsorsTier}) => ({login, avatarUrl, type: organization ? "organization" : "user", amount: sponsorsTier?.monthlyPriceInDollars ?? null, past: true, private: false})))
      }
      for (const user of users) {
        if (!active.has(user.login)) {
          active.add(user.login)
          list.push({...user, avatar: await imports.imgb64(user.avatarUrl)})
          count.past.total++
          count.past[user.type]++
        }
      }
    }

    //Hide private sponsors avatars
    list.filter(user => user.private).map(user =>
      user.avatar =
        "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKgAAACoCAIAAAD7KTLjAAAACXBIWXMAAA7DAAAOwwHHb6hkAAALk0lEQVR42u2dZ1diWRaG61dUMhMkKoqCoqAiZgyYMCAoCqglGEGUVhFREHNoqyvN/NbZNc7U1KrmXLEkXJh3refDdI/NZfGcs8/eJ91Xr4sE4P+QV/gJIB5APIB4APEA4gHEA4gHEA8gHkA8gHgA8QDiAcQDiAcQDyAeQDyAeADxAOIBxAOIh3gA8QDiAcQDiAcQDyAeQDyAeADxAOIBxAOIBxAPIB5APID4Z1BULhFIVLJqrUqj1zSbmo19xh5L18B4z6C1vXfYYDI3GDprG1rkNQ0iWU2xQPamWAjxeczbElF5ZZW8prGlc2DM5lpc2wlFL+I3X87//OcvJO6+hU/vvP79Kedyh3m0ur5ZKK15V1YJ8XkGddkKSbVa1zYyPe/fjyWVzcEf0Uvb/IqutVssVxew/lcFprxEIJPXNk7OLkUvPp7d/+N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r1sxY1efRiadyOqfPsjSbOyNnN3/dDX4p46+kRKBnOfWHxmedB6d/2+kj99+mXAsimS1mMBJoSYuFYsV6vbe4aGJ2U7zmLRa+640b97vRQV9na7NMjHn8gYm55Y0ze28PUpdIHfgAIgHEA8gHkA8gHiIBxAPIB5APIB4APEA4gHEA4gHEA8gHkA8gHgA8QDiAcQDiAcQDyAeQDyAeADxAOIhHr8CxAOIBxAPIB5APIB4APEA4gHEA4gHEA8gHkA8gHgA8QDiAcQDiAcQDyAeMPkXNlCtrDZIMnkAAAAASUVORK5CYII="
    )

    //Update counters
    count.total.count = count.active.total + count.past.total
    count.total.user = count.active.user + count.past.user
    count.total.organization = count.active.organization + count.past.organization

    //Results
    list = list.sort((a, b) => a.private === b.private ? a.past === b.past ? b.amount - a.amount : a.past - b.past : a.private - b.private)
    return {sections, about, list, count, goal, size, past}
  }
  //Handle errors
  catch (error) {
    throw {error: {message: "An error occured", instance: error}}
  }
}
