<div
  class="status-content {{isStatusInOwnThread ? 'status-in-own-thread' : ''}} {{isStatusInNotification ? 'status-in-notification' : ''}}"
  ref:node
>
    {{{massagedContent}}}
</div>
<style>
  .status-content {
    margin: 10px 10px 10px 5px;
    grid-area: content;
    word-wrap: break-word;
    overflow: hidden;
    white-space: pre-wrap;
    font-size: 0.9em;
  }

  .status-content.status-in-own-thread {
    font-size: 1.3em;
    margin: 20px 10px 20px 5px;
  }

  :global(.status-content .status-emoji) {
    width: 20px;
    height: 20px;
    margin: -3px 0;
  }

  :global(.status-content p) {
    margin: 0 0 20px;
  }

  :global(.status-content p:first-child) {
    margin: 0 0 20px;
  }

  :global(.status-content p:last-child) {
    margin: 0;
  }

  .status-content.status-in-notification {
    color: var(--very-deemphasized-text-color);
  }
  :global(.status-content.status-in-notification a, .status-content.status-in-notification a:hover) {
    color: var(--very-deemphasized-link-color);
  }

  :global(.status-content .invisible) {
    /* copied from Mastodon */
    font-size: 0;
    line-height: 0;
    display: inline-block;
    width: 0;
    height: 0;
    position: absolute;
  }

</style>
<script>
  import { replaceAll } from '../../_utils/strings'
  import { mark, stop } from '../../_utils/marks'
  import { store } from '../../_store/store'

  export default {
    oncreate() {
      this.hydrateContent()
    },
    store: () => store,
    computed: {
      massagedContent: (originalStatus, $autoplayGifs) => {
        let content = originalStatus.content
        // emojify
        if (originalStatus.emojis && originalStatus.emojis.length) {
          for (let emoji of originalStatus.emojis) {
            let { shortcode, url, static_url } = emoji
            let urlToUse = $autoplayGifs ? url : static_url
            let shortcodeWithColons = `:${shortcode}:`
            content = replaceAll(
              content,
              shortcodeWithColons,
              `<img class="status-emoji" draggable="false" src="${urlToUse}"
                    alt="${shortcodeWithColons}" title="${shortcodeWithColons}" />`
            )
          }
        }
        // GNU Social and Pleroma don't add <p> tags
        if (!content.startsWith('<p>')) {
          content = `<p>${content}</p>`
        }
        return content
      }
    },
    methods: {
      hydrateContent() {
        if (!this.refs.node) {
          return
        }
        let node = this.refs.node
        let originalStatus = this.get('originalStatus')
        let uuid = this.get('uuid')
        let count = 0
        mark('hydrateContent')
        if (originalStatus.tags && originalStatus.tags.length) {
          let anchorTags = node.querySelectorAll('a[class~=hashtag][href^=http]')
          for (let tag of originalStatus.tags) {
            for (let anchorTag of anchorTags) {
              if (anchorTag.getAttribute('href').endsWith(`/tags/${tag.name}`)) {
                anchorTag.setAttribute('href', `/tags/${tag.name}`)
                anchorTag.setAttribute('focus-key', `status-content-link-${uuid}-${++count}`)
                anchorTag.removeAttribute('target')
                anchorTag.removeAttribute('rel')
              }
            }
          }
        }
        if (originalStatus.mentions && originalStatus.mentions.length) {
          let anchorTags = node.querySelectorAll('a[class~=mention][href^=http]')
          for (let mention of originalStatus.mentions) {
            for (let anchorTag of anchorTags) {
              if (anchorTag.getAttribute('href') === mention.url) {
                anchorTag.setAttribute('href', `/accounts/${mention.id}`)
                anchorTag.setAttribute('title', `@${mention.acct}`)
                anchorTag.setAttribute('focus-key', `status-content-link-${uuid}-${++count}`)
                anchorTag.removeAttribute('target')
                anchorTag.removeAttribute('rel')
              }
            }
          }
        }
        let externalLinks = node.querySelectorAll('a[rel="nofollow noopener"]')
        for (let link of externalLinks) {
          link.setAttribute('title', link.getAttribute('href'))
        }
        stop('hydrateContent')
      }
    }
  }
</script>