import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import html from '@rollup/plugin-html';
import { glob } from 'glob';
import path from 'path';

/**
 * Obtener archivos desde el componente local
 */
function GetFilesArray(query) {
    return glob.sync(query);
}

function getComponentFiles(type) {
    return glob.sync(`vendor/koneko/**/resources/${type}/**/*.${type === 'js' ? 'js' : 'scss'}`);
}

// Processing Vendor JS Files
const vendorJsFiles = GetFilesArray('vendor/koneko/laravel-vuexy-admin/resources/assets/vendor/js/*.js');

// Processing Libs JS Files
const LibsJsFiles = GetFilesArray('vendor/koneko/laravel-vuexy-admin/resources/assets/vendor/libs/**/*.js');

// Processing Core, Themes & Pages Scss Files
const CoreScssFiles = GetFilesArray('vendor/koneko/laravel-vuexy-admin/resources/assets/vendor/scss/**/!(_)*.scss');

// Processing Libs Scss & Css Files
const LibsScssFiles = GetFilesArray('vendor/koneko/laravel-vuexy-admin/resources/assets/vendor/libs/**/!(_)*.scss');
const LibsCssFiles = GetFilesArray('vendor/koneko/laravel-vuexy-admin/resources/assets/vendor/libs/**/*.css');

// Processing Fonts Scss Files
const FontsScssFiles = GetFilesArray('vendor/koneko/laravel-vuexy-admin/resources/assets/vendor/fonts/!(_)*.scss');

// Archivos JS de assets de componentes
const AssetsJsFiles = GetFilesArray('vendor/koneko/**/resources/assets/js/**/*.js');

const pageJsFiles = getComponentFiles('js');
const pageScssFiles = getComponentFiles('scss');


// Processing Window Assignment for Libs like jKanban, pdfMake
function libsWindowAssignment() {
    return {
      name: 'libsWindowAssignment',

      transform(src, id) {
        if (id.includes('jkanban.js')) {
          return src.replace('this.jKanban', 'window.jKanban');
        } else if (id.includes('vfs_fonts')) {
          return src.replaceAll('this.pdfMake', 'window.pdfMake');
        }
      }
    };
}

export default defineConfig({
    plugins: [
        laravel({
            input: [
                'vendor/koneko/laravel-vuexy-admin/resources/assets/css/demo.css',
                ...vendorJsFiles,
                ...LibsJsFiles,
                ...CoreScssFiles,
                ...LibsScssFiles,
                ...LibsCssFiles,
                ...FontsScssFiles,
                ...pageJsFiles,
                ...pageScssFiles,
                ...AssetsJsFiles,
            ],
            refresh: [
                'resources/**',
                'app/**',
                'vendor/koneko/**/resources/**',
                'vendor/koneko/**/Livewire/**'
            ]
        }),
        html(),
        libsWindowAssignment(),
    ],
    resolve: {
        alias: {
            '@vuexy-admin': path.resolve(__dirname, 'vendor/koneko/laravel-vuexy-admin/resources/assets/js'),
        },
    },
    build: {
        emptyOutDir: true,
        rollupOptions: {
            external: ['axios']
        }
    },
    server: {
        cors: true,
        hmr: {
            host: '127.0.0.1',
        },
        fs: {
            allow: ['..'], // Permitir acceso a directorios superiores
        },
        watch: {
            ignored: [
                //'**/vendor/**',   // Ignorar TODAS las carpetas vendor
                '**/node_modules/**', // Ignorar node_modules también
                '**/storage/**',
                '**/public/**'
            ],
        }
    },

});

