<%

function custom_inject() {
  var el = '';
  for (let item of (config.inject?.script || [])) {
    el += item;
  }
  for (let item of (theme.inject?.script || [])) {
    el += item;
  }
  for (let item of (page.inject?.script || [])) {
    el += item;
  }
  return el;
}

// tagtree 脚本仅在与 tagtree 小部件渲染相同的条件下加载（见 _partial/sidebar/index_leftbar.ejs）
function tagtreeWidgetOnPage() {
  const leftbar = page && page.leftbar;
  if (!Array.isArray(leftbar)) {
    return false;
  }
  return leftbar.some(function (w) {
    if (typeof w === 'string') {
      return w === 'tagtree';
    }
    if (w && typeof w === 'object') {
      return w.override === 'tagtree' || w.layout === 'tagtree';
    }
    return false;
  });
}

%>
<%- partial('scripts/defines') %>

<%- partial('scripts/bootstrap') %>

<!-- core: utils 必须同步加载（页尾内联插件片段在解析期即调用 utils.initPlugin） -->
<script src="<%- url_for('/js/utils.js') %>?v=<%- stellar_info('version') %>"></script>
<script>
  // 解析期检测 utils 是否已就绪；未就绪时同步补载，
  // 保证「utils 先于页尾插件片段定义」的不变量。
  if (typeof utils === 'undefined') {
    document.write('<script src="<%- url_for('/js/utils.js') %>?v=<%- stellar_info('version') %>"><\/script>');
  }
</script>
<script defer src="<%- url_for('/js/stellar-icons.js') %>?v=<%- stellar_info('version') %>"></script>
<!-- 非首屏图标异步加载：占位符替换依赖构建期生成的 js/icons/{ns}.json -->
<script defer src="<%- url_for('/js/icons.js') %>?v=<%- stellar_info('version') %>" data-version="<%- stellar_info('version') %>"></script>
<script defer src="<%- url_for('/js/plugins/dropdown.js') %>?v=<%- stellar_info('version') %>"></script>
<script src="<%- url_for(`${theme.stellar.main_js}?v=${stellar_info('version')}`) %>" defer></script>
<script defer src="<%- url_for('/js/theme.js') %>?v=<%- stellar_info('version') %>"></script>

<% if (tagtreeWidgetOnPage()) { %>
  <script>window.__STELLAR_TAGTREE__ = { expand_active: <%- JSON.stringify(!!(theme.widgets.tagtree && theme.widgets.tagtree.expand_active)) %> };</script>
  <script defer src="<%- url_for('/js/tagtree.js') %>?v=<%- stellar_info('version') %>"></script>
<% } %>

<%- partial('scripts/lazyload') %>

<!-- optional -->
<%- partial('comments/script') %>

<script defer src="<%- url_for('/js/services.js') %>?v=<%- stellar_info('version') %>"></script>
<%- partial('../_plugins/index') %>

<!-- inject -->
<%- custom_inject() %>
