# shellcheck shell=dash

___x_cmd_notify___main(){
    [ "$#" -gt 0 ] ||   set - -h

    local op="$1";      shift
    case "$op" in
        --help|-h)      x help -m notify >&2;                ;;
        --)             ___x_cmd_notify___run           "$@" ;;
        *)              ___x_cmd_notify___run "$op"     "$@" ;;
    esac
}

___x_cmd_notify___run(){
    local x_
    x os name_
    case "$x_" in
        win|darwin)
            "___x_cmd_notify___${x_}" "$@"
            ;;
        linux)
            if x os is wsl; then
                ___x_cmd_notify___win  "$@"
                return
            fi
            ___x_cmd_notify___linux "$@"
            ;;
        *)
            ;;
    esac
}

___x_cmd_notify___darwin(){
    local msg="${1:-"msg from 'x notify'"}"
    local title="${2:-"x-cmd"}"

    msg=$(printf "%s" "$msg" | ___x_cmd_cmds sed 's/\\/\\\\/g; s/"/\\"/g')
    title=$(printf "%s" "$title" | ___x_cmd_cmds sed 's/\\/\\\\/g; s/"/\\"/g')

    ___x_cmd_cmds osascript -e \
        "display notification \"$msg\" with title \"$title\""
}

___x_cmd_notify___linux(){
    local msg="${1:-"msg from 'x notify'"}"
    local title="${2:-"x-cmd"}"

    ___x_cmd_cmds notify-send "$title" "$msg"
}

___x_cmd_notify___win(){
    local msg="${1:-"msg from 'x notify'"}"
    local title="${2:-"x-cmd"}"

    x pwsh <<A
Add-Type -AssemblyName System.Windows.Forms
function DisplayNotification([string]\$msg, [string]\$title) {
    \$action = New-Object System.Windows.Forms.NotifyIcon
    \$path = (Get-Process -id \$pid).Path
    \$action.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon(\$path)
    \$action.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Info
    \$action.BalloonTipText = \$msg
    \$action.BalloonTipTitle = \$title
    \$action.Visible = \$true
    \$action.ShowBalloonTip(1)
}

DisplayNotification -msg "$msg" -title "$title"
A

}
