#!/bin/zsh


set_wallpaper_linux() {
    echo "TODO: theme_update_wallpaper_linux not yet implemented"
}

set_wallpaper_macos() {
    # after back and forth with gpt and failing approaches, landing on this swift snippet
    swift <(cat << NESTED_SWIFT_BLOCK
        import AppKit
        let url = URL(fileURLWithPath: "$1")
        try NSWorkspace.shared.setDesktopImageURL(url, for: NSScreen.main!, options: [:])
NESTED_SWIFT_BLOCK
    )
}

wallpaper_dir=$DIR_THEME_SETTINGS/.current-theme/wallpaper
image_paths=($(find $wallpaper_dir -type f | sort)) 2> /dev/null

[[ $1 = "zz-default-for-theme" ]] && target_wallpaper=${image_paths[@]:0:1} ||
    target_wallpaper=$(printf '%s\n' ${image_paths[@]} | fzf)
[[ -z $target_wallpaper ]] && exit 0

[[ "$OSTYPE" = *"darwin"* ]] && set_wallpaper_macos $target_wallpaper ||
    set_wallpaper_linux $target_wallpaper
