#!/usr/pkg/bin/ruby40
# frozen_string_literal: true

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '../lib'))

require 'optparse'
require 'json'
require 'cucumber/messages'
require 'gherkin'

options = {
  include_source: true,
  include_gherkin_document: true,
  include_pickles: true,
  predictable_ids: false
}

OptionParser.new do |opts|
  opts.on('--[no-]source', "Don't print source messages") do |value|
    options[:include_source] = value
  end
  opts.on('--[no-]ast', "Don't print ast messages") do |value|
    options[:include_gherkin_document] = value
  end
  opts.on('--[no-]pickles', "Don't print pickle messages") do |value|
    options[:include_pickles] = value
  end
  opts.on('--predictable-ids', 'Generate incrementing ids rather than UUIDs') do |value|
    options[:id_generator] = Cucumber::Messages::Helpers::IdGenerator::Incrementing.new if value
  end
end.parse!

def process_messages(messages, _options)
  messages.each do |message|
    $stdout.write(message.to_json)
    $stdout.write("\n")
  end
end

messages =
  if ARGV.empty?
    # Read from $stdin
    Cucumber::Messages::Helpers::NdjsonToMessageEnumerator.new($stdin)
  else
    Gherkin.from_paths(ARGV, options)
  end

process_messages(messages, options)
