<script setup lang="ts">
import {computed, ref, watch} from 'vue';
import {IncremarkContent, ThemeProvider} from '@incremark/vue';
import '@incremark/theme/styles.css';

import {usePreferences} from '@easyflow-core/preferences';

interface Props {
  content?: string;
  streaming?: boolean;
}

const props = withDefaults(defineProps<Props>(), {
  content: '',
  streaming: false,
});
const { isDark } = usePreferences();
const markdownContent = computed(() => props.content || '');
const isFinished = computed(() => !props.streaming);
const incremarkOptions = {
  breaks: true,
  containers: false,
  gfm: true,
  htmlTree: false,
  math: true,
};
const previousContent = ref('');

watch(
  () => [props.content || '', props.streaming] as const,
  ([content, streaming]) => {
    const previous = previousContent.value;
    if (import.meta.env.DEV && streaming) {
      const startsWithPrevious = content.startsWith(previous);
      console.debug('[ChatTimeMarkdown] streaming update', {
        deltaLength: startsWithPrevious ? content.length - previous.length : null,
        length: content.length,
        previousLength: previous.length,
        preview: content.slice(-160).replaceAll('\n', '\\n'),
        startsWithPrevious,
      });
    }
    previousContent.value = content;
  },
  { immediate: true },
);
</script>

<template>
  <div class="chat-time-markdown">
    <ThemeProvider :theme="isDark ? 'dark' : 'default'">
      <IncremarkContent
        :content="markdownContent"
        :incremark-options="incremarkOptions"
        :is-finished="isFinished"
      />
    </ThemeProvider>
  </div>
</template>

