use std
use ../lib_provisioning/utils *

export def clip_copy [
  msg: string
  show: bool
] {
  if (not $show) { _print $msg }
}

export def notify_msg [
    title: string
    body: string
    icon: string
    time_body: string
    timeout: duration
    task?: closure
] {
    if $task != null {
      _print (
        $"(_ansi blue)($title)(_ansi reset)\n(ansi blue_bold)($time_body)(_ansi reset)"
      )
   } else {
      _print (
        $"(_ansi blue)($title)(_ansi reset)\n(ansi blue_bold)($body)(_ansi reset)"
      )
    }
}

export def show_qr  [
  url: string
] {
  let qr_path = ($env.PROVISIONING_RESOURCES | path join "qrs" | path join ($env.PROVISIONING |
    ($url | path basename)
  ))
  if ($qr_path | path exists) {
    _print (open -r $qr_path)
  } else {
    _print $"(_ansi purple)($url)(_ansi reset)"
  }
}

export def port_scan [
  ip: string
  port: int
  sec_timeout: int
] {
  # # control moved to core/bin/install_nu.sh
  # if (^bash -c "type -P nc" | is-empty) {
  #   (throw-error $"🛑 port scan ($ip) ($port)" $"(_ansi green)nc(_ansi reset) command not found"
  #   "port_scan")
  #    exit 1
  # }
  (^nc -zv -w $sec_timeout ($ip | str trim) $port  err> (if $nu.os-info.name == "windows" { "NUL" } else { "/dev/null" })| complete).exit_code == 0
}
