<script>
  import { marked } from 'marked';
  import DOMPurify from 'dompurify';

  function scroll(message) {
    message.scrollIntoView();
  }

  let { at, body } = $props();
  let renderedBody = $derived(DOMPurify.sanitize(marked.parse(body, { breaks: true })));
</script>

<div class="message relative">
  <span class="timestamp chip variant-soft absolute top-0 right-0">{at}</span>
  <section use:scroll class="py-1 message-body">
    <!-- eslint-disable-next-line svelte/no-at-html-tags -->
    {@html renderedBody}
  </section>
</div>

<style>
  .message .timestamp {
    display: none;
  }
  .message:hover .timestamp {
    display: flex;
  }
  .message-body {
    overflow: auto;
    max-width: 80vw;
    @media (width > 640px) {
      /* 21rem is width of the nav bar in full-screen mode. */
      max-width: calc(90vw - 21rem);
    }
  }
  .message-body:empty:after {
    content: '.';
    visibility: hidden;
  }
  /* Without the global selector, the Svelte CSS compiler will see this as
   * unused, and purge it from the output. However, this is "plausibly used"
   * because it may occur in renderedBody.
   */
  :global(.message-body blockquote) {
    border-left: 0.25rem solid lightgrey;
    margin-left: 0.5rem;
    padding-left: 0.5rem;
  }
</style>
