---
import { defaultLocale, themeConfig } from '@/config'
import { walineLocaleMap } from '@/i18n/config'

const {
  light: { primary: lightPrimary, secondary: lightSecondary, background: lightBackground },
  dark: { primary: darkPrimary, secondary: darkSecondary, background: darkBackground },
} = themeConfig.color

const {
  serverURL = '',
  emoji = [],
  search = false,
  imageUploader = false,
} = themeConfig.comment?.waline ?? {}

// Get the language code of Waline
function getWalineLang(currentPath: string, defaultLocale: string): string {
  // Extract language code from path
  const pathLang = Object.keys(walineLocaleMap).find(code =>
    currentPath.startsWith(`/${code}/`),
  )
  // Return found path language or default language
  const lang = pathLang || defaultLocale
  return walineLocaleMap[lang as keyof typeof walineLocaleMap]
}

// Get Waline language and generate configuration json
const walineLang = getWalineLang(Astro.url.pathname, defaultLocale)
const walineConfigJson = JSON.stringify({
  serverURL,
  lang: walineLang,
  emoji,
  search,
  imageUploader,
})
---

<div
  id="waline"
  class="mt-16"
  data-config={walineConfigJson}
>
</div>

<!-- Not use is:inline or define:vars >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> -->
<script>
import { init } from '@waline/client'
import '@waline/client/style'

function initWaline() {
  const walineEl = document.getElementById('waline')
  const walineConfig = JSON.parse(walineEl?.dataset.config || '{}')

  init({
    el: '#waline',
    path: window.location.pathname.replace(/^\/([a-z]{2}(-[a-z]{2})?)\//, '/'),
    dark: 'html.dark',
    requiredMeta: ['nick', 'mail'],
    highlighter: false,
    texRenderer: false,
    noCopyright: true,
    reaction: [],
    ...walineConfig,
  })
}

initWaline()
document.addEventListener('astro:after-swap', initWaline)
</script>

<!-- Custom CSS Styles >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> -->
<style is:global>
#waline .wl-login-info {
  --at-apply: 'mt-0 mr-3'
}

#waline .wl-login-nick:not(:has(img)) {
  --at-apply: 'leading-3.6 mt-1.4';
}

#waline .wl-panel {
  --at-apply: 'm-0 rounded-lg border-secondary/25'
}

#waline .wl-header {
  --at-apply: 'p-0';
}

#waline .wl-header-item {
  border-bottom: 1px solid var(--waline-border-color);
}

#waline .wl-header label {
  --at-apply: 'text-3';
}

#waline .wl-header input {
  --at-apply: 'text-2.8';
}

#waline .wl-card,
#waline .wl-header.item3 {
  --at-apply: 'border-b-0';
}

#waline .wl-editor {
  --at-apply: 'min-h-24';
}

#waline .wl-editor::placeholder {
  color: var(--waline-light-grey);
}

#waline .wl-footer {
  --at-apply: 'm-2';
}

#waline .wl-text-number,
#waline .wl-action[title="Markdown Guide"],
#waline .wl-sort,
#waline .wl-emoji-popup .wl-tab-wrapper::-webkit-scrollbar,
#waline .wl-gallery::-webkit-scrollbar {
  --at-apply: 'hidden';
}

#waline .wl-emoji-popup {
  --at-apply: 'start-0 border-secondary/25';
}

#waline .wl-emoji-popup .wl-tab-wrapper,
#waline .wl-gallery {
  --at-apply: 'scrollbar-hidden';
}

#waline .wl-gif-popup {
  --at-apply: 'border-secondary/25';
}

#waline .wl-gif-popup input {
  --at-apply: 'bg-background border-secondary/25';
}

#waline .wl-gif-popup input::placeholder {
  --at-apply: 'c-secondary/30 text-3.5';
}

#waline .wl-meta-head {
  --at-apply: 'pt-3 pb-2 px-0';
}

#waline .wl-card-item {
  --at-apply: 'px-0';
}

#waline .wl-user-avatar {
  --at-apply: 'mt-1';
}

#waline .wl-content p {
  --at-apply: 'leading-6 text-3.5';
}

#waline .wl-time {
  color: color-mix(in oklch, var(--waline-theme-color), transparent 20%);
}

#waline .wl-edit,
#waline .wl-delete {
  --at-apply: 'mr-0.4';
}

#waline .wl-like {
  --at-apply: 'mr-1.2';
}
</style>

<!-- Official CSS Variables >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> -->
<!-- https://waline.js.org/reference/client/style.html -->
<style
  define:vars={{
  lightPrimary,
  lightSecondary,
  lightBackground,
  darkPrimary,
  darkSecondary,
  darkBackground,
}}
>
#waline {
  /* Regular Colors */
  --waline-white: var(--lightBackground);
  --waline-light-grey: color-mix(in oklch, var(--lightPrimary), transparent 75%);
  --waline-dark-grey: var(--lightSecondary);

  /* Theme Colors */
  --waline-theme-color: var(--lightPrimary);
  --waline-active-color: var(--lightPrimary);

  /* Layout Colors */
  --waline-color: var(--lightSecondary);
  --waline-bg-color: var(--lightBackground);
  --waline-bg-color-light: var(--lightBackground);
  --waline-bg-color-hover: var(--lightBackground);
  --waline-border-color: color-mix(in oklch, var(--lightPrimary), transparent 75%);
  --waline-disable-bg-color: color-mix(in oklch, var(--lightSecondary), transparent 92%);
  --waline-disable-color: var(--lightPrimary);

  /* Special Colors */
  --waline-bq-color: color-mix(in oklch, var(--lightPrimary), transparent 75%);

  /* Information */
  --waline-info-bg-color: var(--lightBackground);
  --waline-info-color: color-mix(in oklch, var(--lightPrimary), transparent 75%);

  /* Rendering Options */
  --waline-avatar-radius: 20%;
}

html.dark #waline {
  /* Regular Colors */
  --waline-white: var(--darkBackground);
  --waline-light-grey: color-mix(in oklch, var(--darkPrimary), transparent 75%);
  --waline-dark-grey: var(--darkSecondary);

  /* Theme Colors */
  --waline-theme-color: var(--darkPrimary);
  --waline-active-color: var(--darkPrimary);

  /* Layout Colors */
  --waline-color: var(--darkSecondary);
  --waline-bg-color: var(--darkBackground);
  --waline-bg-color-light: var(--darkBackground);
  --waline-bg-color-hover: var(--darkBackground);
  --waline-border-color: color-mix(in oklch, var(--darkPrimary), transparent 75%);
  --waline-disable-bg-color: color-mix(in oklch, var(--darkSecondary), transparent 92%);
  --waline-disable-color: var(--darkPrimary);

  /* Special Colors */
  --waline-bq-color: color-mix(in oklch, var(--darkPrimary), transparent 75%);

  /* Information */
  --waline-info-bg-color: var(--darkBackground);
  --waline-info-color: color-mix(in oklch, var(--darkPrimary), transparent 75%);
}
</style>
