<template>
  <div class="preview-layer">
    <div class="pull-meta">
      <ResourceView :data="media.url" :type="media.type" />
    </div>
  </div>
</template>

<script lang="ts">
import { ref, watchEffect, defineComponent, PropType } from "vue";
import { getResource } from "@/env";
import { MediaType } from "./index.vue";
import ResourceView from "./resource.vue";
import type { MediaItem } from "./index.vue";

export const Preview = defineComponent({
  name: "static-preview",
  props: {
    media: {
      type: Object as PropType<MediaItem>,
      required: true,
    },
  },
  emits: {
    close: () => true,
  },
  setup(props) {
    const staticURL = ref("");
    watchEffect(() => {
      const data = props.media.url;
      const url =
        typeof data === "string" ? getResource(data) : URL.createObjectURL(data);

      staticURL.value = url;
      return () => URL.revokeObjectURL(url);
    });

    return {
      staticURL,
      MediaType,
    };
  },
});

export default Preview;
</script>

<style scoped lang="scss" src="./style.scss"></style>
