/**
 * NewsCategory
 *
 * Immutable category model used across the application.
 */
class NewsCategory {
  /**
   * @param {string} key
   * @param {string} query
   * @param {string} fileName
   * @param {string} label
   * @param {string} apiUrl
   * @param {boolean} worldNews
   */
  constructor(key, query, fileName, label, apiUrl, worldNews = false) {
    this.key = key
    this.query = query
    this.fileName = fileName
    this.label = label
    this.apiUrl = apiUrl
    this.worldNews = worldNews
  }
}

/**
 * NewsCategoryCatalog
 *
 * Encapsulates all supported news categories and lookup behavior.
 */
class NewsCategoryCatalog {
  constructor() {
    this._categories = [
      new NewsCategory('flashnews', 'flashnews', 'flashnews.json', 'Realtime News', 'https://m.stock.naver.com/front-api/news/category?category=flashnews&pageSize=20&page=1'),
      new NewsCategory('mainnews', 'mainnews', 'mainnews.json', 'Main News', 'https://m.stock.naver.com/front-api/news/category?category=mainnews&pageSize=20&page=1'),
      new NewsCategory('ranknews', 'ranknews', 'ranknews.json', 'Rank News', 'https://m.stock.naver.com/front-api/news/category?category=ranknews&pageSize=20&page=1'),
      new NewsCategory('worldnews', 'worldnews', 'worldnews.json', 'World News', 'https://m.stock.naver.com/front-api/news/worldnews?pageSize=20&page=1', true)
    ]
    this._aliases = new Map([
      ['finance', 'flashnews'],
      ['business', 'mainnews'],
      ['technology', 'ranknews'],
      ['market', 'worldnews'],
      ['flash', 'flashnews'],
      ['main', 'mainnews'],
      ['rank', 'ranknews'],
      ['ranks', 'ranknews'],
      ['world', 'worldnews']
    ])
  }

  /**
   * @returns {NewsCategory[]}
   */
  all() {
    return [...this._categories]
  }

  /**
   * @param {string} key
   * @returns {NewsCategory | null}
   */
  getByKey(key) {
    const normalizedKey = this._aliases.get(key) || key
    return this._categories.find((item) => item.key === normalizedKey) || null
  }
}

module.exports = {
  NewsCategory,
  NewsCategoryCatalog
}
