#compdef bmm

_bmm_commands() {
  local -a commands
  commands=(
    'import:Import bookmarks from various sources'
    'delete:Delete bookmarks'
    'list:List bookmarks based on several kinds of queries'
    'save:Save/update a bookmark'
    'save-all:Save/update multiple bookmarks'
    'search:Search bookmarks by matching over terms'
    'show:Show bookmark details'
    'tags:Interact with tags'
    'tui:Open bmm'\''s TUI'
    'help:Print this message or the help of the given subcommand(s)'
  )
  _describe 'command' commands
}

_bmm_tags() {
  # local subcommands ret=1
  local -a subcommands=(
    'delete:Delete tags'
    'list:List tags stored by bmm'
    'rename:Rename a tag'
    'help:Print this message or the help of the given subcommand(s)'
  )

  _arguments -s -S \
    '1: :{_describe 'command' subcommands}' \
    '*:: :->subcommand_args' && ret=0

  case $state in
    (subcommand_args)
    case $words[1] in
      (delete)
        _arguments \
          {-y,--yes}'[Whether to skip confirmation]' \
          '--db-path[Override bmm'\''s database location (default: <DATA_DIR>/bmm/bmm.db)]:STRING:' \
          '--debug[Output debug information without doing anything]' \
          {-h,--help}'[Print help]' \
          '*::STRING:' \
        && ret=0
        ;;
      (help)
        _arguments \
          '*::HELP:(delete list rename)' \
        && ret=0
        ;;
      (list)
        _arguments \
          {-f,--format}'[Format to output in (default: plain)]:format:(delimited json plain)' \
          {-s,--show-stats}'[Whether to show tag stats]' \
          '--tui[Whether to show results in bmm'\''s TUI]' \
          '--db-path[Override bmm'\''s database location (default: <DATA_DIR>/bmm/bmm.db)]:STRING:' \
          '--debug[Output debug information without doing anything]' \
          {-h,--help}'[Print help]' \
        && ret=0
        ;;
      (rename)
        _arguments \
          '--db-path[Override bmm'\''s database location (default: <DATA_DIR>/bmm/bmm.db)]:STRING:' \
          '--debug[Output debug information without doing anything]' \
          {-h,--help}'[Print help]' \
          ':SOURCE:' \
          ':TARGET:' \
        && ret=0
       ;;
    esac
    ;;
  esac

  return ret
}

_bmm_tags_list() {
  local expl
  tags=(${${(f)"$(bmm tags list)"}})

  _wanted options expl 'bmm tags' compadd -qS, -a '' tags
  # _wanted opts expl 'bmm tags' compadd -S '' - {,}${^ts} && ret=0
}

_bmm() {
  local ret=1
  local curcontext=$curcontext state line
  typeset -A opt_args

  _arguments -C \
    '(- 1 *)'{-h,--help}'[Print help]' \
    '(- 1 *)''--db-path[Override bmm'\''s database location]:STRING:' \
    '(- 1 *)''--debug[Output debug information without doing anything]' \
    '1: :_bmm_commands' \
    '*:: :->command_args' \
    && ret=0

  case $state in
    (command_args)
      case $words[1] in
      (import)
        _arguments \
          {-d,--dry-run}'[Display bookmarks that will be imported without actually importing them]' \
          {-r,--reset-missing-details}'[Reset previously saved details if not provided]' \
          {-i,--ignore-attribute-errors}'[Ignore errors related to title and tags; trim long titles, correct invalid tags]' \
          '--db-path[Override bmm'\''s database location (default: <DATA_DIR>/bmm/bmm.db)]:STRING:' \
          '--debug[Output debug information without doing anything]' \
          {-h,--help}'[Print help]' \
          '*::FILE (html json txt):_files' \
        && ret=0
        ;;
      (delete)
        _arguments \
          {-y,--yes}'[Whether to skip confirmation]' \
          '--db-path[Override bmm'\''s database location (default: <DATA_DIR>/bmm/bmm.db)]:STRING:' \
          '--debug[Output debug information without doing anything]' \
          {-h,--help}'[Print help]' \
          '*::URI:' \
        && ret=0
        ;;
      (help)
        _wanted expl 'commands' compadd _bmm_commands
        ;;
      (list)
        _arguments \
          {-u,--uri}'[Pattern to match bookmark URIs on]:URI:' \
          {-d,--title}'[Pattern to match bookmark titles on]:STRING:' \
          {-t,--tags}'[Tags to match (exactly)]:STRING:_sequence -s , _bmm_tags_list' \
          {-f,--format}'[Format to use]:format:(delimited json plain)' \
          {-l,--limit}'[Number of items to fetch]:INTEGER:' \
          '--db-path[Override bmm'\''s database location (default: <DATA_DIR>/bmm/bmm.db)]:STRING:' \
          '--debug[Output debug information without doing anything]' \
          {-h,--help}'[Print help]' \
        && ret=0
        ;;
      (save)
        _arguments \
          '--title[Title for the bookmark]:STRING:' \
          {-t,--tags}'[Tags to attach to the bookmark]:STRING:_sequence -s , _bmm_tags_list -' \
          {-e,--editor}'[Provide details via a text editor]' \
          {-f,--fail-if-already-saved}'[Fail if URI already saved (bmm will update the existing entry by default)]' \
          {-r,--reset-missing-details}'[Reset previously saved details if not provided]' \
          '--db-path[Override bmm'\''s database location (default: <DATA_DIR>/bmm/bmm.db)]:STRING:' \
          {-i,--ignore-attribute-errors}'[Ignore errors related to title and tags; trim long titles, correct invalid tags]' \
          '--debug[Output debug information without doing anything]' \
          {-h,--help}'[Print help]' \
        && ret=0
        ;;
      (save-all)
        _arguments \
          {-t,--tags}'[Tags to attach to the bookmark]:STRING:_sequence -s , _bmm_tags_list' \
          {-s,--stdin}'[Read input from stdin]' \
          {-r,--reset}'[Reset previously saved details if not provided]' \
          '--db-path[Override bmm'\''s database location (default: <DATA_DIR>/bmm/bmm.db)]:STRING:' \
          {-i,--ignore-attribute-errors}'[Ignore errors related to title and tags; trim long titles, correct invalid tags]' \
          '--debug[Output debug information without doing anything]' \
          {-h,--help}'[Print help]' \
        && ret=0
        ;;
      (search)
        _arguments \
          {-f,--format}'[Format to output in (default: plain)]:format:(delimited json plain)' \
          {-l,--limit}'[Number of items to fetch (default: 500)]:INTEGER:' \
          '--tui[Whether to show results in bmm'\''s TUI]' \
          '--db-path[Override bmm'\''s database location (default: <DATA_DIR>/bmm/bmm.db)]:STRING:' \
          '--debug[Output debug information without doing anything]' \
          {-h,--help}'[Print help]' \
          '*::TERM:' \
        && ret=0
        ;;
      (show)
        _arguments \
          '--db-path[Override bmm'\''s database location (default: <DATA_DIR>/bmm/bmm.db)]:STRING:' \
          '--debug[Output debug information without doing anything]' \
          {-h,--help}'[Print help]' \
          '*::URI:' \
        && ret=0
        ;;
      (tags)
        _bmm_tags && ret=0
        ;;
      (tui)
        _arguments \
          '--db-path[Override bmm'\''s database location (default: <DATA_DIR>/bmm/bmm.db)]:STRING:' \
          '--debug[Output debug information without doing anything]' \
          {-h,--help}'[Print help]' \
        && ret=0
        ;;
      esac
    ;;
  esac
  return ret
}

_bmm "$@"
