/**
 * Get SASS shared resources
 *
 * Paths of sass file that might be made always available with
 * `sass-resource-loader`
 *
 * The order of the array is crucial here to enable SASS overriding defaults
 * behaviour.
 */
function getSassSharedResources() {
  const fs = require("fs");
  const path = require("path");
  const paths = require("../paths");
  const { coreLibrary } = require("../config");
  const coreSassConfig = paths.join(
    paths.root,
    "/node_modules/",
    coreLibrary + "-scss/"
  );

  const possiblePaths = [
    path.join(coreSassConfig, "_functions.scss"),
    path.join(paths.frontend.src.config, "/functions.scss"),
    path.join(paths.frontend.src.config, "/variables.scss"),
    path.join(coreSassConfig, "_variables.scss"),
    path.join(coreSassConfig, "_mixins.scss"),
    path.join(paths.frontend.src.config, "/mixins.scss"),
    path.join(coreSassConfig, "_placeholders.scss"),
    path.join(paths.frontend.src.config, "/placeholders.scss"),
  ];

  return possiblePaths.filter((sassFilePath) => {
    return fs.existsSync(path.resolve(sassFilePath));
  });
}

/**
 * Get SASS include paths
 *
 * Adds the core library node_modules folder if the core library exists, this
 * is needed when using the core library installed through `npm link` which
 * does not install its node_modules in the project root (in a flat deps
 * structure) but keeps them under the node_modules inside the symlinked
 * library's folder (e.g. in `./node_modules/@acanto/frontend/node_modules)
 *
 * @returns {string[]}
 */
function getSassIncludePaths() {
  const path = require("path");
  const paths = require("../paths");
  const { getCoreLibraryNodeModulesPath } = require("./coreLibrary");
  const includePaths = [path.resolve(path.join(paths.root, "node_modules/"))];
  const coreLibraryNodeModules = getCoreLibraryNodeModulesPath();

  // FIXME: this is not working, but not sure if still needed
  if (coreLibraryNodeModules) {
    includePaths.push(coreLibraryNodeModules);
  }

  return includePaths;
}

/**
 * Get SASS additional data based on env variables and paths/urls
 *
 * @returns {string}
 */
function getSassAdditionalData() {
  const path = require("path");
  const { frontend } = require("../paths");
  const { getPublicUrls } = require("./index");
  const publicUrls = getPublicUrls();
  const output = [
    `$DEV: ${process.env.NODE_ENV !== "production"};`,
    `$ENV: "${process.env.APP_ENV}";`,
    `$SRC_ASSETS: "${path.relative(frontend.src.utils, frontend.src.assets)}";`,
    `$URL_ASSETS: "${publicUrls.assets}";`,
    `$SRC_FONTS: "${path.relative(frontend.src.utils, frontend.src.fonts)}";`, // "../assets/fonts";`,
    `$URL_FONTS: "${publicUrls.fonts}";`,
    `$SRC_IMAGES: "${path.relative(frontend.src.utils, frontend.src.images)}";`,
    `$URL_IMAGES: "${publicUrls.images}";`,
    `$SRC_SVGICONS: "${frontend.src.svgicons}";`,
  ];

  return output.join(" ");
}

module.exports = {
  getSassSharedResources,
  getSassIncludePaths,
  getSassAdditionalData,
};
