import { BadRequestException, ForbiddenException } from "@nestjs/common";
import { join } from "path";
import { writeFile } from "fs/promises";
import { toDataURL } from 'qrcode';
export class QrCode {
    //return base64
    static async generate(currentName: string): Promise<string> {
        try {
            return await toDataURL(currentName.toString(), {
                color: {
                    dark: '#000000', // Màu tối cho QR code
                    light: '#ffffff' // Màu sáng (trong suốt)
                },
                width: 300,
                margin: 1,
                scale: 0,
            });
        } catch (error) {
            throw new BadRequestException('Error generating QR Code');
        }
    }
    static async  saveQRCode(content: string, filenameQR: string): Promise<string> {
        const qrData = await this.generate(content);
        const base64Data = qrData.split(',')[1]; // Lấy phần base64
        // Sử dụng process.cwd() để lấy đường dẫn đúng
        //const filePathQR = join(process.cwd(), 'qrcode', filenameQR);        
        const filePathQR = join(__dirname, '..', '..','qrcode', filenameQR);
        await writeFile(filePathQR, base64Data, { encoding: 'base64' });
        return filePathQR;
    }
}