'use strict'

var listAssets = require('list-assets')
var isCSS = require('is-css')
var gutil = require('gulp-util')
var minimatch = require('minimatch')
var Transform = require('stream').Transform

// const PLUGIN_NAME = 'gulp-resource-hints'

var defaults = {
  pageToken: '##gulp-resource-hints##',
  paths: {
    prefetch: '**/*.+(png|svg|jpg|gif|woff|woff2)'
  }
}

var fonts = [
  '**/*.woff',
  '**/*.woff2',
  '**/*.ttf',
  '**/*.eot',
  '**/*.eot',
  '**/*.otf'
]

function matches (file, globs) {
  for (let i = 0, len = globs.length; i < len; i++) {
    if (minimatch(file, globs[i])) {
      return true
    }
  }
}

function buildResourceHint (hint, asset) {
  var as = 'as="'

  if (matches(asset, fonts)) {
    as += 'font"'
  } else if (isCSS(asset)) {
    as += 'style"'
  } else {
    as = ''
  }

  return `<link rel="${hint}" href="${asset}" ${as} />`
}

function gulpResourceHints (opt) {
  opt = typeof opt === 'object' ? opt : {}
  var options = Object.assign(defaults, opt)

  // console.log('Options are', options)

  var stream = new Transform({
    objectMode: true,
    transform: function (file, enc, cb) {
      if (file.isNull()) {
        gutil.log('no file')

        cb(null, file)
        return
      }

      // Gather assets
      var assets = listAssets.html(String(file.contents)).map((ob) => {
        return ob.url
      })

      // Build resource hints based on user-selected assets
      var data = ''

      for (let i = 0, len = assets.length; i < len; i++) {
        Object.keys(options.paths).forEach((key) => {
          if (minimatch(assets[i], options.paths[key])) {
            data += buildResourceHint(key, assets[i])
          }
        })
      }

      file.contents = new Buffer(String(file.contents)
        .replace(options.pageToken, data))

      cb(null, file)
    }
  })

  return stream
}

module.exports = gulpResourceHints

@@ -356,21 +356,119 @@
 %7Cwoff2)'
-%0A  %7D%0A
+,%0A    preload: '',%0A    preconnect: '',%0A    'dns-prefetch': '',%0A    prerender: ''%0A  %7D%0A%7D%0A%0Avar options = %7B
 %7D%0A%0Avar f
@@ -979,117 +979,702 @@
 ion 
-gulpResourceHints (o
+mergeOptions (userOpts) %7B%0A  // iterate over existing keys%0A  if (!userO
 pt
+s
 ) %7B%0A  
+ 
+ 
 opt
- = typeof opt === 'object' ? opt :
+ions = defaults%0A  %7D else %7B%0A    var iterate = function (obj) %7B%0A      for (var key in defaults) %7B%0A        if (typeof obj%5Bkey%5D === 'object' && typeof defaults%5Bkey%5D !== 'object') %7B%0A          // Deep compare if object found%0A          iterate(obj%5Bkey%5D)%0A        %7D else if (!obj%5Bkey%5D)
  %7B
-%7D
 %0A  
-var options = Object.assign(defaults, 
+        // Use default since there is no user option%0A          options%5Bkey%5D = defaults%5Bkey%5D%0A        %7D else if (typeof defaults%5Bkey%5D === typeof obj%5Bkey%5D) %7B%0A          // Use user-defined valud%0A          options%5Bkey%5D = obj%5Bkey%5D%0A        %7D%0A      %7D%0A    %7D%0A%0A    iterate(userOpts)%0A  %7D%0A%7D%0A%0Afunction gulpResourceHints (opt) %7B%0A  mergeOptions(
 opt)
@@ -2131,27 +2131,27 @@
 %0A      for (
-let
+var
  i = 0, len 

add some defaults