<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="com.pandoli365.bibimbap.security.CsrfTokens" %>
<%@ page import="org.springframework.web.util.HtmlUtils" %>
<%
  String themeCsrfToken = CsrfTokens.getOrCreate(request.getSession());
%>
<meta name="csrf-token" content="<%= HtmlUtils.htmlEscape(themeCsrfToken) %>">
<script>
(function () {
  try {
    var k = 'bibimbap-theme';
    var t = localStorage.getItem(k);
    if (t !== 'light' && t !== 'dark') {
      t = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    }
    document.documentElement.setAttribute('data-theme', t);
  } catch (e) {}

  window.BibimbapCsrf = {
    token: function () {
      var meta = document.querySelector('meta[name="csrf-token"]');
      return meta ? meta.getAttribute('content') || '' : '';
    },
    headers: function (extra) {
      var headers = extra || {};
      var token = this.token();
      if (token) headers['X-CSRF-Token'] = token;
      return headers;
    }
  };
})();
</script>
