// app-view-dispatch.jsx - central view dispatch boundary.
// Receives already-wired page nodes from app.jsx and chooses the active surface.

function AppViewDispatch({
  view,
  routes,
  settingsProps,
  forceProps,
  calendarProps,
  fieldTestProps,
  reviewDatabaseProps,
  trainingProps,
  injuryProps,
  normsProps,
  knowledgeProps,
  teamProps,
  reportProps,
  individualProps,
}) {
  if (view === 'settings') {
    const settingsNode = <AppSettingsRenderer settingsProps={settingsProps} />;
    const meta = { section: 'System', routeLabel: 'Settings', breadcrumb: 'Workspace / System / Settings' };
    if (
      typeof isBlueprintRouteWrapperEnabled === 'function' &&
      isBlueprintRouteWrapperEnabled(view) &&
      typeof BlueprintRouteWrapper === 'function'
    ) {
      return (
        <BlueprintRouteWrapper section={meta.section} routeLabel={meta.routeLabel} breadcrumb={meta.breadcrumb}>
          {settingsNode}
        </BlueprintRouteWrapper>
      );
    }
    return settingsNode;
  }
  if (routes.isForceView(view)) {
    const forceNode = <AppForceRenderer {...forceProps} />;
    const blueprintRouteMeta = {
      cmj: { section: 'Lab', routeLabel: 'CMJ', breadcrumb: 'Workspace / Lab / CMJ' },
      sj: { section: 'Lab', routeLabel: 'SJ', breadcrumb: 'Workspace / Lab / SJ' },
      imtp: { section: 'Lab', routeLabel: 'IMTP', breadcrumb: 'Workspace / Lab / IMTP' },
    };
    const meta = blueprintRouteMeta[view];
    if (
      meta &&
      typeof isBlueprintRouteWrapperEnabled === 'function' &&
      isBlueprintRouteWrapperEnabled(view) &&
      typeof BlueprintRouteWrapper === 'function'
    ) {
      return (
        <BlueprintRouteWrapper section={meta.section} routeLabel={meta.routeLabel} breadcrumb={meta.breadcrumb}>
          {forceNode}
        </BlueprintRouteWrapper>
      );
    }
    return forceNode;
  }
  if (view === 'calendar') {
    const calendarNode = <AppCalendarRenderer {...calendarProps} />;
    const meta = { section: 'Workspace', routeLabel: 'Calendar', breadcrumb: 'Workspace / Calendar' };
    if (
      typeof isBlueprintRouteWrapperEnabled === 'function' &&
      isBlueprintRouteWrapperEnabled(view) &&
      typeof BlueprintRouteWrapper === 'function'
    ) {
      return (
        <BlueprintRouteWrapper section={meta.section} routeLabel={meta.routeLabel} breadcrumb={meta.breadcrumb}>
          {calendarNode}
        </BlueprintRouteWrapper>
      );
    }
    return calendarNode;
  }
  if (view === 'field-test') {
    const fieldTestNode = <AppFieldTestRenderer {...fieldTestProps} />;
    const meta = { section: 'Lab', routeLabel: 'Field Test', breadcrumb: 'Workspace / Lab / Field Test' };
    if (
      typeof isBlueprintRouteWrapperEnabled === 'function' &&
      isBlueprintRouteWrapperEnabled(view) &&
      typeof BlueprintRouteWrapper === 'function'
    ) {
      return (
        <BlueprintRouteWrapper section={meta.section} routeLabel={meta.routeLabel} breadcrumb={meta.breadcrumb}>
          {fieldTestNode}
        </BlueprintRouteWrapper>
      );
    }
    return fieldTestNode;
  }
  if (view === 'review-database') {
    return <AppReviewDatabaseRenderer {...reviewDatabaseProps} />;
  }
  if (view === 'training') {
    const trainingNode = <AppTrainingRenderer {...trainingProps} />;
    const meta = { section: 'Athletes', routeLabel: 'Training', breadcrumb: 'Workspace / Athletes / Training' };
    if (
      typeof isBlueprintRouteWrapperEnabled === 'function' &&
      isBlueprintRouteWrapperEnabled(view) &&
      typeof BlueprintRouteWrapper === 'function'
    ) {
      return (
        <BlueprintRouteWrapper section={meta.section} routeLabel={meta.routeLabel} breadcrumb={meta.breadcrumb}>
          {trainingNode}
        </BlueprintRouteWrapper>
      );
    }
    return trainingNode;
  }
  if (routes.isInjuryView(view)) {
    const adaptationNode = <AppInjuryRenderer {...injuryProps} />;
    const meta = { section: 'Athletes', routeLabel: 'Adaptation', breadcrumb: 'Workspace / Athletes / Adaptation' };
    if (
      typeof isBlueprintRouteWrapperEnabled === 'function' &&
      isBlueprintRouteWrapperEnabled(view) &&
      typeof BlueprintRouteWrapper === 'function'
    ) {
      return (
        <BlueprintRouteWrapper section={meta.section} routeLabel={meta.routeLabel} breadcrumb={meta.breadcrumb}>
          {adaptationNode}
        </BlueprintRouteWrapper>
      );
    }
    return adaptationNode;
  }
  if (view === 'norms') {
    const normsNode = <AppNormsRenderer {...normsProps} />;
    const meta = { section: 'Knowledge', routeLabel: 'Norms', breadcrumb: 'Workspace / Knowledge / Norms' };
    if (
      typeof isBlueprintRouteWrapperEnabled === 'function' &&
      isBlueprintRouteWrapperEnabled(view) &&
      typeof BlueprintRouteWrapper === 'function'
    ) {
      return (
        <BlueprintRouteWrapper section={meta.section} routeLabel={meta.routeLabel} breadcrumb={meta.breadcrumb}>
          {normsNode}
        </BlueprintRouteWrapper>
      );
    }
    return normsNode;
  }
  if (view === 'knowledge') {
    const knowledgeNode = <AppKnowledgeRenderer {...knowledgeProps} />;
    const meta = { section: 'Knowledge', routeLabel: 'Library', breadcrumb: 'Workspace / Knowledge / Library' };
    if (
      typeof isBlueprintRouteWrapperEnabled === 'function' &&
      isBlueprintRouteWrapperEnabled(view) &&
      typeof BlueprintRouteWrapper === 'function'
    ) {
      return (
        <BlueprintRouteWrapper section={meta.section} routeLabel={meta.routeLabel} breadcrumb={meta.breadcrumb}>
          {knowledgeNode}
        </BlueprintRouteWrapper>
      );
    }
    return knowledgeNode;
  }
  if (view === 'team') {
    const teamNode = <AppTeamRenderer {...teamProps} />;
    const meta = { section: 'Teams', routeLabel: 'Team', breadcrumb: 'Workspace / Teams / Team' };
    if (
      typeof isBlueprintRouteWrapperEnabled === 'function' &&
      isBlueprintRouteWrapperEnabled(view) &&
      typeof BlueprintRouteWrapper === 'function'
    ) {
      return (
        <BlueprintRouteWrapper section={meta.section} routeLabel={meta.routeLabel} breadcrumb={meta.breadcrumb}>
          {teamNode}
        </BlueprintRouteWrapper>
      );
    }
    return teamNode;
  }
  if (view === 'report') {
    const reportNode = <AppReportRenderer {...reportProps} />;
    const meta = { section: 'Athletes', routeLabel: 'Report', breadcrumb: 'Workspace / Athletes / Report' };
    if (
      typeof isBlueprintRouteWrapperEnabled === 'function' &&
      isBlueprintRouteWrapperEnabled(view) &&
      typeof BlueprintRouteWrapper === 'function'
    ) {
      return (
        <BlueprintRouteWrapper section={meta.section} routeLabel={meta.routeLabel} breadcrumb={meta.breadcrumb}>
          {reportNode}
        </BlueprintRouteWrapper>
      );
    }
    return reportNode;
  }
  const individualNode = <AppIndividualRenderer {...individualProps} />;
  if (view === 'individual') {
    const meta = { section: 'Athletes', routeLabel: 'Story', breadcrumb: 'Workspace / Athletes / Story' };
    if (
      typeof isBlueprintRouteWrapperEnabled === 'function' &&
      isBlueprintRouteWrapperEnabled(view) &&
      typeof BlueprintRouteWrapper === 'function'
    ) {
      return (
        <BlueprintRouteWrapper section={meta.section} routeLabel={meta.routeLabel} breadcrumb={meta.breadcrumb}>
          {individualNode}
        </BlueprintRouteWrapper>
      );
    }
  }
  return individualNode;
}
