import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import config from '../shared/config.js'
import { toSass } from '../shared/libs/sass/index.js'
import { metaApiPlugin } from './src/libs/vite-plugin-meta-api.js'

export default defineConfig({
    plugins: [vue(), metaApiPlugin()],
    server: {
        port: 8787,
    },
    resolve: {
        alias: {
            '@': fileURLToPath(new URL('./src', import.meta.url)),
            '@shared': fileURLToPath(new URL('../shared', import.meta.url)),
        },
    },
    css: {
        preprocessorOptions: {
            scss: {
                api: 'modern-compiler',
                additionalData:
                    '@use "@shared/styles/_functions.scss" as *; @use "@shared/styles/_font-style.scss" as *;',
                functions: {
                    'get($keys)': function (keys) {
                        keys = keys.toString().replace(/['"]+/g, '').split('.')

                        let result = config
                        for (let i = 0; i < keys.length; i++) {
                            result = result[keys[i]]
                        }

                        return toSass(result)
                    },
                    'getColors()': function () {
                        return toSass(config.colors)
                    },
                    'getThemes()': function () {
                        return toSass(config.themes)
                    },
                },
            },
        },
    },
})
