/**
 * PageManager page interface and helpers.
 * Used by PageData definitions and SidebarProvider.
 */

import type React from 'react';

export interface GenericPageData {
  id: string;
  path: string;
  name: string;
  description?: string;
  parentPath?: string;
  icon?: React.ComponentType<React.SVGProps<SVGSVGElement>>;
  title?: string;
  subtitle?: string;
  headerButtons?: Array<Record<string, unknown>>;
  content?: Array<Record<string, unknown>>;
  moduleEnabled?: boolean;
  order?: number;
  hide?: boolean;
  showInSidebar?: boolean;
  showInSidebarIf?: boolean;
  hasSubpages?: boolean;
  privilegeChecker?: () => Promise<boolean> | boolean;
  persistent?: boolean;
  preload?: boolean;
  preserveState?: boolean;
  onActivate?: () => void | Promise<void>;
  onLoad?: () => void | Promise<void>;
  onUnload?: () => void | Promise<void>;
}

type TranslationFunction = (key: string, fallback?: string) => string;

/**
 * Resolve display text from a page name that may be a i18n key or { de?, en? }.
 */
export function resolveLanguageText(
  name: string | { de?: string; en?: string },
  t: TranslationFunction
): string {
  if (typeof name === 'string') {
    const resolved = t(name);
    return resolved !== name ? resolved : name;
  }
  const lang = (typeof navigator !== 'undefined' && navigator.language?.startsWith('en')) ? 'en' : 'de';
  return name[lang] ?? name.de ?? name.en ?? '';
}
