<commit_before># encoding: utf-8

module Carto
  module NamedMaps
    class Template
      NAMED_MAPS_VERSION = '0.0.1'.freeze
      NAME_PREFIX = 'tpl_'.freeze
      AUTH_TYPE_OPEN = 'open'.freeze
      AUTH_TYPE_SIGNED = 'token'.freeze
      EMPTY_CSS = '#dummy{}'.freeze

      TILER_WIDGET_TYPES = {
        'category': 'aggregation',
        'formula': 'formula',
        'histogram': 'histogram',
        'list': 'list',
        'time-series': 'histogram'
      }.freeze

      def initialize(visualization)
        raise 'Carto::NamedMaps::Template needs a Carto::Visualization' unless visualization.is_a?(Carto::Visualization)

        @visualization = visualization
      end

      def to_hash
        @template ||= stats_aggregator.timing('named-map.template-data') do
          {
            name: name,
            auth: auth,
            version: NAMED_MAPS_VERSION,
            placeholders: placeholders,
            layergroup: {
              layers: layers,
              stat_tag: @visualization.id,
              dataviews: dataviews,
              analyses: analyses
            },
            view: view
          }
        end
      end

      def to_json
        to_hash.to_json
      end

      def name
        (NAME_PREFIX + @visualization.id).gsub(/[^a-zA-Z0-9\-\_.]/, '').tr('-', '_')
      end

      private

      def placeholders
        placeholders = {}

        index = -1
        @visualization.map.named_maps_layers.select(&:data_layer?).each do |layer|
          index += 1
          placeholders["layer#{index}".to_sym] = {
            type: 'number',
            default: layer.options[:visible] ? 1 : 0
          }
        end

        @visualization.map.layers.select(&:torque?).each do |layer|
          index += 1
          placeholders["layer#{index}".to_sym] = {
            type: 'number',
            default: layer.options[:visible] ? 1 : 0
          }
        end

        placeholders
      end

      def layers
        layers = []
        index = -1 # forgive me for I have sinned

        @visualization.map.named_maps_layers.each do |layer|
          type, options = if layer.data_layer?
                            index += 1
                            type_and_options_for_cartodb_layers(layer, index)
                          elsif layer.basemap?
                            type_and_options_for_basemap_layers(layer)
                          end

          layers.push(type: type, options: options)
        end

        @visualization.map.layers.select(&:torque?).each do |layer|
          index += 1<commit_msg>reek refactor<commit_after># encoding: utf-8

module Carto
  module NamedMaps
    class Template
      NAMED_MAPS_VERSION = '0.0.1'.freeze
      NAME_PREFIX = 'tpl_'.freeze
      AUTH_TYPE_OPEN = 'open'.freeze
      AUTH_TYPE_SIGNED = 'token'.freeze
      EMPTY_CSS = '#dummy{}'.freeze

      TILER_WIDGET_TYPES = {
        'category': 'aggregation',
        'formula': 'formula',
        'histogram': 'histogram',
        'list': 'list',
        'time-series': 'histogram'
      }.freeze

      def initialize(visualization)
        raise 'Carto::NamedMaps::Template needs a Carto::Visualization' unless visualization.is_a?(Carto::Visualization)

        @visualization = visualization
      end

      def to_hash
        @template ||= stats_aggregator.timing('named-map.template-data') do
          {
            name: name,
            auth: auth,
            version: NAMED_MAPS_VERSION,
            placeholders: placeholders,
            layergroup: {
              layers: layers,
              stat_tag: @visualization.id,
              dataviews: dataviews,
              analyses: analyses
            },
            view: view
          }
        end
      end

      def to_json
        to_hash.to_json
      end

      def name
        (NAME_PREFIX + @visualization.id).gsub(/[^a-zA-Z0-9\-\_.]/, '').tr('-', '_')
      end

      private

      def placeholders
        placeholders = {}

        layers = @visualization.map.layers

        index = -1
        layers.select(&:data_layer?).each do |layer|
          index += 1
          placeholders["layer#{index}".to_sym] = {
            type: 'number',
            default: layer.options[:visible] ? 1 : 0
          }
        end

        layers.select(&:torque?).each do |layer|
          index += 1
          placeholders["layer#{index}".to_sym] = {
            type: 'number',
            default: layer.options[:visible] ? 1 : 0
          }
        end

        placeholders
      end

      def layers
        layers = []
        index = -1 # forgive me for I have sinned

        @visualization.map.named_maps_layers.each do |layer|
          type, options = if layer.data_layer?
                            index += 1
                            type_and_options_for_cartodb_layers(layer, index)
                          elsif layer.basemap?
                            type_and_options_for_basemap_layers(layer)
                          end

          layers.push(type: type, options: options)
        end

        @visualization.map.layers.select(&:torque?).each do |layer|
          index += 1
{'fraction_non_alphanumeric': 0.10608121235671265, 'fraction_numerical': 0.006217213911016126, 'mean_word_length': 1.5765765765765767, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5915310', 'n_tokens_mistral': 1554, 'n_tokens_neox': 1469, 'n_words': 300}