/* eslint-disable react-refresh/only-export-components */

import { type ComponentType, lazy, type LazyExoticComponent } from 'react';

import { isEdutainmentEntryEnabled } from '../components/platform-entry/platformEdutainmentVisibility';
import { normalizeAppPath } from './appPageRoutes';

type AppRouteComponent = LazyExoticComponent<
  ComponentType<Record<string, unknown>>
>;

export type AppRouteMatch =
  | {
      kind: 'puzzle-playground';
    }
  | {
      kind: 'big-fish-playground';
    }
  | {
      kind: 'match3d-playground';
    }
  | {
      kind: 'bark-battle-playground';
    }
  | {
      kind: 'child-motion-demo';
    }
  | {
      kind: 'baby-love-drawing';
    }
  | {
      kind: 'game';
    };

export type ResolvedAppRoute = {
  kind: AppRouteMatch['kind'];
  loadingEyebrow: string;
  loadingText: string;
  Component: AppRouteComponent;
  componentProps?: Record<string, unknown>;
};

const GameApp = lazy(() => import('../AuthenticatedApp')) as AppRouteComponent;
const BigFishPlaygroundApp = lazy(() => import('../BigFishPlaygroundApp')) as AppRouteComponent;
const Match3DPlaygroundApp = lazy(() => import('../Match3DPlaygroundApp')) as AppRouteComponent;
const BarkBattlePlaygroundApp = lazy(() => import('../BarkBattlePlaygroundApp')) as AppRouteComponent;
const PuzzlePlaygroundApp = lazy(() => import('../PuzzlePlaygroundApp')) as AppRouteComponent;
const ChildMotionDemoApp = lazy(() => import('../ChildMotionDemoApp')) as AppRouteComponent;
const BabyLoveDrawingRuntimeApp = lazy(
  () => import('../components/edutainment-runtime/BabyLoveDrawingRuntimeShell'),
) as AppRouteComponent;

function normalizeRoutePath(pathname: string) {
  return normalizeAppPath(pathname);
}

export function matchAppRoute(pathname: string): AppRouteMatch {
  const normalizedPath = normalizeRoutePath(pathname);

  if (normalizedPath === '/puzzle') {
    return {
      kind: 'puzzle-playground',
    };
  }

  if (normalizedPath === '/big-fish') {
    return {
      kind: 'big-fish-playground',
    };
  }

  if (normalizedPath === '/match3d') {
    return {
      kind: 'match3d-playground',
    };
  }

  if (normalizedPath === '/bark-battle') {
    return {
      kind: 'bark-battle-playground',
    };
  }

  if (
    normalizedPath === '/child-motion-demo' &&
    isEdutainmentEntryEnabled()
  ) {
    return {
      kind: 'child-motion-demo',
    };
  }

  if (
    normalizedPath === '/runtime/baby-love-drawing' &&
    isEdutainmentEntryEnabled()
  ) {
    return {
      kind: 'baby-love-drawing',
    };
  }

  return {
    kind: 'game',
  };
}

export function resolveAppRoute(pathname: string): ResolvedAppRoute {
  const matchedRoute = matchAppRoute(pathname);

  if (matchedRoute.kind === 'puzzle-playground') {
    return {
      kind: 'puzzle-playground',
      loadingEyebrow: '正在载入拼图',
      loadingText: '正在进入拼图关卡...',
      Component: PuzzlePlaygroundApp,
    };
  }

  if (matchedRoute.kind === 'big-fish-playground') {
    return {
      kind: 'big-fish-playground',
      loadingEyebrow: '正在载入大鱼',
      loadingText: '正在进入玩法...',
      Component: BigFishPlaygroundApp,
    };
  }

  if (matchedRoute.kind === 'match3d-playground') {
    return {
      kind: 'match3d-playground',
      loadingEyebrow: '正在载入抓大鹅',
      loadingText: '正在进入消除关卡...',
      Component: Match3DPlaygroundApp,
    };
  }

  if (matchedRoute.kind === 'bark-battle-playground') {
    return {
      kind: 'bark-battle-playground',
      loadingEyebrow: '正在载入汪汪声浪',
      loadingText: '正在进入竖屏声浪竞技场...',
      Component: BarkBattlePlaygroundApp,
    };
  }

  if (matchedRoute.kind === 'child-motion-demo') {
    return {
      kind: 'child-motion-demo',
      loadingEyebrow: '正在载入热身关',
      loadingText: '正在进入寓教于乐 Demo...',
      Component: ChildMotionDemoApp,
    };
  }

  if (matchedRoute.kind === 'baby-love-drawing') {
    return {
      kind: 'baby-love-drawing',
      loadingEyebrow: '正在载入宝贝爱画',
      loadingText: '正在进入画板...',
      Component: BabyLoveDrawingRuntimeApp,
    };
  }

  return {
    kind: 'game',
    loadingEyebrow: '正在载入游戏',
    loadingText: '正在加载内容',
    Component: GameApp,
  };
}
