
use builtin;
use str;

set edit:completion:arg-completer[pibtool] = {|@words|
    fn spaces {|n|
        builtin:repeat $n ' ' | str:join ''
    }
    fn cand {|text desc|
        edit:complex-candidate $text &display=$text' '(spaces (- 14 (wcswidth $text)))$desc
    }
    var command = 'pibtool'
    for word $words[1..-1] {
        if (str:has-prefix $word '-') {
            break
        }
        set command = $command';'$word
    }
    var completions = [
        &'pibtool'= {
            cand -h 'Print help'
            cand --help 'Print help'
            cand -V 'Print version'
            cand --version 'Print version'
            cand fetch-documents 'Fetch documents from one or multiple authorities'
            cand fetch-borders 'Fetch the borders from the OpenStreetMap overpass API'
            cand check 'Check that the `pib.json` definition files in the current directory are correct'
            cand generate-website-data 'Generate website data'
            cand generate-shell-completion 'Generate shell comletion'
            cand help 'Print this message or the help of the given subcommand(s)'
        }
        &'pibtool;fetch-documents'= {
            cand --only-recent-years 'Set the number of recent years which should be checked for new documents'
            cand --force-download 'Force a (re-)download, even if the file is already known to `pibtool`'
            cand --recursive 'Recurse into subdirectories and download for all authorities found there'
            cand -v 'Switch to a more verbose mode. By default, only `info`, `warn` and `error` log levels are printed. Given once, `debug` log level is turned on. Given twice or more, `trace` log level is turned on as well'
            cand --verbose 'Switch to a more verbose mode. By default, only `info`, `warn` and `error` log levels are printed. Given once, `debug` log level is turned on. Given twice or more, `trace` log level is turned on as well'
            cand -h 'Print help'
            cand --help 'Print help'
        }
        &'pibtool;fetch-borders'= {
            cand --geojson-converter 'Convert the download OSM JSON file to GeoJson with this command if present'
            cand --simplify-tolerance 'Simplify the downloaded borders using the Douglas-Peucker algorithm with the given tolerance'
            cand --force-download 'Force a (re-)download,  even if the file is already present'
            cand --recursive 'Recurse into subdirectories and download for all authorities found there'
            cand -h 'Print help (see more with ''--help'')'
            cand --help 'Print help (see more with ''--help'')'
        }
        &'pibtool;check'= {
            cand -h 'Print help'
            cand --help 'Print help'
        }
        &'pibtool;generate-website-data'= {
            cand -h 'Print help'
            cand --help 'Print help'
        }
        &'pibtool;generate-shell-completion'= {
            cand -h 'Print help'
            cand --help 'Print help'
        }
        &'pibtool;help'= {
            cand fetch-documents 'Fetch documents from one or multiple authorities'
            cand fetch-borders 'Fetch the borders from the OpenStreetMap overpass API'
            cand check 'Check that the `pib.json` definition files in the current directory are correct'
            cand generate-website-data 'Generate website data'
            cand generate-shell-completion 'Generate shell comletion'
            cand help 'Print this message or the help of the given subcommand(s)'
        }
        &'pibtool;help;fetch-documents'= {
        }
        &'pibtool;help;fetch-borders'= {
        }
        &'pibtool;help;check'= {
        }
        &'pibtool;help;generate-website-data'= {
        }
        &'pibtool;help;generate-shell-completion'= {
        }
        &'pibtool;help;help'= {
        }
    ]
    $completions[$command]
}
