/**
 * Small generic helpers ported from the source design system's kernel for
 * the ui/molecules library.
 */
import { __ } from '@wordpress/i18n';

export function isValidArray(value) {
    return Array.isArray(value) && value.length > 0;
}

export const formatFileSize = (bytes) => {
    if (bytes === 0) return __('0 Bytes', 'rewind-form-bridge-for-brevo');
    const k = 1024;
    const sizes = [
        __('Bytes', 'rewind-form-bridge-for-brevo'),
        __('KB', 'rewind-form-bridge-for-brevo'),
        __('MB', 'rewind-form-bridge-for-brevo'),
        __('GB', 'rewind-form-bridge-for-brevo'),
        __('TB', 'rewind-form-bridge-for-brevo'),
        __('PB', 'rewind-form-bridge-for-brevo'),
        __('EB', 'rewind-form-bridge-for-brevo'),
        __('ZB', 'rewind-form-bridge-for-brevo'),
        __('YB', 'rewind-form-bridge-for-brevo'),
    ];
    const i = Math.floor(Math.log(bytes) / Math.log(k));
    return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
};
