# vim: ft=zsh:noml:
# @module: view
# view various builtin parameters

local function print::flag() {
  setopt extendedglob
  local MATCH MEND MBEGIN
  local str="  "
  str+=${(j:, :)${(@)${(@s:,:)2}//(#m)*/%${1}F${MATCH}%f}}
  if [[ -n "$4" ]] {
    str+=${${${4}:+%${3}F${4}%f}:-}
  } else {
    str+="$3"
  }
  builtin print -Pr -- "$str"
}
local function print::header() {
  builtin print -Pr -- "%F{$1}%B$2:%f%b"
}
local function print::usage() {
  print::header 4 "USAGE"
  setopt extendedglob
  local MATCH MEND MBEGIN
  local str="  "
  str+=${(j:, :)${(@)${(@s:,:)2}//(#m)*/%${1}F${MATCH}%f}}
  if [[ -n "$4" ]] {
    str+=${${${4}:+%${3}F${4}%f}:-}
  } else {
    str+="$3"
  }
  # print -Pr -- "  %4F%BUsage%b%f: %3F$1%f ${@[2,-1]}"
  print -Pr -- "  %3F$1%f ${@[2,-1]}"
}

local function __make_patt() {
  typeset -g REPLY
  if [[ -n "$2" ]]; then
    if (( $1 )) { REPLY="$2" } else { REPLY="*${2}*" }
  else
    REPLY=
  fi
}

local function print_help() {
  print::usage "view" "[FLAGS] [PATTERN]"
  print::header 12 "OPTIONS"
  print::flag 20 "-e,--exact     " "          Pattern is an exact"
  print::flag 20 "-f,--func      " "[PATTERN] Show functions"
  print::flag 20 "-v,--vec       " "[PATTERN] Show vector variables"
  print::flag 20 "-A,--assoc     " "[PATTERN] Show associative array variables"
  print::flag 20 "-a,--arr       " "[PATTERN] Show all array variables"
  print::flag 20 "-H,--hidden    " "[PATTERN] Show hidden variables"
  print::flag 20 "-h,--special   " "[PATTERN] Show special variables"
  print::flag 20 "-l,--local     " "[PATTERN] Show local variables"
  print::flag 20 "-g,--global    " "[PATTERN] Show global variables"
  print::flag 20 "-x,--export    " "[PATTERN] Show exported variables"
  print::flag 20 "-r,--readonly  " "[PATTERN] Show readonly variables"
  print::flag 20 "    --help      " "          Show help menu"
}

# view various params
function view() {
  builtin emulate -L zsh -o extendedglob -o warncreateglobal
  zmodload -Fa zsh/zutil b:zparseopts

  local -a match=() mbegin=() mend=()
  local -A Opts
  builtin zparseopts \
    ${${(M)ZSH_VERSION:#(5.[8-9]|6.[0-9])}:+-F} -D -E -A Opts -- \
      -exact      e   \
      -func::     f:: \
      -vec::      v:: \
      -assoc::    A:: \
      -arr::      a:: \
      -hidden::   h:: \
      -hideval::  H:: \
      -special::  s:: \
      -local::    l:: \
      -global::   g:: \
      -export::   x:: \
      -readonly:: r:: \
      -tagged::   t:: \
      -tied::     T:: \
      -help \
    || { zerr "invalid option given; see {cmd}--help{%}";
         builtin print;
         print_help;
         return errno 22; }

  [[ -v Opts[--help] ]] && { print_help; return 0; }

  integer ex; ex=$(( ${+Opts[--exact]} + ${+Opts[-e]} ))
  {
    case "${(k)Opts[@]}" {
      ((#b)*(--local|-l)*)
        __make_patt $ex "${Opts[$match[1]]}"
        typeset -mp1 "${REPLY:-'*'}"
      ;|
      ((#b)*(--global|-g)*)
        __make_patt $ex "${Opts[$match[1]]}"
        typeset -gmp1 "${REPLY:-'*'}"
      ;|
      ((#b)*(--export|-x)*)
        __make_patt $ex "${Opts[$match[1]]}"
        typeset -xmp1 "${REPLY:-'*'}"
        # export -mp1 "$REPLY"
      ;|
      ((#b)*(--readonly|-r)*)
        __make_patt $ex "${Opts[$match[1]]}"
        typeset -mrp1 "${REPLY:-'*'}"
      ;|
      ((#b)*(--hidden|-h)*)
        __make_patt $ex "${Opts[$match[1]]}"
        typeset -mhp1 "${REPLY:-'*'}"
      ;|
      ((#b)*(--special|-s)*)
        __make_patt $ex "${Opts[$match[1]]}"
        typeset -mhp1 "${REPLY:-'*'}"
      ;|
      ((#b)*(--hideval|-H)*)
        __make_patt $ex "${Opts[$match[1]]}"
        typeset -mHp1 "${REPLY:-'*'}"
      ;|
      ((#b)*(--tied|-t)*)
        __make_patt $ex "${Opts[$match[1]]}"
        typeset -mtp1 "${REPLY:-'*'}"
      ;|
      ((#b)*(--tagged|-T)*)
        # FIX: thinks changine username
        __make_patt $ex "${Opts[$match[1]]}"
        # typeset -mTp1 "${REPLY:-'*'}"
        typeset -mTp1
      ;|
      ((#b)*(--func|-f)*)
        __make_patt $ex "${Opts[$match[1]]}"
        # typeset -mfp1 "$REPLY"
        functions -mx2 "${REPLY:-'*'}"
      ;|
      ((#b)*(--vec|-v)*)
        __make_patt $ex "${Opts[$match[1]]}"
        typeset -map1 "$REPLY"
      ;|
      ((#b)*(--assoc|-A)*)
        __make_patt $ex "${Opts[$match[1]]}"
        typeset -mAp1 "${REPLY:-'*'}"
      ;|
      ((#b)*(--arr|-a)*)
        __make_patt $ex "${Opts[$match[1]]}"
        typeset -map1 "${REPLY:-'*'}"
        typeset -mAp1 "${REPLY:-'*'}"
      ;|
    }
  } | bat --color=always --style=snip --language=bash
}

view "$@"
