'use strict'

const Joi = require('joi')
const { BaseSvgScrapingService } = require('..')

const schema = Joi.object({
  message: Joi.alternatives()
    .try(Joi.string().regex(/^\d*$/), Joi.equal('unknown'))
    .required(),
}).required()

module.exports = class SnykVulnerabilityBase extends BaseSvgScrapingService {
  static category = 'analysis'

  static defaultBadgeData = {
    label: 'vulnerabilities',
  }

  static render({ vulnerabilities }) {
    let color = 'red'
    if (vulnerabilities === '0') {
      color = 'brightgreen'
    }
    return {
      message: vulnerabilities,
      color,
    }
  }

  async fetch({ url, qs, errorMessages }) {
    const { message: vulnerabilities } = await this._requestSvg({
      url,
      schema,
      options: {
        qs,
      },
      errorMessages,
    })

    return { vulnerabilities }
  }
}
