<!DOCTYPE html>
<html lang="en" class="govuk-template">
  <head>
    <title><%= browser_title(yield(:title), @pagy, @admin_user, @user, @organisation, @log, @resource) %></title>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>
    <%= tag.meta name: "viewport", content: "width=device-width, initial-scale=1" %>
    <%= tag.meta property: "og:image", content: asset_path("images/govuk-opengraph-image.png") %>
    <%= tag.meta name: "theme-color", content: "#0b0c0c" %>
    <%= favicon_link_tag asset_path("images/favicon.ico") %>
    <%= favicon_link_tag asset_path("images/govuk-mask-icon.svg"), rel: "mask-icon", type: "image/svg", color: "#0b0c0c" %>
    <%= favicon_link_tag asset_path("images/govuk-apple-touch-icon.png"), rel: "apple-touch-icon", type: "image/png" %>
    <%= favicon_link_tag asset_path("images/govuk-apple-touch-icon-152x152.png"), rel: "apple-touch-icon", type: "image/png", size: "152x152" %>
    <%= favicon_link_tag asset_path("images/govuk-apple-touch-icon-167x167.png"), rel: "apple-touch-icon", type: "image/png", size: "167x167" %>
    <%= favicon_link_tag asset_path("images/govuk-apple-touch-icon-180x180.png"), rel: "apple-touch-icon", type: "image/png", size: "180x180" %>
    <%= stylesheet_link_tag "application" %>
    <%= javascript_include_tag "vendor/html5shiv.min.js" %>
    <script>
      window.html5.elements = "output";
      html5.shivDocument(document);
    </script>
    <%= javascript_include_tag "vendor/polyfill-output-value.js" %>
    <%= javascript_include_tag "vendor/outerHTML.js" %>

    <% gtm_container = get_gtm_container %>
    <% gtm_id = get_gtm_id %>

    <% if cookies[:accept_analytics_cookies] == "on" %>
      <!-- Google Tag Manager (doesn't store personal info until permission given) -->
      <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
      new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
      j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
      'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
      })(window,document,'script','dataLayer','<%= gtm_container %>');</script>
      <!-- End Google Tag Manager -->

      <script async src="https://www.googletagmanager.com/gtag/js?id=<%= gtm_id %>"></script>
    <% else %>
      <script>
        // For adding the script once the user has given permission
        window.analyticsScript = "https://www.googletagmanager.com/gtag/js?id=<%= gtm_id %>";
      </script>
    <% end %>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', '<%= gtm_id %>');
    </script>

    <%= javascript_include_tag "application", defer: true %>

    <% if content_for?(:head) %>
      <%= yield(:head) %>
    <% end %>
    <%= capybara_lockstep if defined?(Capybara::Lockstep) %>

    <% if Rails.env.development? %>
      <script>
        console.log(<%= session.to_json.html_safe %>)
      </script>
    <% end %>

    <% if Rails.env.production? && ENV["APP_HOST"].present? %>
      <script defer data-domain="<%= ENV["APP_HOST"].split("https://")[1] %>" src="https://plausible.io/js/plausible.js"></script>
    <% end %>
  </head>

  <body class="govuk-template__body app-template--wide">
    <script>
      document.body.className = ((document.body.className) ? document.body.className + " js-enabled" : "js-enabled");
    </script>

    <% if cookies[:accept_analytics_cookies] == "on" %>
      <!-- Google Tag Manager (noscript) -->
      <noscript>
        <iframe src="https://www.googletagmanager.com/ns.html?id=<% gtm_container %>" height="0" width="0" style="display:none;visibility:hidden"></iframe>
      </noscript>
    <% end %>

    <% unless cookies[:accept_analytics_cookies] || current_page?(cookies_path) %>
      <%= render "cookies/banner" %>
    <% end %>

    <%= govuk_skip_link %>

    <%= govuk_header(
      classes: govuk_header_classes(current_user),
      service_url: current_user.nil? ? "/" : "/logs",
      navigation_classes: "govuk-header__navigation--end",
    ) do |component|
      component.product_name(name: t("service_name"))
      if current_user.nil?
        component.navigation_item(text: "Sign in", href: user_session_path)
      else
        component.navigation_item(text: "Your account", href: account_path)
        component.navigation_item(text: "Sign out", href: destroy_user_session_path)
      end
    end %>

    <% feedback_link = govuk_link_to "giving us your feedback (opens in a new tab)", t("feedback_form"), rel: "noreferrer noopener", target: "_blank" %>

    <%= govuk_phase_banner(
      classes: "govuk-width-container",
      tag: govuk_phase_banner_tag(current_user),
      text: "This is a new service – help us improve it by #{feedback_link}".html_safe,
    ) %>

    <% if !current_user.nil? %>
      <%= render PrimaryNavigationComponent.new(
        items: primary_items(request.path, current_user),
      ) %>
    <% end %>

    <div class="govuk-width-container">
      <%= content_for(:breadcrumbs) %>
      <%= content_for(:before_content) %>

      <main class="govuk-main-wrapper govuk-main-wrapper--auto-spacing" id="main-content" role="main">
        <% if flash.notice && !flash.notice.include?("translation missing") %>
          <%= govuk_notification_banner(
            title_text: "Success",
            success: true, title_heading_level: 3,
            title_id: "swanky-notifications"
          ) do |notification_banner|
            notification_banner.heading(text: flash.notice.html_safe)
          end %>
        <% end %>
        <%= content_for?(:content) ? yield(:content) : yield %>
      </main>
    </div>
    <%= render partial: "layouts/feedback" %>
    <%= render partial: "layouts/footer" %>
  </body>
</html>