export default class HamalronChatMessage extends ChatMessage {
  /** @override */
  prepareDerivedData() {
    if (this._source?.rolls) {
      for (const r of this._source.rolls) {
        if (r.formula && !/^[\ddDfFkKlLhH+\-*\/()%\s]+$/.test(r.formula)) {
          r.formula = "1d6"
        }
      }
    }
    try {
      super.prepareDerivedData()
    } catch (e) {
      console.warn("Hamalron | Failed to parse roll, resetting rolls")
      if (this._source) this._source.rolls = []
      super.prepareDerivedData()
    }
  }

  async _renderRollContent(messageData) {
    const data = messageData.message
    const { default: HamalronTirageTarot } = await import("./tirage-tarot.mjs")
    if (this.rolls[0] instanceof HamalronTirageTarot) {
      const isPrivate = !this.isContentVisible
      const rollHTML = await this._renderRollHTML(isPrivate)
      if (isPrivate) {
        data.flavor = game.i18n.format("CHAT.PrivateRollContent", { user: this.user.name })
        messageData.isWhisper = false
        messageData.alias = this.user.name
      }
      data.content = `<section class="dice-rolls">${rollHTML}</section>`
      return
    }

    return super._renderRollContent(messageData)
  }
}
