# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
module Elasticsearch
module API
<%- @spec.module_namespace.each_with_index do |name, i| -%>
  <%= '  '*i %>module <%= module_name_helper(name) %>
<%- end -%>
module Actions
<%= ERB.new(File.new("./thor/templates/_documentation_top.erb").read, trim_mode: '-').result(binding) -%>
<%# Method definition -%>
def <%= @spec.method_name %>(arguments = {})
  request_opts = { endpoint: arguments[:endpoint] || "<%= @spec.endpoint_name %>" }
<%- unless @spec.path_params.empty? %>
  defined_params =<%=  @spec.path_params %>.inject({}) do |set_variables, variable|
    set_variables[variable] = arguments[variable] if arguments.key?(variable)
    set_variables
  end
  request_opts[:defined_params] = defined_params unless defined_params.empty?
<%- end %>
<%-    %>
<%- if @spec.endpoint_name == 'create' -%>
if arguments[:id]
  index arguments.update op_type: 'create'
else
  index arguments
end
end
<%- else -%>
  <%- if @spec.method_name == 'get_field_mapping' %>
    arguments = arguments.clone
    _fields = arguments.delete(:field) || arguments.delete(:fields)
    raise ArgumentError, "Required argument 'field' missing" unless _fields
  <%- else -%>
    <%- @spec.required_parts.each do |required| %><%# Arguments -%>
      <%= "raise ArgumentError, \"Required argument '#{required}' missing\" unless arguments[:#{required}]" + "\n" -%>
    <%- end -%>
    arguments = arguments.clone
    <%- end -%>
    headers = arguments.delete(:headers) || {}
    <%- #Body %>
    <%= ERB.new(File.new("./thor/templates/_body.erb").read, trim_mode: '-').result(binding) %>
    <%- # Method setup -%>
    <%= ERB.new(File.new("./thor/templates/_method_setup.erb").read, trim_mode: '-').result(binding) %>
    <%- # Perform request -%>
    <%= ERB.new(File.new("./thor/templates/_perform_request.erb").read, trim_mode: '-').result(binding) %>
<%- end -%>
<%- if @spec.method_name.match?(/^exists\S*/) -%>
  alias_method :<%= @spec.method_name %>?, :<%= @spec.method_name %>
<%- end -%>
<%- if @spec.method_name == 'termvectors' %>
  # Deprecated: Use the plural version, {#termvectors}
  #
  def termvector(arguments={})
    warn "[DEPRECATION] `termvector` is deprecated. Please use the plural version, `termvectors` instead."
    termvectors(arguments.merge(endpoint: '_termvector'))
  end
<%- end -%>
<%- @spec.namespace_depth.downto(1) do |i| -%>
  <%= '  '*(i-1) %>end
<%- end if @spec.namespace_depth > 0 -%>

    end
  end
end
