import log from '@converse/headless/log';
import tpl_audio from 'templates/audio.js';
import tpl_gif from 'templates/gif.js';
import tpl_image from 'templates/image.js';
import tpl_video from 'templates/video.js';
import { URL_PARSE_OPTIONS } from '@converse/headless/shared/constants.js';
import { _converse, api, converse } from '@converse/headless/core';
import { containsDirectives, getDirectiveAndLength, getDirectiveTemplate, isQuoteDirective } from './styling.js';
import { getHyperlinkTemplate } from 'utils/html.js';
import {
convertASCII2Emoji,
getCodePointReferences,
getEmojiMarkup,
getShortnameReferences
} from '@converse/headless/plugins/emoji/index.js';
import {
filterQueryParamsFromURL,
isAudioDomainAllowed,
isAudioURL,
isEncryptedFileURL,
isGIFURL,
isImageDomainAllowed,
isImageURL,
isVideoDomainAllowed,
isVideoURL
} from '@converse/headless/utils/url.js';
import { html } from 'lit';
const { URI } = converse.env;
const isString = s => typeof s === 'string';
// We don't render more than two line-breaks, replace extra line-breaks with
// the zero-width whitespace character
const collapseLineBreaks = text => text.replace(/\n\n+/g, m => `\n${'\u200B'.repeat(m.length - 2)}\n`);
const tpl_mention_with_nick = o => html`${o.mention}`;
const tpl_mention = o => html`${o.mention}`;
/**
* @class RichText
* A String subclass that is used to render rich text (i.e. text that contains
* hyperlinks, images, mentions, styling etc.).
*
* The "rich" parts of the text is represented by lit TemplateResult
* objects which are added via the {@link RichText.addTemplateResult}
* method and saved as metadata.
*
* By default Converse adds TemplateResults to support emojis, hyperlinks,
* images, map URIs and mentions.
*
* 3rd party plugins can listen for the `beforeMessageBodyTransformed`
* and/or `afterMessageBodyTransformed` events and then call
* `addTemplateResult` on the RichText instance in order to add their own
* rich features.
*/
export class RichText extends String {
/**
* Create a new {@link RichText} instance.
* @param { String } text - The text to be annotated
* @param { Integer } offset - The offset of this particular piece of text
* from the start of the original message text. This is necessary because
* RichText instances can be nested when templates call directives
* which create new RichText instances (as happens with XEP-393 styling directives).
* @param { Array } mentions - An array of mention references
* @param { Object } options
* @param { String } options.nick - The current user's nickname (only relevant if the message is in a XEP-0045 MUC)
* @param { Boolean } options.render_styling - Whether XEP-0393 message styling should be applied to the message
* @param { Boolean } options.show_images - Whether image URLs should be rendered as tags.
* @param { Boolean } options.embed_videos - Whether video URLs should be rendered as