// @ts-check

/**
 * Clean compiled template files location
 */
function tplClean() {
  const del = require("del");
  const paths = require("../paths");
  return del(paths.join(paths.frontend.dest.base, "**/*"));
}

/**
 * Build only template files ( blade templates and php)
 */
function tplBuild(callback) {
  const gulp = require("gulp");
  const coreSync = require("./coreSync");
  const tplComponents = require("./tplComponents");
  const tplLayouts = require("./tplLayouts");
  const tplRoutes = require("./tplRoutes");
  const tplUtils = require("./tplUtils");

  return gulp.parallel([
    coreSync,
    tplComponents,
    tplLayouts,
    tplRoutes,
    tplUtils,
  ])(callback);
}

/**
 * Watch only template files ( blade templates and php)
 */
function tplWatch(callback) {
  const gulp = require("gulp");
  const path = require("path");
  const del = require("del");
  const paths = require("../paths");
  const { getWatchPath } = require("../utils");
  const tplComponents = require("./tplComponents");
  const tplLayouts = require("./tplLayouts");
  const tplRoutes = require("./tplRoutes");
  const tplUtils = require("./tplUtils");
  const { join, frontend } = paths;
  const { src, dest } = frontend;

  gulp.watch(getWatchPath(join(src.components, "/**/*.php")), tplComponents);
  gulp.watch(getWatchPath(join(src.layouts, "/**/*.php")), tplLayouts);
  gulp.watch(getWatchPath(join(src.utils, "/**/*.php")), tplUtils);
  gulp
    .watch(getWatchPath(join(src.routes, "/**/*.{php,json}")), tplRoutes)
    .on("unlink", (filepath) => {
      const filePathFromSrc = path.relative(path.resolve(src.routes), filepath);
      const destFilePath = path.resolve(dest.routes, filePathFromSrc);
      del.sync(destFilePath);
    });
}

/**
 * Exposse build and watch task separetely
 */
module.exports = {
  tplClean,
  tplBuild,
  tplWatch,
};
