<% if (importCoreJs) { -%>import 'core-js';<% } %>
<%- entry.imports %>
import { createElement, Fragment } from 'react';
import { runClientApp, getAppConfig } from '<%- iceRuntimePath %>';
import { commons, statics } from './runtime-modules';
import * as app from '@/app';
<% if (enableRoutes) { -%>
import createRoutes from './routes';
<% } -%>
<%- runtimeOptions.imports %>
<% if (dataLoaderImport.imports && hasDataLoader) {-%><%-dataLoaderImport.imports%><% } -%>
import type { RunClientAppOptions } from '@ice/runtime';

const getRouterBasename = () => {
  const appConfig = getAppConfig(app);
  return appConfig?.router?.basename ?? <%- basename %> ?? '';
}
// Add react fragment for split chunks of app.
// Otherwise chunk of route component will pack @ice/jsx-runtime and depend on framework bundle.
const App = <></>;

<% if (!dataLoaderImport.imports && hasDataLoader) {-%>
let dataLoaderFetcher = (options) => {
  return window.fetch(options.url, options);
}

let dataLoaderDecorator = (dataLoader) => {
  return dataLoader;
}
<% } -%>

const renderOptions: RunClientAppOptions = {
  app,
  runtimeModules: {
    commons,
    statics,
  },
  <% if (enableRoutes) { %>createRoutes,<% } %>
  basename: getRouterBasename(),
  hydrate: <%- hydrate %>,
  memoryRouter: <%- memoryRouter || false %>,
<% if (hasDataLoader) { -%>
  dataLoaderFetcher,
  dataLoaderDecorator,<% } -%>
  runtimeOptions: {
<% if (runtimeOptions.exports) { -%>
  <%- runtimeOptions.exports %>
<% } -%>
<% if (locals.customRuntimeOptions) { -%>...<%- JSON.stringify(customRuntimeOptions) %>,<% } -%>
  },
};

const defaultRender = (customOptions: Partial<RunClientAppOptions> = {}) => {
  return runClientApp({
    ...renderOptions,
    ...customOptions,
    runtimeOptions: {
      ...(renderOptions.runtimeOptions || {}),
      ...customOptions.runtimeOptions,
    },
  });
};

const renderApp = (appExport: any, customOptions: Partial<RunClientAppOptions>) => {
  if (appExport.runApp) {
    return appExport.runApp(defaultRender, renderOptions);
  } else {
    return defaultRender(customOptions);
  }
};

const render = (customOptions: Partial<RunClientAppOptions> = {}) => {
  return renderApp(app, customOptions);
};

<%- entryCode %>

if (import.meta.webpackHot) {
  import.meta.webpackHot.accept();
}
