<%
// setup page object for current route
const { path, resource, collection } = _.route
let pathname

switch (path) {
  case '/':
  case '/index':
    pathname = '/'
    _.pageName = 'home'
    break
  default:
    pathname = path
    if (resource) {
      _.pageName = resource
    } else {
      _.pageName = path.slice(1)
    }
}

_.state = _.cms(_.pageName) || {}
if (resource || collection) {
  // merge resource body or folder collection content
  const { body, content } = await _.resolveRoute()
  _.state = Object.assign({}, body || content, _.state)
}

// preset global list of active widgets
_.widgets = []
const widgets = _.cms('widgets')
if (Array.isArray(widgets)) {
  _.widgets = widgets.map(slug => _.cms(`widgets/${slug}`))
    .filter(({ pkg, active }) => Boolean(pkg && active))
}

// page title and description
const title = _.state.meta_title || _.state.name || _.state.title || _.settings.name
const description = _.state.meta_description || _.state.short_description || _.settings.description

let url, domain, social, ogLocale, ogImage
if (pathname) {
  if (!_.settings.domain && _.store.domain) {
    _.settings.domain = _.store.domain
  }
  const { domain } = _.settings
  if (domain) {
    // canonical url
    url = `https://${domain}${pathname}`
    // content for og metatags
    social = _.cms('social')
    ogLocale = _.lang.length === 2 ? _.lang : _.lang.substr(0, 3) + _.lang.slice(3).toUpperCase()
    if (resource) {
      const picture = _.ecomUtils.img(_.state, null, 'zoom')
      ogImage = picture && picture.url
    }
    if (!ogImage) {
      if (social.og_image) {
        ogImage = social.og_image.charAt(0) === '/' ? `https://${domain}${social.og_image}` : social.og_image
      }
    } else {
      ogImage = ogImage.replace(/(\w+\.)?(ecoms\d)\.com/i, '$2-nyc3.nyc3.cdn.digitaloceanspaces.com')
    }
  }
}
%>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, minimum-scale=1">
<link rel="manifest" href="/manifest.json" crossOrigin="use-credentials">
<meta name="theme-color" content="<%= _.primaryColor %>">
<link rel="icon" href="<%= _.settings.favicon %>">
<title><%= title %></title>
<meta name="description" content="<%= description %>">
<meta name="author" content="<%= _.settings.name %>">
<meta name="generator" content="E-Com Plus Storefront">
<% if (url) { %>
  <link rel="canonical" href="<%= url %>">
<% } %>

<% if (!path.startsWith('/admin/')) { %>
  <link rel="preload" href="<%= _.assetsPrefix || '' %>/storefront.css" as="style">
  <% if (!path.startsWith('/app/')) { %>
    <link rel="preload" href="<%= _.assetsPrefix || '' %>/storefront.js" as="script">
  <% } %>

  <%
  // setup preloaded widget partials (pixels, tags)
  if (_.widgets.length) {
    %>
    <script>
      partytown = {
        resolveUrl: function (url, location) {
          switch (url.hostname) {
            case 'www.googletagmanager.com':
            case 'www.google-analytics.com':
            case 'analytics.google.com':
            case 'api.hubspot.com':
            case 'api.hubapi.com':
            case 'trc.taboola.com':
            case location.hostname:
              return url;
            default:
              const proxyUrl = new URL(`https://${location.hostname}/~reverse-proxy/`);
              proxyUrl.searchParams.append('url', url.href);
              return proxyUrl;
          };
        },
        forward: [],
      };
    </script>
    <%- await include('@/helpers/widgets-append', {
      _, opt: {}, field: 'headAppend'
    }) %>
    <% if (
      Array.isArray(_.widgets) &&
      _.widgets.find(({ usePartytown, options }) => {
        return (usePartytown || (options && options.partytown))
      })
    ) { %>
      <script>
        <%- _.partytownSnippetText %>
      </script>
    <% } %>
    <%
  }
  %>

  <% if (_.assetsPrefix) { %>
    <link rel="preconnect" href="<%= _.assetsPrefix || '' %>/" crossorigin>
    <link rel="dns-prefetch" href="<%= _.assetsPrefix || '' %>/">
  <% } %>
  <link rel="preconnect" href="https://cdn.jsdelivr.net/" crossorigin>
  <link rel="dns-prefetch" href="https://cdn.jsdelivr.net/">
  <% if (resource) { %>
    <link rel="preconnect" href="https://ioapi.ecvol.com/" crossorigin>
    <link rel="dns-prefetch" href="https://ioapi.ecvol.com/">
  <% } %>
  <link rel="preconnect" href="https://apx-search.e-com.plus/" crossorigin>
  <link rel="dns-prefetch" href="https://apx-search.e-com.plus/">
<% } %>

<link rel="apple-touch-icon" href="<%= _.settings.icon %>">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="default">

<% if (social) { %>
  <meta property="og:site_name" content="<%= _.settings.name %>">
  <meta property="og:url" content="<%= url %>">
  <meta property="og:title" content="<%= title %>">
  <meta property="og:description" content="<%= description %>">
  <meta property="og:type" content="website">
  <meta property="og:locale" content="<%= ogLocale %>">
  <% if (ogImage) { %>
    <meta property="og:image" content="<%= ogImage %>">
  <% } %>
  <% if (social.fb_app_id) { %>
    <meta property="fb:app_id" content="<%= social.fb_app_id %>">
  <% } %>

  <meta name="twitter:card" content="summary">
  <% if (social.twitter_username) { %>
    <meta name="twitter:site" content="<%= social.twitter_username %>">
  <% } %>
<% } %>

<meta name="ecom-store-id" content="<%= _.storeId %>">
<meta name="views-v" content="1.3.0">
