var BroccoliSass = require("broccoli-sass-source-maps")(require("sass"));
var BroccoliPostCSS = require("broccoli-postcss");
var BroccoliUglify = require("broccoli-uglify-sourcemap");
var BroccoliMergeTrees = require("broccoli-merge-trees");
var BroccoliRollup = require("broccoli-rollup");
var resolve = require("@rollup/plugin-node-resolve");
var commonjs = require("rollup-plugin-commonjs");
var terser = require("@rollup/plugin-terser");
var env = process.env.BROCCOLI_ENV || "development";
var isProduction = env === "production";

var css = new BroccoliSass(["src/css"], "index.scss", "css/thedock-enhanced-rich-text-editor-admin.css");

var postcssPlugins = [
  {
    module: require("autoprefixer"),
  },
];

css = new BroccoliPostCSS(css, {
  plugins: postcssPlugins,
});

// Build Rollup plugins array
var rollupPlugins = [
  resolve({
    skip: ["jquery", "tinymce"],
  }),
  commonjs(),
];

// Add terser for production builds to minify and remove console statements
if (isProduction) {
  rollupPlugins.push(
    terser({
      compress: {
        drop_console: true,
        drop_debugger: true,
      },
      format: {
        comments: false,
      },
    })
  );
}

var js = new BroccoliRollup("src/js", {
  external: ["jquery", "tinymce"],
  rollup: {
    input: "main.js",
    output: {
      file: "js/thedock-enhanced-rich-text-editor-admin.js",
      format: "es",
      globals: {
        jquery: "jQuery",
        tinymce: "tinymce"
      },
      intro: "(function($, tinymce) { 'use strict';",
      outro: "})(jQuery, tinymce);",
    },
    plugins: rollupPlugins,
  },
});

module.exports = new BroccoliMergeTrees([css, js], { overwrite: true });
