/**
 * Shell entry point — loaded on every page.
 *
 * Registers the three header web components:
 *   <rstack-app-switcher>
 *   <rstack-space-switcher>
 *   <rstack-identity>
 */

import { RStackIdentity } from "../shared/components/rstack-identity";
import { RStackAppSwitcher } from "../shared/components/rstack-app-switcher";
import { RStackSpaceSwitcher } from "../shared/components/rstack-space-switcher";
import { RStackTabBar } from "../shared/components/rstack-tab-bar";
import { RStackMi } from "../shared/components/rstack-mi";
import { rspaceNavUrl } from "../shared/url-helpers";
import { TabCache } from "../shared/tab-cache";

// Expose URL helper globally (used by shell inline scripts + components)
(window as any).__rspaceNavUrl = rspaceNavUrl;

// Expose TabCache for inline shell script to instantiate
(window as any).__RSpaceTabCache = TabCache;

// Register all header components
RStackIdentity.define();
RStackAppSwitcher.define();
RStackSpaceSwitcher.define();
RStackTabBar.define();
RStackMi.define();

// Reload space list when user signs in/out (to show/hide private spaces)
document.addEventListener("auth-change", () => {
	const spaceSwitcher = document.querySelector("rstack-space-switcher") as any;
	spaceSwitcher?.reload?.();

	// If signed out, redirect to homepage
	const session = localStorage.getItem("encryptid_session");
	if (!session) {
		window.location.href = "/";
	}
});
