<template>
    <div v-for="(file, index) of files" :key="file.name + file.type + file.size" :class="cx('file')" v-bind="ptm('file')">
        <img role="presentation" :class="cx('fileThumbnail')" :alt="file.name" :src="file.objectURL" :width="previewWidth" v-bind="ptm('fileThumbnail')" />
        <div :class="cx('fileInfo')" v-bind="ptm('fileInfo')">
            <div :class="cx('fileName')" v-bind="ptm('fileName')">{{ file.name }}</div>
            <span :class="cx('fileSize')" v-bind="ptm('fileSize')">{{ formatSize(file.size) }}</span>
        </div>
        <Badge :value="badgeValue" :class="cx('pcFileBadge')" :severity="badgeSeverity" :unstyled="unstyled" :pt="ptm('pcFileBadge')" />
        <div :class="cx('fileActions')" v-bind="ptm('fileActions')">
            <Button iconOnly text rounded severity="danger" @click="$emit('remove', index)" :class="cx('pcFileRemoveButton')" :unstyled="unstyled" :pt="ptm('pcFileRemoveButton')">
                <component v-if="templates.fileremoveicon" :is="templates.fileremoveicon" :file="file" :index="index" />
                <Times v-else aria-hidden="true" v-bind="ptm('pcFileRemoveButton')['icon']" />
            </Button>
        </div>
    </div>
</template>

<script>
import BaseComponent from '@primevue/core/basecomponent';
import Times from '@primeicons/vue/times';
import Badge from 'primevue/badge';
import Button from 'primevue/button';

export default {
    name: 'FileContent',
    hostName: 'FileUpload',
    extends: BaseComponent,
    emits: ['remove'],
    props: {
        files: {
            type: Array,
            default: () => []
        },
        badgeSeverity: {
            type: String,
            default: 'warn'
        },
        badgeValue: {
            type: String,
            default: null
        },
        previewWidth: {
            type: Number,
            default: 50
        },
        templates: {
            type: null,
            default: null
        }
    },
    methods: {
        formatSize(bytes) {
            const k = 1024;
            const dm = 3;
            const sizes = this.$primevue.config.locale?.fileSizeTypes || ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

            if (bytes === 0) {
                return `0 ${sizes[0]}`;
            }

            const i = Math.floor(Math.log(bytes) / Math.log(k));
            const formattedSize = parseFloat((bytes / Math.pow(k, i)).toFixed(dm));

            return `${formattedSize} ${sizes[i]}`;
        }
    },
    components: {
        Button,
        Badge,
        Times
    }
};
</script>
