const process = require('process');
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const currDir = process.cwd();

const { fullConfig, modules } = require('../../WpjConfig');

let icons = [];
let iconsNames = [];

if (fs.existsSync(currDir + '/templates/icons/')) {
   const filesShop = fs.readdirSync(currDir + '/templates/icons/');

   filesShop.forEach(file => {
      if (path.extname(file) === '.svg') {
         icons.push(currDir + '/templates/icons/' + file);
         iconsNames.push(file);
      }
   });
}

if (fullConfig['frontend_bundles']) {
   for (const bundle in fullConfig['frontend_bundles']) {
      if (fs.existsSync(fullConfig['frontend_bundles'][bundle] + '/icons/')) {
         const filesShop = fs.readdirSync(fullConfig['frontend_bundles'][bundle] + '/icons/');

         filesShop.forEach(file => {
            if (path.extname(file) === '.svg' && !iconsNames.includes(file)) {
               icons.push(fullConfig['frontend_bundles'][bundle] + '/icons/' + file);
               iconsNames.push(file);
            }
         });
      }
   }
}

const sharedIcons = (modules['COMPONENTS'] === 'true') ? fs.realpathSync(currDir + '/engine/web/templates/x/static/icons/') : fs.realpathSync(currDir + '/static/icons/');

if (fs.existsSync(sharedIcons)) {
   const filesEngine = fs.readdirSync(sharedIcons);

   filesEngine.forEach(file => {
      if (path.extname(file) === '.svg' && !iconsNames.includes(file)) {
         icons.push(sharedIcons + '/' + file);
      }
   });
}


module.exports = icons;
