'use strict';

var gulp = require('gulp');
var browserSync = require('browser-sync');
var merge = require('merge-stream');
var $ = require('gulp-load-plugins')();

var wiredep = require('wiredep').stream;

module.exports = function (options) {
    gulp.task('styles-' + options.name, function () {
        var sassOptions = {
            style: 'expanded'
        };

        var injectFiles = gulp.src([
            '{' + options.src + ',' + options.common + '}' + '/**/*.scss',
            '!' + '{' + options.src + ',' + options.common + '}' + '/index.scss',
            '!' + '{' + options.src + ',' + options.common + '}' + '/vendor.scss'
        ], { read: false });

        var injectOptions = {
            transform: function (filePath) {
                filePath = filePath.replace(options.src, '');
                return '@import \'' + filePath + '\';';
            },
            starttag: '// injector',
            endtag: '// endinjector',
            addRootSlash: false
        };

        var indexFilter = $.filter('index.scss');
        var vendorFilter = $.filter('vendor.scss');

        var scss = gulp.src([
            options.common + '/index.scss',
            options.src + '/vendor.scss'
        ])
            .pipe(indexFilter)
            .pipe($.inject(injectFiles, injectOptions))
            .pipe(indexFilter.restore())
            .pipe(vendorFilter)
            .pipe(wiredep(options.wiredep))
            .pipe(vendorFilter.restore())
            .pipe($.sourcemaps.init())
            .pipe($.sass(sassOptions)).on('error', options.errorHandler('Sass'))
            .pipe($.autoprefixer()).on('error', options.errorHandler('Autoprefixer'))
            .pipe($.sourcemaps.write())
            .pipe(gulp.dest(options.tmp + '/serve/app/'))
            .pipe(browserSync.reload({ stream: true }));

        var css = gulp.src(options.src + '/**/*.css')
            .pipe(gulp.dest(options.tmp + '/serve/app/'));

        return merge(scss, css);
    });
};

@@ -413,34 +413,8 @@
     
- '%7B' + options.src + ',' +
  opt
@@ -426,22 +426,16 @@
 common +
- '%7D' +
  '/**/*.
@@ -462,34 +462,8 @@
 !' +
- '%7B' + options.src + ',' +
  opt
@@ -475,22 +475,16 @@
 common +
- '%7D' +
  '/index
@@ -512,34 +512,8 @@
 !' +
- '%7B' + options.src + ',' +
  opt
@@ -529,14 +529,8 @@
 on +
- '%7D' +
  '/v
@@ -568,16 +568,32 @@
 false %7D)
+.pipe($.print())
 ;%0A%0A     
@@ -1749,16 +1749,535 @@
 rue %7D));
+%0A        %0A        // Copy scss files that weren't injected in index.scss%0A        var userScss = %0A            gulp.src(options.src + '/**/*.scss')%0A            .pipe($.sourcemaps.init())%0A            .pipe($.sass(sassOptions)).on('error', options.errorHandler('Sass'))%0A            .pipe($.autoprefixer()).on('error', options.errorHandler('Autoprefixer'))%0A            .pipe($.sourcemaps.write())%0A            .pipe(gulp.dest(options.tmp + '/serve/app/'))%0A            .pipe(browserSync.reload(%7B stream: true %7D));%0A            
 %0A%0A      
@@ -2410,16 +2410,26 @@
 ge(scss,
+ userScss,
  css);%0A 

fix user styles not being included in final package