use std

export def datalist_to_format [
  out: string
  data: list
] {
  # Not supported "toml" => ($data | flatten | to toml )
  match $out {
    "json" => ( $data | to json )
    "yaml" => ( $data | to yaml )
    "text" => ( $data | to text )
    "md" => ( $data | to md )
    "nuon" => ( $data | to nuon )
    "csv" => ( $data | to csv )
    _  => {
        $data |table -e
        # if $cols != null {
        #   let str_cols = ($cols | str replace "ips" "")
        #   $ips = if ($cols | str contains "ips") {
        #     # _print (mw_servers_ips $curr_settings $args --prov $prov --serverpos $serverpos)
        #     ($data | each {|srv| | ($srv.ip_addresses |
        #       each {|it| { hostname: $srv.hostname, ip: $it.address, access: $it.access, family: $it.family }})} |
        #       flatten
        #     )
        #   }
        #   #if $str_cols != "" {
        #   #  ($data | select -o ($str_cols | split row ","))
        #   #}
        # } else {
        #   $data
        # }
    }
  }
}
export def money_conversion [
    src: string
    target: string
    amount: float
] {
    let host = 'api.frankfurter.app';
    let url =  $"https://($host)/latest?amount=($amount)&from=($src)&to=($target)"
    #let data = (http get $url --raw --allow-errors)
    let res = (^curl -sSL $url  err> (if $nu.os-info.name == "windows" { "NUL" } else { "/dev/null" }) | complete)
    if $res.exit_code == 0 and ($res.stdout | is-not-empty) {
      let json_data = ($res.stdout | from json)
      let rates = ($json_data | get rates? | default {})
      if ($target in ($rates | columns)) {
        $rates | get $target
      } else {
        0
      }
    } else { 0 }
}
