const addCodeSamples = require('../utils/addCodeSamples')

module.exports = class CodeSamplesGenerator {
  static get rule () {
    return 'githubCodeSamplesGenerator'
  }

  OpenAPIRoot () {
    return {
      onEnter: node => {
        this.serverUrl = node.servers[0].url
      }
    }
  }

  OpenAPIOperation () {
    return {
      onEnter: (operation, a, context) => {
        const [path, method] = context.pathStack[context.pathStack.length - 1].path.slice(-2)

        addCodeSamples(operation, path, method, this.serverUrl, context.filePath)
      }
    }
  }
}
