/**
 * Open a RazorBooking admin page in a new tab.
 *
 * The address comes from the app URL configured for this site
 * (`RAZORBOOKING_APP_URL`), plus the path of the page. A signed-out visitor
 * is sent to login and brought back to that page afterwards.
 */

import { appUrl } from './boot';

/** Pages the plugin links to in the RazorBooking admin. */
export type Destination =
	| 'home'
	| 'calendar'
	| 'bookings'
	| 'customers'
	| 'transactions'
	| 'reports'
	| 'services'
	| 'service'
	| 'service_duplicate'
	| 'service_create'
	| 'resources'
	| 'categories'
	| 'team'
	| 'team_member'
	| 'invitations'
	| 'settings'
	| 'business_details'
	| 'business_hours'
	| 'policies'
	| 'booking_page_appearance'
	| 'gallery'
	| 'reviews'
	| 'faq'
	| 'booking_forms'
	| 'booking_rules'
	| 'domain'
	| 'payment_methods'
	| 'tax'
	| 'voice_agent'
	| 'access'
	| 'api_credentials'
	| 'notifications'
	| 'integrations'
	| 'activity'
	| 'plan'
	| 'billing'
	| 'support'
	| 'feedback';

const PATHS: Record< Destination, string > = {
	home: 'dashboard',
	calendar: 'calendar',
	bookings: 'bookings',
	customers: 'customers',
	transactions: 'payments',
	reports: 'reports',
	services: 'services',
	service: 'services',
	service_duplicate: 'services',
	service_create: 'services/create',
	resources: 'resources',
	categories: 'categories',
	team: 'users',
	team_member: 'users',
	invitations: 'invitations',
	settings: 'settings',
	business_details: 'settings/general',
	business_hours: 'settings/availability',
	policies: 'settings/policy',
	booking_page_appearance: 'settings/appearance',
	gallery: 'settings/gallery',
	reviews: 'settings/reviews',
	faq: 'settings/faq',
	booking_forms: 'settings/booking-forms',
	booking_rules: 'settings/booking-preferences',
	domain: 'settings/domain',
	payment_methods: 'settings/payments',
	tax: 'settings/tax',
	voice_agent: 'settings/voice-agent',
	access: 'settings/roles-permission',
	api_credentials: 'settings/api',
	notifications: 'settings/notifications',
	integrations: 'settings/integrations',
	activity: 'settings/activity-log',
	plan: 'billing',
	billing: 'billing/details',
	support: 'settings/support',
	feedback: 'settings/feedback',
};

export function handoffUrl( to: Destination, id?: string | number, query?: Record< string, string > ): string {
	const search = query ? `?${ new URLSearchParams( query ).toString() }` : '';

	return appUrl( pathFor( to, id ) ) + search;
}

export function openHandoff(
	to: Destination,
	id?: string | number,
	query?: Record< string, string >
): void {
	window.open( handoffUrl( to, id, query ), '_blank', 'noopener,noreferrer' );
}

function pathFor( to: Destination, id?: string | number ): string {
	const record = id === undefined || String( id ) === '' ? '' : encodeURIComponent( String( id ) );

	if ( to === 'service' && record ) {
		return `services/${ record }/edit`;
	}

	if ( to === 'service_duplicate' && record ) {
		return `services/${ record }/duplicate`;
	}

	if ( to === 'team_member' && record ) {
		return `users/${ record }/edit`;
	}

	return PATHS[ to ];
}
