/**
 * Module: {{ dashboard_name }} entry point
 * Description: Dashboard module contract — exports mount/unmount for Hub loading
 *
 * @implements docs/dashboards/loading.md#module-contract
 * @implements docs/dashboards/publishing.md#build-contract
 * @implements docs/dashboards/sdk.md#initialization
 */

import { createRoot, type Root } from 'react-dom/client';
import { HuitzoProvider } from '@huitzo/dashboard-sdk-react';
import type { HuitzoMountContext } from '@huitzo/dashboard-sdk-react';
import App from './App';
import './index.css';

// Re-export the type so dev.tsx can import it
export type { HuitzoMountContext };

let root: Root | null = null;

/**
 * Mount the dashboard into the provided container.
 * Called by Hub when the user navigates to this dashboard.
 */
export function mount(container: HTMLElement, context: HuitzoMountContext): void {
  root = createRoot(container);
  root.render(
    <HuitzoProvider context={context}>
      <App />
    </HuitzoProvider>
  );
}

/**
 * Unmount the dashboard and clean up resources.
 * Called by Hub when the user navigates away.
 */
export function unmount(_container: HTMLElement): void {
  if (root) {
    root.unmount();
    root = null;
  }
}
