import '@johnlindquist/kit'
import Jimp from 'jimp'
let downloadsPath = home('Downloads')
let files = await readdir(downloadsPath)
let pngFiles = files.filter(file => file.toLowerCase().endsWith('.png'))
let filesWithStats = []
for (let pngFile of pngFiles) {
let filePath = path.join(downloadsPath, pngFile)
let stats = await stat(filePath)
filesWithStats.push({ path: filePath, stats })
}
filesWithStats.sort((a, b) => b.stats.mtimeMs - a.stats.mtimeMs)
let latestTwoImages = filesWithStats.slice(0, 2)
if (latestTwoImages.length < 2) {
await div(md(`## Not enough PNG images found in Downloads`))
exit()
}
let [image1Path, image2Path] = latestTwoImages.map(f => f.path)
let image1 = await Jimp.read(image1Path)
let image2 = await Jimp.read(image2Path)
let combinedWidth = image1.getWidth() + image2.getWidth()
let maxHeight = Math.max(image1.getHeight(), image2.getHeight())
let combinedImage = new Jimp(combinedWidth, maxHeight)
combinedImage.composite(image1, 0, 0)
combinedImage.composite(image2, image1.getWidth(), 0)
let outputImagePath = path.join(downloadsPath, `combined-${Date.now()}.png`)
await combinedImage.writeAsync(outputImagePath)
await revealFile(outputImagePath)