export const uploadImage = async (file: File): Promise<string> => {
    if (!file) return '';

    try {
        const formData = new FormData();
        formData.append('file', file);

        const response = await fetch('/api/upload', {
            method: 'POST',
            body: formData,
        });

        if (!response.ok) {
            throw new Error('Server returned error status ' + response.status);
        }

        const data = await response.json();
        if (data.success && data.url) {
            return data.url;
        } else {
            throw new Error(data.error || 'Gagal menyimpan berkas di server');
        }
    } catch (e: any) {
        console.error("Upload failed: ", e);
        throw e;
    }
};
