#!/bin/zsh

### Parse antidote's bundle DSL.
#function __antidote_parse_bundles {
  emulate -L zsh; setopt local_options $_adote_funcopts

  # Declare vars
  local bundle_str bundle_repr collected_input err lineno=0 skip_load_defer=0
  local key val
  local -a bundles
  local -A bundle

  # Get piped/passed bundles
  collected_input="$(__antidote_collect_input "$@")"
  if [[ -n "$collected_input" ]]; then
    bundles=( "${(@f)collected_input}" )
  else
    bundles=()
  fi
  if ! (( $#bundles )) ; then
    print -ru2 -- "antidote: error: bundle argument expected"
    return 1
  fi

  # Loop through bundles
  for bundle_str in $bundles; do
    (( lineno += 1 ))

    # Parse the bundle.
    bundle_repr=$(__antidote_parser "$bundle_str"); err=$?
    if [[ -z "$bundle_repr" ]]; then
      continue
    elif [[ "$err" -ne 0 ]]; then
      print -ru2 -- "antidote: Bundle parser error on line ${lineno}: '$bundle_str'"
      return 1
    fi

    # Turn the typeset repr into the bundle assoc_arr
    eval "$bundle_repr"

    # move flags to front and call antidote-script
    print -rn -- "antidote-script"
    for key in ${(ok)bundle}; do
      [[ "$key" != name ]] && [[ "$key" != '_'* ]] || continue
      val="${bundle[$key]}"
      if [[ "$val" == "${(q)val}" ]]; then
        printf ' --%s %s' $key $val
      else
        printf ' --%s %s' $key ${(qqq)val}
      fi
    done

    # Add flag for first defer
    if [[ "${bundle[kind]}" == "defer" ]]; then
      if [[ "$skip_load_defer" -eq 0 ]]; then
        skip_load_defer=1
      else
        printf ' --skip-load-defer'
      fi
    fi

    # Escape leading '$' variables
    if [[ "${bundle[name]}" == '$'* ]]; then
      printf ' \$%s\n' "${bundle[name]#\$}"
    else
      printf ' %s\n' "${bundle[name]}"
    fi
  done
#}
