require 'turnip/node/base'
require 'turnip/node/tag'
require 'turnip/node/scenario_group_definition'
require 'turnip/node/rule'

module Turnip
  module Node
    #
    # @note Feature metadata generated by Gherkin
    #
    #     {
    #       type: :Feature,
    #       tags: [], # Array of Tag
    #       location: { line: 10, column: 3 },
    #       language: 'en',
    #       keyword: 'Feature',
    #       name: 'Feature name',
    #       description: 'Feature description',
    #       children: [], # Array of Background, Scenario and Scenario Outline
    #     }
    #
    class Feature < ScenarioGroupDefinition
      include HasTags

      def language
        @raw.language
      end

      def children
        @children ||= @raw.children.map do |child|
          if child.is_a?(CukeModeler::Background)
            next Background.new(child)
          end

          if child.is_a?(CukeModeler::Scenario)
            next Scenario.new(child)
          end

          if child.is_a?(CukeModeler::Outline)
            next ScenarioOutline.new(child)
          end

          if child.is_a?(CukeModeler::Rule)
            next Rule.new(child)
          end
        end.compact
      end

      def rules
        @rules ||= children.select do |c|
          c.is_a?(Rule)
        end
      end

      def metadata_hash
        super.merge(:type => Turnip.type, :turnip => true)
      end
    end
  end
end
