/**
 * Tab Registry — single source of truth for tab metadata, subtab storage keys,
 * loader/switcher function names, and active-tab queries.
 *
 * Eliminates scattered if-chains that compare tab IDs as string literals.
 */

interface SubTabConfig {
    storageKey: string;
    defaultSubTab: string;
    switchFnName: string;
}

interface TabConfig {
    loadFnName: string;
    subTab?: SubTabConfig;
    autoRefresh?: boolean;
}

const TAB_REGISTRY: Readonly<Record<string, TabConfig>> = {
    dashboard:    { loadFnName: 'loadDashboard', autoRefresh: true },
    targets:      { loadFnName: 'loadTargetsTab', autoRefresh: true,
                    subTab: { storageKey: 'activeTargetSubTab', defaultSubTab: 'led-devices', switchFnName: 'switchTargetSubTab' } },
    streams:      { loadFnName: 'loadPictureSources',
                    subTab: { storageKey: 'activeStreamTab', defaultSubTab: 'raw', switchFnName: 'switchStreamTab' } },
    integrations: { loadFnName: 'loadIntegrations',
                    subTab: { storageKey: 'activeIntegrationTab', defaultSubTab: 'weather', switchFnName: 'switchIntegrationTab' } },
    automations:  { loadFnName: 'loadAutomations',
                    subTab: { storageKey: 'activeAutomationTab', defaultSubTab: 'automations', switchFnName: 'switchAutomationTab' } },
    graph:        { loadFnName: 'loadGraphEditor' },
};

/** Get the full config for a tab, or undefined if not registered. */
export function getTabConfig(tab: string): TabConfig | undefined {
    return TAB_REGISTRY[tab];
}

/** Get the subtab config for a tab, or undefined if the tab has no subtabs. */
export function getSubTabConfig(tab: string): SubTabConfig | undefined {
    return TAB_REGISTRY[tab]?.subTab;
}

/** Read the active subtab from localStorage (with default fallback), or null if tab has no subtabs. */
export function getActiveSubTab(tab: string): string | null {
    const cfg = TAB_REGISTRY[tab]?.subTab;
    if (!cfg) return null;
    return localStorage.getItem(cfg.storageKey) || cfg.defaultSubTab;
}

/** Write the active subtab to localStorage. No-op if tab has no subtabs. */
export function setActiveSubTab(tab: string, subTab: string): void {
    const cfg = TAB_REGISTRY[tab]?.subTab;
    if (cfg) localStorage.setItem(cfg.storageKey, subTab);
}

/** Call the tab's loader function via window. Returns true if the function existed and was called. */
export function callTabLoader(tab: string): boolean {
    const fnName = TAB_REGISTRY[tab]?.loadFnName;
    if (fnName && typeof (window as any)[fnName] === 'function') {
        (window as any)[fnName]();
        return true;
    }
    return false;
}

/** Call the tab's subtab switcher function via window. Returns true if the function existed and was called. */
export function callSubTabSwitcher(tab: string, subTab: string): boolean {
    const fnName = TAB_REGISTRY[tab]?.subTab?.switchFnName;
    if (fnName && typeof (window as any)[fnName] === 'function') {
        (window as any)[fnName](subTab);
        return true;
    }
    return false;
}

/** Check whether the given tab is currently active. */
export function isActiveTab(tab: string): boolean {
    return (localStorage.getItem('activeTab') || 'dashboard') === tab;
}

/** Get all tab names that have autoRefresh enabled. */
export function getAutoRefreshTabs(): string[] {
    return Object.entries(TAB_REGISTRY)
        .filter(([, cfg]) => cfg.autoRefresh)
        .map(([name]) => name);
}
