/** 客服 iframe 基址，可通过 VITE_CUSTOMER_SERVICE_URL 覆盖（如外部客服页） */
export const CUSTOMER_SERVICE_BASE =
  (import.meta.env.VITE_CUSTOMER_SERVICE_URL as string | undefined)?.trim() || '/tawk.html';

export type TawkVisitor = {
  name?: string;
  avatar?: string;
  id?: string;
};

export function buildCustomerServiceUrl(
  loadingText: string,
  visitor?: TawkVisitor | null,
): string {
  const params = new URLSearchParams({ loadingText });

  if (visitor) {
    if (visitor.name) params.set('name', visitor.name);
    if (visitor.avatar) params.set('avatar', visitor.avatar);
    if (visitor.id) params.set('id', visitor.id);
  }

  const separator = CUSTOMER_SERVICE_BASE.includes('?') ? '&' : '?';
  return `${CUSTOMER_SERVICE_BASE}${separator}${params.toString()}`;
}
