class AsDoc
  class Complete

    include Util

    ID = [:id, 'id']

    def initialize(options)
      @options = complete(options)
    end

    def value
      deep_copy @options
    end

    private

    def complete(options)
      options.each_pair do |k, v|
        case
        when k == :only
          v = handle_only(v)
        when k == :include
          v = handle_include(v)
        end
        options[k] = v
      end

      options
    end

    def handle_only(fields)
      fields = Array(fields)
      fields << :id if (fields & ID).empty?
      fields
    end

    def handle_include(includes)
      includes = convert_to_hash(includes)

      includes.each_pair do |relation, opts|
        includes[relation] = complete(opts)
      end

      includes
    end
  end
end
