// Data the plugin prints for the admin app (see Admin::boot_data()).
import type { PluginState, WordPressInfo } from '../shared/types.ts';

export type Screen = 'welcome' | 'overview' | 'standins' | 'knowledge' | 'widget' | 'settings';

export interface Boot {
  screen: Screen;
  version: string;
  restPath: string;
  /** Fingerprint of the connection the page was loaded on. */
  connection: string;
  adminUrl: string;
  homeUrl: string;
  urls: Record<
    | 'app'
    | 'chats'
    | 'standins'
    | 'history'
    | 'sites'
    | 'team'
    | 'subscription'
    | 'profile'
    | 'digest'
    | 'signup'
    | 'pricing'
    | 'terms'
    | 'privacy'
    | 'dpa'
    | 'guide'
    | 'widget'
    | 'policyGuide'
    | 'permalinks'
    | 'general'
    | 'consentApi'
    | 'siteHealth'
    | 'menus',
    string
  >;
  wp: WordPressInfo;
  state: PluginState;
}

declare global {
  interface Window {
    standChatAdmin?: Boot;
  }
}

export function boot(): Boot {
  if (!window.standChatAdmin) throw new Error('Stand Chat admin data is missing.');
  return window.standChatAdmin;
}

/** Admin URL for one of the plugin's pages, with query arguments. */
export function adminPage(page: string, args: Record<string, string> = {}): string {
  const url = new URL(boot().adminUrl, window.location.origin);
  url.searchParams.set('page', page);
  for (const [key, value] of Object.entries(args)) url.searchParams.set(key, value);
  return url.toString();
}

export const SCREEN_PAGES: Record<Screen, string> = {
  welcome: 'stand-chat',
  overview: 'stand-chat',
  standins: 'stand-chat-standins',
  knowledge: 'stand-chat-knowledge',
  widget: 'stand-chat-widget',
  settings: 'stand-chat-settings',
};
