text: let querystring = require('querystring')
  let xhr = require('xhr')
  
  if (!xhr.open) xhr = require('request')
  
  let allowedProperties = [
    'fields',
    'channelId',
    'channelType',
    'eventType',
    'forContentOwner',
    'forDeveloper',
    'forMine',
    'location',
    'locationRadius',
    'onBehalfOfContentOwner',
    'order',
    'pageToken',
    'publishedAfter',
    'publishedBefore',
    'regionCode',
    'relatedToVideoId',
    'relevanceLanguage',
    'safeSearch',
    'topicId',
    'type',
    'videoCaption',
    'videoCategoryId',
    'videoDefinition',
    'videoDimension',
    'videoDuration',
    'videoEmbeddable',
    'videoLicense',
    'videoSyndicated',
    'videoType',
    'key'
  ]
  
  module.exports = function (term, opts) {
    return new Promise((resolve, reject) => {
      if (typeof opts === 'function') {
        opts = {}
      }
  
      let params = {
        q: term,
        part: opts.part || 'snippet',
        maxResults: opts.maxResults || 30
      }
  
      Object.keys(opts).map(function (k) {
        if (allowedProperties.indexOf(k) > -1) params[k] = opts[k]
      })
  
      xhr({
        url: 'https://www.googleapis.com/youtube/v3/search?' + querystring.stringify(params),
        method: 'GET'
      }, function (err, res, body) {
        if (err) throw err
  
        try {
          let result = JSON.parse(body)
  
          if (result.error) {
            let error = new Error(result.error.errors.shift().message)
            throw error
          }
  
          let pageInfo = {
            totalResults: result.pageInfo.totalResults,
            resultsPerPage: result.pageInfo.resultsPerPage,
            nextPageToken: result.nextPageToken,
            prevPageToken: result.prevPageToken
          }
  
          let findings = result.items.map(function (item) {
            let link = ''
            let id = ''
            switch (item.id.kind) {
              case 'youtube#channel':
                link = 'https://www.youtube.com/channel/' + item.id.channelId
                id = item.id.channelId
                break
              case 'youtube#playlist':
                link = 'https://www.youtube.com/playlist?list=' + item.id.playlistId
                id = item.id.playlistId
                break
              default:
                link = 'https://www.youtube.com/watch?v=' + item.id.videoId
                id = item.id.videoId
                break
            }
  
            return {
              id: id,
              link: link,
              kind: item.id.kind,
              publishedAt: item.snippet.publishedAt,
              channelId: item.snippet.channelId,
              channelTitle: item.snippet.channelTitle,
              title: item.snippet.title,
              description: item.snippet.description,
              thumbnails: item.snippet.thumbnails
            }
          })
  
          resolve(findings, pageInfo)
        } catch (error) {
          reject(error)
        }
      })
    })
  }
  