import { dataLoader } from '@ice/runtime';
import * as app from '@/app';
import dataloaderConfig from './dataloader-config'
<% if (dataLoaderImport.imports) {-%><%-dataLoaderImport.imports%><% } -%>
<% const staticModuleNames = []; -%>
<% if (runtimeModules.length) { -%>
  <% runtimeModules.forEach((runtimeModule, index) => { -%>
    <% if (runtimeModule.staticRuntime) { -%>
      import module<%= index %> from '<%= runtimeModule.path %>';
      <% staticModuleNames.push('module' + index) -%>
    <% } -%>
  <% }) -%>
<% } -%>

const loaders = {
  ...dataloaderConfig,
  <% if (hasExportAppData) {-%>__app: app.dataLoader,<% } -%>
}

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

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

// Only init static runtime in data-loader. 
const staticRuntimeModules = [
<% staticModuleNames.forEach((moduleName, index) => { -%>
  <%= moduleName %>,
<% }) -%>
];

dataLoader.init(loaders, {
  fetcher: dataLoaderFetcher,
  decorator: dataLoaderDecorator,
  runtimeModules: staticRuntimeModules,
  appExport: app,
});
