'use strict';

var PluginError = require('plugin-error'),
    through = require('through2'),
    inlineCss = require('inline-css');

module.exports = function (opt) {
    return through.obj(function (file, enc, cb) {
        var self = this,
            _opt = JSON.parse(JSON.stringify(opt || {}));

        // 'url' option is required
        // set it automatically if not provided
        if (!_opt.url) {
            _opt.url = 'file://' + file.path;
        }

        if (file.isNull()) {
            cb(null, file);
            return;
        }

        if (file.isStream()) {
            cb(new PluginError('gulp-inline-css', 'Streaming not supported'));
            return;
        }

        inlineCss(file.contents, _opt)
            .then(function (html) {
                file.contents = new Buffer(String(html));

                self.push(file);

                return cb();
            })
            .catch(function (err) {
                if (err) {
                    self.emit('error', new PluginError('gulp-inline-css', err));
                }
            });
    });
};

@@ -707,16 +707,23 @@
 lineCss(
+String(
 file.con
@@ -727,16 +727,17 @@
 contents
+)
 , _opt)%0A
@@ -819,21 +819,13 @@
 fer(
-String(
 html)
-)
 ;%0A%0A 

Convert file to string before sending downstream.