import { compressionThumbnail, cropImage } from "./compressor"

let PSDObject:any = null
export const ResolveLayer = async (psd:any) => {
    PSDObject = psd
    const { _children } = psd.tree()
    let thumbs = await ResolveThumbHandler(_children)
    return thumbs
}
const ResolveThumbHandler = async (items:any)=>{
    let imgs:Array<any> = []
    let imgBase64String = PSDObject.image.toBase64()
    for (let i = 0; i < items.length; i++) {
        const item = items[i];
        if(item.layer.artboard) {
            let cropImagePath = await cropImage(imgBase64String,"image/jpeg",item.layer.artboard().export().coords)
            let imagePath = await compressionThumbnail(cropImagePath,"image/jpeg",300)
            imgs.push(imagePath)
        }
    }
    return imgs
}

const ResolveHandler = async (items:any) =>{
    let imgs:Array<any> = []
    let imgBase64String = PSDObject.image.toBase64()
    items.forEach(async (x:any)=>{
        if(x.layer.artboard) {
            //console.log(x.layer.artboard().export())
            console.log(x.layer.image)
            let cropImagePath = await cropImage(imgBase64String,"image/jpeg",x.layer.artboard().export())
            console.log('加载了1 ',cropImagePath)
            let imagePath = await compressionThumbnail(cropImagePath,"image/jpeg",300)
            console.log('加载了2 ')
            imgs.push(imagePath)
            console.log('加载了 ',imgs.length)
           
        }
        if(x._children && x._children.length>0) ResolveHandler(x._children)
    })
    console.log('我已经返回了',imgs.length)
    return imgs
}