# frozen_string_literal: true

require 'git/commands/base'

module Git
  module Commands
    module ConfigOptionSyntax
      # Retrieve config entries matching a name regex
      #
      # Wraps `git config --get-regexp` to return all config entries whose
      # key name matches the given regular expression.
      #
      # @example Get all entries matching a pattern
      #   cmd = Git::Commands::ConfigOptionSyntax::GetRegexp.new(lib)
      #   cmd.call('remote\..*\.url')
      #
      # @example Get matching entries with value filter
      #   cmd = Git::Commands::ConfigOptionSyntax::GetRegexp.new(lib)
      #   cmd.call('remote\..*\.url', 'github')
      #
      # @note `arguments` block audited against https://git-scm.com/docs/git-config/2.53.0
      #
      # @see Git::Commands::ConfigOptionSyntax
      #
      # @see https://git-scm.com/docs/git-config git-config
      #
      # @api private
      #
      class GetRegexp < Git::Commands::Base
        arguments do
          literal 'config'
          literal '--get-regexp'

          # File-scope options
          flag_option :global
          flag_option :system
          flag_option :local
          flag_option :worktree
          value_option %i[file f]
          value_option :blob

          # General read options
          flag_option :includes, negatable: true

          # Type constraint
          value_option :type, inline: true

          # Output modifiers
          flag_option :show_origin
          flag_option :show_scope
          flag_option %i[null z]
          flag_option :name_only

          # Operands
          end_of_options
          operand :name_regex, required: true
          operand :value_regex
        end

        # git config --get-regexp exits 1 when no match is found (not an error)
        allow_exit_status 0..1

        # @overload call(name_regex, value_regex = nil, **options)
        #
        #     Execute the `git config --get-regexp` command
        #
        #     @param name_regex [String] regex pattern to match config key names
        #
        #     @param value_regex [String, nil] optional regex to filter values
        #
        #     @param options [Hash] command options
        #
        #     @option options [Boolean, nil] :global (nil) read from global config (`~/.gitconfig`)
        #
        #     @option options [Boolean, nil] :system (nil) read from system config
        #
        #     @option options [Boolean, nil] :local (nil) read from repository config (`.git/config`)
        #
        #     @option options [Boolean, nil] :worktree (nil) read from worktree config
        #
        #     @option options [String] :file (nil) read from the specified file
        #
        #       Alias: :f
        #
        #     @option options [String] :blob (nil) read from the specified blob
        #
        #     @option options [Boolean, nil] :includes (nil) respect include directives in
        #       config files (`--includes`)
        #
        #     @option options [Boolean, nil] :no_includes (nil) ignore include directives in
        #       config files (`--no-includes`)
        #
        #     @option options [String] :type (nil) ensure values conform to the given type
        #
        #     @option options [Boolean, nil] :show_origin (nil) show the origin of each config entry
        #
        #     @option options [Boolean, nil] :show_scope (nil) show the scope of each config entry
        #
        #     @option options [Boolean, nil] :null (nil) terminate values with NUL byte instead of newline
        #
        #       Alias: :z
        #
        #     @option options [Boolean, nil] :name_only (nil) output only the names of config keys
        #
        #     @return [Git::CommandLine::Result] the result of calling `git config --get-regexp`
        #
        #     @raise [ArgumentError] if unsupported options are provided
        #
        #     @raise [Git::FailedError] if git exits outside the allowed range (exit code > 1)
        #
        #     @api public
        #
        def call(*, **)
          super
        end
      end
    end
  end
end
