'user strict'
const Discord = require('discord.js')
const { ErrorCodes } = require('../Errors/ErrorCodes')
const { BaseManager } = require('./BaseManager')
const { MissingValue, InvalidValue, RankPriorityInUse } = require('../Errors/LME')
/** @typedef {import('../../typings').Rank} Rank */
/**
* Manager for the ranks.
* @extends {BaseManager<Rank>}
*/
class RankManager extends BaseManager {
/**
* @param {Discord.Client} client - The Discord Client.
* @param {Rank[]} iterable - Array of ranks.
*/
constructor (client, iterable) {
super(client, RankManager)
if (!iterable) throw new MissingValue(ErrorCodes.MissingArgument, 'iterable')
if (!Array.isArray(iterable)) throw new InvalidValue(ErrorCodes.InvalidValue, 'iterable', 'an array')
for (const item of iterable) {
this._add(item)
}
}
/**
* Add a new rank to the cache.
* @param {Rank} rank - The rank to add.
* @returns {Rank} - The rank added.
* @private
* @ignore
*/
_add (rank) {
if (typeof rank !== 'object') throw new InvalidValue(ErrorCodes.InvalidValue, 'rank', 'an object')
const existing = this.cache.get(rank.priority.toString())
if (existing) throw new RankPriorityInUse(rank.priority, existing.nameplate)
this.cache.set(rank.priority.toString(), rank)
return rank
}
}
module.exports = { RankManager }