const gulp = require('gulp'),
      sass = require('gulp-sass'),
      rename = require("gulp-rename"),
      concat = require("gulp-concat"),
      uglify = require('gulp-uglify'),
      cleanCSS = require('gulp-clean-css'),
      purgeCSS = require('gulp-purgecss'),
      sourcemaps = require('gulp-sourcemaps');
      purgeSourcemaps = require('gulp-purge-sourcemaps');
      browserSync = require('browser-sync').create();

async function copy() {
  gulp.src([
    './node_modules/bootstrap/dist/js/bootstrap.bundle.js',
    './node_modules/bootstrap/dist/css/bootstrap.css',
    './node_modules/bootstrap-icons/font/bootstrap-icons.css'
  ])
  .pipe(sourcemaps.init({loadMaps: true}))
  .pipe(purgeSourcemaps())
  .pipe(gulp.dest('./_src/'));
  gulp.src([
    './node_modules/bootstrap-icons/font/fonts/bootstrap-icons.woff',
    './node_modules/bootstrap-icons/font/fonts/bootstrap-icons.woff2'
  ])
  .pipe(gulp.dest('./css/fonts/'));
console.log("📄 Copied");
}

async function build() {
  gulp.src([
    './_src/bootstrap.bundle.js'
  ])
    .pipe(concat('script.js'))
    .pipe(uglify())
    .pipe(rename('script.min.js'))
  .pipe(gulp.dest('./js/'));
  gulp.src([
    './style.css',
    './css/custom.css',
    './_src/bootstrap-icons.css'
  ])
    .pipe(concat('style.css'))
    .pipe(cleanCSS({level: {1: {specialComments: 0}}}))
    .pipe(rename('style.min.css'))
  .pipe(gulp.dest('./css/'));
console.log("🔧 Built");
}

function mixin() {
  return gulp.src('./css/*.scss')
    .pipe(sass().on('error',sass.logError))
    .pipe(gulp.dest('./css/'))
    .pipe(browserSync.stream());
}

function run() {
  browserSync.init({
    injectChanges: true,
    proxy: 'https://macs.local/hp',
    host: 'a.macs',
    open: 'external',
    port: 8888,
    https: true
  });
  gulp.watch('./css/*.scss', mixin);
  gulp.watch([
    './*.css',
    './**/*.css',
    './**/*.scss',
    './**/*.js',
    './**/*.php'
  ]).on('change', browserSync.reload);
console.log("🔥 Ran");
}

exports.copy = copy;
exports.build = build;
exports.mixin = mixin;
exports.run = run;