require("./bootstrap");

import { createApp, h } from 'vue';
import { createInertiaApp } from '@inertiajs/inertia-vue3';
import { InertiaProgress } from '@inertiajs/progress';
import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers';
import { ZiggyVue } from '../../vendor/tightenco/ziggy/dist/vue.m';
import translationHelper from './base.js';
import Notifications from 'notiwind';
import AppLayout from './Layouts/AppLayout.vue';
import AuthLayout from './Layouts/AuthLayout.vue';
import BaseLayout from './Layouts/BaseLayout.vue';
import GuestLayout from './Layouts/GuestLayout.vue';

const appName = window.document.getElementsByTagName('title')[0]?.innerText || 'Laravel';

window.genUid = function () {
    return [...Array(16)].map(() => Math.floor(Math.random() * 16).toString(16)).join('');
};

createInertiaApp({
    title: (title) => `${title} - ${appName}`,
    resolve: (name) => {
        const page = resolvePageComponent(`./Pages/${name}.vue`, import.meta.glob('./Pages/**/*.vue'));

        page.then((module) => {
            if (module.default.layout === undefined) {
                if (name.startsWith('Auth/')) {
                    module.default.layout = AuthLayout;
                } else if (name.startsWith('Dashboard/') || name.startsWith('Profile/')) {
                    module.default.layout = AppLayout;
                } else if (name.startsWith('Guest/')) {
                    module.default.layout = GuestLayout;
                } else if (!name.startsWith('Components/') && !name.startsWith('Jetstream/') && !name.startsWith('Icons/') && !name.startsWith('Layouts/') && !name.includes('Partials')) {
                    module.default.layout = BaseLayout;
                }
            }
        });

        return page;
    },
    setup({ el, app, props, plugin }) {
        return createApp({ render: () => h(app, props) })
            .use(plugin)
            .use(Notifications)
            .use(ZiggyVue, Ziggy)
            .mixin(translationHelper)
            .mixin({ methods: { route } })
            .mount(el);
    },
});

InertiaProgress.init({ color: "#4b5563" });
