<template>
	<div>
		<slot :navigation="navigation" />
	</div>
</template>
<script>
import { h } from 'vue';
import DoorOpen from '~icons/lucide/door-open';
import PanelTopInactive from '~icons/lucide/panel-top-inactive';
import Package from '~icons/lucide/package';
import Boxes from '~icons/lucide/boxes';
import Server from '~icons/lucide/server';
import WalletCards from '~icons/lucide/wallet-cards';
import Settings from '~icons/lucide/settings';
import App from '~icons/lucide/layout-grid';
import DatabaseZap from '~icons/lucide/database-zap';
import Activity from '~icons/lucide/activity';
import Logs from '~icons/lucide/scroll-text';
import Globe from '~icons/lucide/globe';
import Notification from '~icons/lucide/inbox';
import Code from '~icons/lucide/code';
import CreditCard from '~icons/lucide/credit-card';
import { unreadNotificationsCount } from '../data/notifications';

export default {
	name: 'NavigationItems',
	computed: {
		navigation() {
			if (!this.$team?.pg) return [];

			const routeName = this.$route?.name || '';
			const onboardingComplete = this.$team.pg.onboarding.complete;
			const isSaasUser = this.$team.pg.is_saas_user;
			const enforce2FA = Boolean(
				!this.$team.pg.is_desk_user &&
					this.$team.pg.enforce_2fa &&
					!this.$team.pg.user_info?.is_2fa_enabled,
			);

			return [
				{
					name: this.$t('Welcome'),
					icon: () => h(DoorOpen),
					route: '/welcome',
					isActive: routeName === 'Welcome',
					condition: !onboardingComplete,
				},
				{
					name: this.$t('Notifications'),
					icon: () => h(Notification),
					route: '/notifications',
					isActive: routeName === 'Jcloud Notification List',
					condition: onboardingComplete && !isSaasUser,
					badge: () => {
						if (unreadNotificationsCount.data > 0) {
							return h(
								'span',
								{
									class: '!ml-auto px-1.5 py-0.5 text-xs text-gray-600',
								},
								unreadNotificationsCount.data > 99
									? '99+'
									: unreadNotificationsCount.data,
							);
						}
					},
					disabled: enforce2FA,
				},
				{
					name: this.$t('Sites'),
					icon: () => h(PanelTopInactive),
					route: '/sites',
					isActive:
						['Site List', 'Site Detail', 'New Site'].includes(routeName) ||
						routeName.startsWith('Site Detail'),
					disabled: enforce2FA,
				},
				{
					name: this.$t('Benches'),
					icon: () => h(Package),
					route: '/benches',
					isActive: routeName.startsWith('Bench'),
					condition: this.$team.pg?.is_desk_user,
					disabled: !onboardingComplete || enforce2FA,
				},
				{
					name: this.$t('Release Group'),
					icon: () => h(Boxes),
					route: '/groups',
					isActive:
						[
							'Release Group List',
							'Release Group Detail',
							'New Release Group',
							'Release Group New Site',
							'Deploy Candidate',
						].includes(routeName) ||
						routeName.startsWith('Release Group Detail'),
					condition: onboardingComplete && !isSaasUser && this.$team.pg.is_pro,
					disabled: enforce2FA,
				},
				{
					name: this.$t('Jingrow Servers'),
					icon: () => h(Server),
					route: '/servers',
					isActive:
						['New Server'].includes(routeName) ||
						routeName.startsWith('Server'),
					condition: onboardingComplete && !isSaasUser && this.$team.pg.is_pro,
					disabled: enforce2FA,
				},
				{
					name: this.$t('Domain'),
					icon: () => h(Globe),
					route: '/domains',
					isActive:
						['Jsite Domain List', 'New Jsite Domain'].includes(routeName) ||
						routeName.startsWith('Jsite Domain'),
					disabled: enforce2FA,
				},
				{
					name: this.$t('Servers'),
					icon: () => h(Server),
					route: '/jsite-servers',
					isActive:
						['Jsite Servers', 'New Jsite Server'].includes(routeName) ||
						routeName.startsWith('Jsite Server'),
					disabled: enforce2FA,
				},
				{
					name: this.$t('Marketplace'),
					icon: () => h(App),
					route: '/apps',
					isActive: routeName.startsWith('Marketplace'),
					condition:
						this.$team.pg?.is_desk_user ||
						(!!this.$team.pg.is_developer && this.$team.pg.is_pro && this.$session.hasAppsAccess),
					disabled: enforce2FA,
				},
				{
					name: this.$t('Developer Tools'),
					icon: () => h(Code),
					route: '/devtools',
					condition: onboardingComplete && !isSaasUser && this.$team.pg.is_developer && this.$team.pg.is_pro,
					disabled: enforce2FA,
					children: [
						{
							name: this.$t('SQL Playground'),
							icon: () => h(DatabaseZap),
							route: '/sql-playground',
							isActive: routeName === 'SQL Playground',
						},
						{
							name: this.$t('Log Browser'),
							icon: () => h(Logs),
							route: '/log-browser',
							isActive: routeName === 'Log Browser',
						},
						{
							name: this.$t('Database Analyzer'),
							icon: () => h(Activity),
							route: '/database-analyzer',
							isActive: routeName === 'DB Analyzer',
						},
					].filter((item) => item.condition ?? true),
					isActive: ['SQL Playground', 'DB Analyzer', 'Log Browser'].includes(
						routeName,
					),
					disabled: enforce2FA,
				},
				{
					name: this.$t('Recharge'),
					icon: () => h(CreditCard),
					route: '/recharge',
					isActive: routeName.startsWith('RechargeCredits'),
					condition:
						this.$team.pg?.is_desk_user || this.$session.hasBillingAccess,
					disabled: enforce2FA,
				},				
				{
					name: this.$t('Billing'),
					icon: () => h(WalletCards),
					route: '/billing',
					isActive: routeName.startsWith('Billing'),
					condition:
						this.$team.pg?.is_desk_user || this.$session.hasBillingAccess,
					disabled: enforce2FA,
				},
				{
					name: this.$t('Partner Portal'),
					icon: () => h(Globe),
					route: '/partners',
					isActive: routeName.startsWith('Partner'),
					condition: Boolean(this.$team.pg.jerp_partner),
					disabled: enforce2FA,
				},
				{
					name: this.$t('Settings'),
					icon: () => h(Settings),
					route: '/settings',
					isActive: routeName.startsWith('Settings'),
					disabled: enforce2FA,
				},
			].filter((item) => item.condition ?? true);
		},
	},
	mounted() {
		this.$socket.emit('pagetype_subscribe', 'Jcloud Notification');
		this.$socket.on('jcloud_notification', (data) => {
			if (data.team === this.$team.pg.name) {
				unreadNotificationsCount.setData((data) => data + 1);
			}
		});
	},
	unmounted() {
		this.$socket.off('jcloud_notification');
	},
};
</script>