<template>
  <message-outgoing v-bind="{ ...$props, ...$attrs }">
    <div class="bubble image-bubble">
      <img :src="src" :style="imageStyle" />
    </div>
  </message-outgoing>
</template>

<script>
import MessageOutgoing from "../MessageOutgoing.vue";

export default {
  name: "MessageOutgoingImageExport",
  extends: MessageOutgoing,
  components: { MessageOutgoing },
  data() {
    return {
      src: null,
      cover: true,
      contain: false,
    };
  },
  mounted() {
    const info = this.event.getContent().info;
    // JPEGs use cover, PNG and GIF ect contain. This is because PNG and GIF are expected to
    // be stickers and small emoji type things.
    if (info && info.mimetype && info.mimetype.startsWith("image/jp")) {
      this.cover = true;
      this.contain = false;
    } else {
      this.cover = false;
      this.contain = true;
    }
  },
  beforeUnmount() {
    if (this.src) {
      const objectUrl = this.src;
      this.src = null;
      URL.revokeObjectURL(objectUrl);
    }
  },
  computed: {
    imageStyle() {
      return "width:100%;aspect-ratio:16/9;object-fit:" + (this.cover ? "cover" : "contain");
    }
  }
};
</script>

<style lang="scss">
@use "@/assets/css/chat.scss" as *;
</style>
