const { MessageVisual } = require('discord.js-prompts')
const LocalizedPrompt = require('../common/utils/LocalizedPrompt.js')
const Translator = require('../../../structs/Translator.js')

/**
 * @typedef {Object} Data
 * @property {import('../../../structs/db/Profile.js')} [profile]
 * @property {import('../../../structs/db/Feed.js')[]} feeds
 * @property {import('../../../structs/db/Feed.js')} selectedFeed
 */

/**
 * @param {Data} data
 */
async function listSubscribersVisual (data) {
  const { profile, selectedFeed: feed } = data
  const translate = Translator.createProfileTranslator(profile)
  const subscribers = await feed.getSubscribers()

  if (subscribers.length === 0) {
    return new MessageVisual(translate('commands.mention.listSubscribersDescription', {
      link: feed.url,
      channel: `<#${feed.channel}>`
    }))
  }

  let output = `${translate('commands.mention.listSubscribersDescription', {
    link: feed.url,
    channel: `<#${feed.channel}>`
  })}\n`
  const userSubscribers = subscribers.filter(s => s.type === 'user')
  const roleSubscribers = subscribers.filter(s => s.type === 'role')

  const userSubscribersString = userSubscribers
    .map(s => `<@${s.id}>`)
    .join(' ')
  const roleSubscribersString = roleSubscribers
    .map(s => `<@&${s.id}>`)
    .join(' ')

  if (userSubscribersString) {
    output += `\n**Users**\n${userSubscribersString}`
  }
  if (roleSubscribersString) {
    output += `\n**Roles**\n${roleSubscribersString}`
  }
  return new MessageVisual(output, {
    // It may be so large, the message will have to be split
    split: true
  })
}

const prompt = new LocalizedPrompt(listSubscribersVisual)

exports.prompt = prompt

@@ -409,16 +409,541 @@
 ed%0A */%0A%0A
+/**%0A  * @param %7Bstring%5B%5D%7D mentionStrings%0A  */%0Afunction splitMentionsByNewlines (mentionStrings) %7B%0A  // Put 10 mentions on new lines so message splitting work properly%0A  const outputMentionArrs = %5B%5D%0A  for (const substring of mentionStrings) %7B%0A    const lastArray = outputMentionArrs%5BoutputMentionArrs.length - 1%5D%0A    if (!lastArray %7C%7C lastArray.length === 10) %7B%0A      outputMentionArrs.push(%5Bsubstring%5D)%0A    %7D else %7B%0A      lastArray.push(substring)%0A    %7D%0A  %7D%0A  return outputMentionArrs.map(arr =%3E arr.join(' ')).join('%5Cn')%0A%7D%0A%0A
 /**%0A * @
@@ -1274,35 +1274,28 @@
 tSubscribers
-Descripti
+N
 on
+e
 ', %7B%0A      l
@@ -1666,16 +1666,17 @@
 rsString
+s
  = userS
@@ -1714,31 +1714,16 @@
 .id%7D%3E%60)%0A
-    .join(' ')%0A
   const 
@@ -1743,16 +1743,17 @@
 rsString
+s
  = roleS
@@ -1795,23 +1795,8 @@
 %7D%3E%60)
-%0A    .join(' ')
 %0A%0A  
@@ -1812,32 +1812,44 @@
 ubscribersString
+s.length %3E 0
 ) %7B%0A    output +
@@ -1866,16 +1866,40 @@
 rs**%5Cn$%7B
+splitMentionsByNewlines(
 userSubs
@@ -1903,32 +1903,34 @@
 ubscribersString
+s)
 %7D%60%0A  %7D%0A  if (rol
@@ -1947,16 +1947,28 @@
 rsString
+s.length %3E 0
 ) %7B%0A    
@@ -1993,16 +1993,40 @@
 es**%5Cn$%7B
+splitMentionsByNewlines(
 roleSubs
@@ -2038,16 +2038,18 @@
 rsString
+s)
 %7D%60%0A  %7D%0A 

Fix mention list not showing if too long Fix wrong string showing for no subscribers in list option