<script type="text/javascript">
  stellar = {
    // 懒加载 css https://github.com/filamentgroup/loadCSS
    loadCSS: (href, before, media, attributes) => {
      var doc = window.document;
      var ss = doc.createElement("link");
      var ref;
      if (before) {
        ref = before;
      } else {
        var refs = (doc.body || doc.getElementsByTagName("head")[0]).childNodes;
        ref = refs[refs.length - 1];
      }
      var sheets = doc.styleSheets;
      if (attributes) {
        for (var attributeName in attributes) {
          if (attributes.hasOwnProperty(attributeName)) {
            ss.setAttribute(attributeName, attributes[attributeName]);
          }
        }
      }
      ss.rel = "stylesheet";
      ss.href = href;
      ss.media = "only x";
      function ready(cb) {
        if (doc.body) {
          return cb();
        }
        setTimeout(function () {
          ready(cb);
        });
      }
      ready(function () {
        ref.parentNode.insertBefore(ss, before ? ref : ref.nextSibling);
      });
      var onloadcssdefined = function (cb) {
        var resolvedHref = ss.href;
        var i = sheets.length;
        while (i--) {
          if (sheets[i].href === resolvedHref) {
            return cb();
          }
        }
        setTimeout(function () {
          onloadcssdefined(cb);
        });
      };
      function loadCB() {
        if (ss.addEventListener) {
          ss.removeEventListener("load", loadCB);
        }
        ss.media = media || "all";
      }
      if (ss.addEventListener) {
        ss.addEventListener("load", loadCB);
      }
      ss.onloadcssdefined = onloadcssdefined;
      onloadcssdefined(loadCB);
      return ss;
    },

    // 从 butterfly 和 volantis 获得灵感
    loadScript: (src, opt) => new Promise((resolve, reject) => {
      var script = document.createElement('script');
      script.src = src;
      if (opt) {
        for (let key of Object.keys(opt)) {
          script[key] = opt[key]
        }
      } else {
        // 默认异步，如果需要同步，第二个参数传入 {} 即可
        script.async = true
      }
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    }),

    // https://github.com/jerryc127/hexo-theme-butterfly
    jQuery: (fn) => {
      if (typeof jQuery === 'undefined') {
        stellar.loadScript(stellar.plugins.jQuery).then(fn)
      } else {
        fn()
      }
    }
  };
  stellar.github = 'https://github.com/xaoxuu/hexo-theme-stellar/tree/<%- theme.stellar.version %>';
  stellar.config = {
    date_suffix: {
      just: '<%- __('meta.date_suffix.just') %>',
      min: '<%- __('meta.date_suffix.min') %>',
      hour: '<%- __('meta.date_suffix.hour') %>',
      day: '<%- __('meta.date_suffix.day') %>',
      month: '<%- __('meta.date_suffix.month') %>',
    },
  };

  // required plugins (only load if needs)
  stellar.plugins = {
    jQuery: '<%- url_for(theme.plugins.jquery || "https://cdn1.tianli0.top/npm/jquery@latest/dist/jquery.min.js") %>',
    sitesjs: '<%- url_for(theme.plugins.sitesjs) %>',
    friendsjs: '<%- url_for(theme.plugins.friendsjs) %>',
  };

  // optional plugins
  if ('<%- theme.plugins.lazyload.enable %>' == 'true') {
    stellar.plugins.lazyload = Object.assign(<%- JSON.stringify(theme.plugins.lazyload) %>);
  }
  if ('<%- theme.plugins.swiper.enable %>' == 'true') {
    stellar.plugins.swiper = Object.assign(<%- JSON.stringify(theme.plugins.swiper) %>);
  }
  if ('<%- theme.plugins.scrollreveal.enable %>' == 'true') {
    stellar.plugins.scrollreveal = Object.assign(<%- JSON.stringify(theme.plugins.scrollreveal) %>);
  }
  if ('<%- theme.plugins.preload.enable %>' == 'true') {
    stellar.plugins.preload = Object.assign(<%- JSON.stringify(theme.plugins.preload) %>);
  }
  if ('<%- theme.plugins.fancybox.enable %>' == 'true') {
    stellar.plugins.fancybox = Object.assign(<%- JSON.stringify(theme.plugins.fancybox) %>);
  }
  if ('<%- theme.plugins.heti.enable %>' == 'true') {
    stellar.plugins.heti = Object.assign(<%- JSON.stringify(theme.plugins.heti) %>);
  }
</script>

<!-- required -->
<% if (theme.stellar.cdn_js) { %>
  <%- js({src: theme.stellar.cdn_js, async: true}) %>
<% } else { %>
  <%- js({src: '/js/main.js', async: true}) %>
<% } %>

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

<!-- inject -->
<% if (config.inject && config.inject.script && config.inject.script.length > 0) { %>
  <% config.inject.script.forEach(function(js) { %>
    <%- js %>
  <% }) %>
<% } %>
