"use strict";

// Load plugins

const browsersync = require("browser-sync").create();
const cp = require("child_process");
const cssnano = require("cssnano");
const gulp = require("gulp");
const sourcemaps = require("gulp-sourcemaps");
const del = require("del");
const embedSvg = require('gulp-embed-svg');
const plumber = require("gulp-plumber");
const postcss = require("gulp-postcss");
const rename = require("gulp-rename");
const easyimport = require("postcss-easy-import");
const csslogical = require("postcss-logical");
const cssCustomMedia = require("postcss-custom-media");
const cssCustomSelectors = require("postcss-custom-selectors");
const cssMediaMinMax = require("postcss-media-minmax");
const cssPresentEnv = require("postcss-preset-env");
const cssDeclarationSorter = require('css-declaration-sorter');

// BrowserSync
function browserSync(done) {
  browsersync.init({
    server: {
      baseDir: "./_site/"
    },
    port: 3000
  });
  done();
}

// BrowserSync Reload
function browserSyncReload(done) {
  browsersync.reload();
  done();
}

// Clean assets
function clean() {
  return del(["./_site/assets/"]);
}

// SVG 
function embedSvgs() {
  return gulp
    .src('_includes/hcard.html')
    .pipe(embedSvg({
      root: './assets/images/icons'
    }))
    .pipe(gulp.dest('_includes/.'))
}



// CSS task
function css() {
  var plugins = [
    easyimport(),
    csslogical({
      dir: 'ltr'
    }),
    cssCustomMedia(),
    cssCustomSelectors(),
    cssMediaMinMax(),
    cssPresentEnv(),
    cssDeclarationSorter({
      order: 'concentric-css'
    })
  ]
  return gulp
    .src("./assets/css/_inc/main.css")
    .pipe(sourcemaps.init())
    .pipe(postcss(plugins))
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest("./_site/assets/css/"))
    .pipe(gulp.dest("./assets/css/"))
    .pipe(browsersync.stream());
}

// Jekyll
function jekyll() {
  return cp.spawn("bundle", ["exec", "jekyll", "build", "--config", "_config.yml,_config_dev.yml"], {
    stdio: "inherit"
  });
}

// Watch files
function watchFiles() {
  gulp.watch("./assets/css/_inc/*", css);
  gulp.series(css, browserSyncReload)
  gulp.watch(
    [
      "./_includes/**/*",
      "./_layouts/**/*",
      "./_pages/**/*",
      "./_posts/**/*"
      
    ],
    gulp.series(embedSvgs, jekyll, css, browserSyncReload)
  );
}

// define complex tasks
const build = gulp.series(clean, embedSvgs, jekyll, css);
const watch = gulp.parallel(watchFiles, browserSync);

// export tasks

exports.css = css;
exports.jekyll = jekyll;
exports.clean = clean;
exports.embedSvgs   = embedSvgs;
exports.build = build;
exports.watch = watch;
exports.default = build;
@@ -1196,13 +1196,18 @@
 des/
-hcard
+icons_base
 .htm
@@ -1275,16 +1275,48 @@
     %7D))%0A
+    .pipe(rename('icons.html'))%0A
     .pip

use an include file for svg icons & preprocess another file