<style scoped>
.chat-time-markdown {
  width: 100%;
  min-width: 0;
  color: hsl(var(--text-strong));
  font-size: 14px;
  line-height: 1.72;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.chat-time-markdown :deep(.incremark-theme-provider),
.chat-time-markdown :deep(.incremark) {
  width: 100%;
  max-width: 100%;
  color: inherit;
  font-size: inherit;
  line-height: inherit;
  background: transparent;
}

.chat-time-markdown :deep(.incremark) {
  overflow-wrap: anywhere;
}

.chat-time-markdown :deep(.incremark > .incremark-block:first-child > *) {
  margin-top: 0;
}

.chat-time-markdown :deep(.incremark > .incremark-block:last-child > *) {
  margin-bottom: 0;
}

.chat-time-markdown :deep(h1),
.chat-time-markdown :deep(h2),
.chat-time-markdown :deep(h3),
.chat-time-markdown :deep(h4),
.chat-time-markdown :deep(h5),
.chat-time-markdown :deep(h6) {
  margin: 1.35em 0 0.62em;
  color: hsl(var(--text-strong));
  font-weight: 650;
  line-height: 1.28;
  letter-spacing: 0;
}

.chat-time-markdown :deep(h1) {
  font-size: 1.55em;
}

.chat-time-markdown :deep(h2) {
  font-size: 1.34em;
}

.chat-time-markdown :deep(h3) {
  font-size: 1.18em;
}

.chat-time-markdown :deep(h4) {
  font-size: 1.06em;
}

.chat-time-markdown :deep(h5),
.chat-time-markdown :deep(h6) {
  font-size: 1em;
}

.chat-time-markdown :deep(p) {
  margin: 0.72em 0;
  font-size: inherit;
  line-height: inherit;
}

.chat-time-markdown :deep(strong) {
  color: hsl(var(--text-strong));
  font-weight: 650;
}

.chat-time-markdown :deep(em) {
  font-style: italic;
}

.chat-time-markdown :deep(del) {
  color: hsl(var(--text-muted));
}

.chat-time-markdown :deep(a) {
  color: hsl(var(--primary));
  text-decoration: underline;
  text-underline-offset: 3px;
  word-break: break-word;
}

.chat-time-markdown :deep(a:hover) {
  color: hsl(var(--primary) / 0.82);
}

.chat-time-markdown :deep(hr) {
  height: 1px;
  margin: 1.25em 0;
  background: hsl(var(--divider-faint) / 0.84);
  border: 0;
}

.chat-time-markdown :deep(blockquote) {
  margin: 0.95em 0;
  padding: 8px 12px;
  color: hsl(var(--text-muted));
  background: hsl(var(--surface-subtle) / 0.72);
  border-left: 3px solid hsl(var(--primary) / 0.32);
  border-radius: 0 10px 10px 0;
}

.chat-time-markdown :deep(blockquote > :first-child) {
  margin-top: 0;
}

.chat-time-markdown :deep(blockquote > :last-child) {
  margin-bottom: 0;
}

.chat-time-markdown :deep(ul),
.chat-time-markdown :deep(ol) {
  margin: 0.72em 0;
  padding-left: 1.45em;
}

.chat-time-markdown :deep(ul) {
  list-style: disc;
}

.chat-time-markdown :deep(ol) {
  list-style: decimal;
}

.chat-time-markdown :deep(ul ul),
.chat-time-markdown :deep(ol ul) {
  list-style: circle;
}

.chat-time-markdown :deep(ul ul ul),
.chat-time-markdown :deep(ol ul ul) {
  list-style: square;
}

.chat-time-markdown :deep(li) {
  margin: 0.28em 0;
  padding-left: 0.12em;
}

.chat-time-markdown :deep(li > p) {
  margin: 0.36em 0;
}

.chat-time-markdown :deep(li.task-list-item) {
  list-style: none;
}

.chat-time-markdown :deep(.task-list-item-checkbox) {
  width: 14px;
  height: 14px;
  margin: 0 0.48em 0 -1.45em;
  vertical-align: -0.12em;
  accent-color: hsl(var(--primary));
}

.chat-time-markdown :deep(table) {
  display: block;
  width: max-content;
  max-width: 100%;
  margin: 1em 0;
  overflow: auto;
  border-spacing: 0;
  border-collapse: collapse;
}

.chat-time-markdown :deep(th),
.chat-time-markdown :deep(td) {
  min-width: 96px;
  padding: 8px 10px;
  vertical-align: top;
  border: 1px solid hsl(var(--divider-faint) / 0.88);
}

.chat-time-markdown :deep(th) {
  color: hsl(var(--text-strong));
  font-weight: 650;
  background: hsl(var(--surface-subtle));
}

.chat-time-markdown :deep(tr:nth-child(2n) td) {
  background: hsl(var(--surface-subtle) / 0.42);
}

.chat-time-markdown :deep(code) {
  padding: 0.12em 0.34em;
  color: hsl(var(--primary));
  font-size: 0.92em;
  font-family:
    ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono',
    'Courier New', monospace;
  background: hsl(var(--surface-subtle));
  border: 1px solid hsl(var(--divider-faint) / 0.7);
  border-radius: 6px;
}

.chat-time-markdown :deep(pre) {
  max-width: 100%;
  margin: 1em 0;
  padding: 12px 14px;
  overflow: auto;
  color: hsl(var(--text-strong));
  background: hsl(var(--surface-subtle));
  border: 1px solid hsl(var(--divider-faint) / 0.82);
  border-radius: 12px;
}

.chat-time-markdown :deep(pre code) {
  display: block;
  min-width: max-content;
  padding: 0;
  color: inherit;
  font-size: 0.92em;
  line-height: 1.68;
  background: transparent;
  border: 0;
  border-radius: 0;
  overflow-wrap: normal;
  word-break: normal;
}

.chat-time-markdown :deep(.incremark-code) {
  max-width: 100%;
  margin: 1em 0;
  overflow: hidden;
  color: hsl(var(--text-strong));
  background: hsl(var(--surface-subtle));
  border: 1px solid hsl(var(--divider-faint) / 0.82);
  border-radius: 12px;
}

.chat-time-markdown :deep(.incremark-code .code-header) {
  padding: 8px 12px;
  color: hsl(var(--text-muted));
  background: hsl(var(--surface-subtle) / 0.72);
  border-bottom: 1px solid hsl(var(--divider-faint) / 0.72);
}

.chat-time-markdown :deep(.incremark-code .code-content) {
  overflow: auto;
}

.chat-time-markdown :deep(.incremark-code pre) {
  margin: 0;
  background: transparent;
  border: 0;
  border-radius: 0;
}

.chat-time-markdown :deep(.shiki),
.chat-time-markdown :deep(.shiki code) {
  background: transparent !important;
}

.chat-time-markdown :deep(img) {
  display: block;
  max-width: min(100%, 460px);
  height: auto;
  margin: 1em 0;
  border: 1px solid hsl(var(--divider-faint) / 0.82);
  border-radius: 12px;
}

.chat-time-markdown :deep(.katex-display) {
  max-width: 100%;
  margin: 1em 0;
  overflow-x: auto;
  overflow-y: hidden;
}

.chat-time-markdown :deep(.katex) {
  font-size: 1em;
}

.chat-time-markdown :deep(.mermaid),
.chat-time-markdown :deep([class*='mermaid']) {
  max-width: 100%;
  margin: 1em 0;
  overflow: auto;
}

.chat-time-markdown :deep(svg) {
  max-width: 100%;
  height: auto;
}

:global(.dark) .chat-time-markdown :deep(blockquote) {
  background: hsl(var(--surface-subtle) / 0.5);
}

:global(.dark) .chat-time-markdown :deep(code),
:global(.dark) .chat-time-markdown :deep(pre),
:global(.dark) .chat-time-markdown :deep(.incremark-code) {
  background: hsl(var(--surface-subtle) / 0.78);
}
</style>
