import { PreviewFileBase64 } from '@/util/file'

export default {
    props: {
        type: {
            type: String,
            default: 'image'
        },
        id: {
            type: [String, Number],
            require: true
        }
    },
    data() {
        return {
            src: ''
        }
    },

    watch: {
        async id() {
            this.src = await this.getSrc()
        }
    },

    async created() {
        if (this.id) {
            this.src = await this.getSrc()
        }
    },

    methods: {

        async getSrc() {
            const base64 = await PreviewFileBase64(this.id)
            return base64
        }

    },

    render() {
        return this.type == 'avatar' ?
            <a-avatar src={this.src} {...{ props: this.$attrs }} />
            :
            <img src={this.src} {...{ props: this.$attrs }} />
    },
}