import { PPTImageElement } from '@/types/slides';
import { compressionThumbnail } from './compressor';

export const ResolveLayer = async (item: any, index: number,offsetLeft:number,offsetTop:number,groupId:string =''): Promise<PPTImageElement | null> => {

    //await compressionThumbnail(item.layer.image.toBase64(), "image/png", 0, 0.8)
    try {
        const src = await compressionThumbnail(item.layer.image.toBase64(), "image/png", 0, 0.8)//item.layer.image.toBase64()
        const left = item.coords.left-offsetLeft
        const width = item.coords.right - item.coords.left
        const top = item.coords.top-offsetTop
        const height = item.coords.bottom - item.coords.top
        const opacity = (parseFloat(item.layer.opacity) / 255.0).toFixed(2)

        let objectEFFFects = item.layer.objectEffects ? item.layer.objectEffects() : null

        let element: PPTImageElement = {
            id: "img_" + index,
            type: 'image',
            src,
            fixedRatio: true,
            filters: {
                opacity:src==''?'0':opacity
            },
            left,
            width,
            top,
            height,
            rotate: 0,
            lock: item.name.indexOf('editor')==-1 || (item.name.indexOf('editor')!=-1 && item.name.indexOf('before')!=-1),
            layerName: item.layer.name,
            groupId:groupId
        }

        return element
    } catch (error) { 
        return null
    }
}