module Concerns::Searchable
  extend ActiveSupport::Concern

  included do
    private

    def ignore_empty_query
      @query = params['search_term'] rescue nil
      redirect_to :root if @query.blank? && filters.empty?
    end

    def load_search
      begin
        @search = Search.search(@query, search_options, search_index)
      rescue => e
        Rails.logger.warn("error in search controller: #{e.message}")
        @search = nil
      end

      @main_filter = params[:main]
    end

    def search_options
      options = {filters: filters}
      options[:page] = params['requested_page'].to_i if params['requested_page'].present?
      options
    end

    def search_index
      # TODO Define mapping for index between FE and BE
      Search::DEFAULT_INDEX_NAME
    end

    AREA_TYPES = %w(wdpa oecm).freeze
    def check_area_type
      redirect_to :root unless AREA_TYPES.include?(params[:area_type].downcase)
    end

    def filters
      JSON.parse(params['filters']).symbolize_keys.slice(*Search::ALLOWED_FILTERS)
    end
  end
end

module Concerns::Searchable
  extend ActiveSupport::Concern

  included do
    private

    def ignore_empty_query
      @query = params['search_term'] rescue nil
      redirect_to :root if @query.blank? && filters.empty?
    end

    def load_search
      begin
        @search = Search.search(@query, search_options, search_index)
      rescue => e
        Rails.logger.warn("error in search controller: #{e.message}")
        @search = nil
      end

      @main_filter = params[:main]
    end

    def search_options
      options = {filters: filters}
      options[:page] = params['requested_page'].to_i if params['requested_page'].present?
      options
    end

    def search_index
      # TODO Define mapping for index between FE and BE
      Search::DEFAULT_INDEX_NAME
    end

    AREA_TYPES = %w(wdpa oecm).freeze
    def check_area_type
      redirect_to :root unless AREA_TYPES.include?(params[:area_type].downcase)
    end

    def filters
      return '' unless params['filters'].present?
      JSON.parse(params['filters']).symbolize_keys.slice(*Search::ALLOWED_FILTERS)
    end
  end
end

Return empty string if filters do not exist
Return empty string if filters do not exist
