<commit_before>include FilestackRails::Transform
include FilestackRails::Version

module FilestackRails
  module ApplicationHelper
    def filestack_js_include_tag
      v2 = -> { javascript_include_tag('https://static.filestackapi.com/v3/filestack.js', type: 'text/javascript') }
      v3 = -> { javascript_include_tag('https://static.filestackapi.com/filestack-js/1.x.x/filestack.min.js', type: 'text/javascript') }

      load_filestack_js(v2: v2, v3: v3)
    end

    def filestack_js_init_tag
      client_name, apikey = get_client_and_api_key
      signature_and_policy = get_policy_and_signature_string
      javascript_string = "var #{client_name} = filestack.init('#{apikey}', #{signature_and_policy}, '#{cname}');"
      javascript_tag javascript_string
    end

    def filestack_picker_element(content, callback, options = {})
      picker_options = options[:pickerOptions]
      options.delete(:pickerOptions)
      options[:onclick] = create_javascript_for_picker(callback, picker_options)
      options[:type] = 'button'
      button_tag content, options
    end

    def filestack_transform
      _, apikey = get_client_and_api_key
      get_transform(apikey)
    end

    def filestack_image(url, options = {})
      transform_object = options[:transform]
      options.delete(:transform)
      if transform_object
        transform_object.add_external_url url
        image_tag transform_object.fs_url, options
      else
        image_tag url
      end
    end

    private

    def cname
      ::Rails.application.config.filestack_rails.cname
    end

    def create_javascript_for_picker(callback, options)
      client_name, = get_client_and_api_key
      json_string = if options.nil?
                      ''
                    else
                      options.to_json
                    end
      v2 = -> { "(function(){
        #{client_name}.pick(#{json_string}).then(function(data){#{callback}(data)})
      })()" }

      v3 = -> { json_string = "#{json_string}".tr('{}','')
                "(function(){
                  #{client_name}.picker({#{json_string}, onUploadDone: data => #{callback}(data)}).open()
                })()" }
      load_filestack_js(v2: v2, v3: v3)
    end

    def get_client_and_api_key
      client_name = ::Rails.application.config.filestack_rails.client_name
      apikey = ::Rails.application.config.filestack_rails.api_key
      [client_name, apikey]<commit_msg>Fixed pickerOptions<commit_after>include FilestackRails::Transform
include FilestackRails::Version

module FilestackRails
  module ApplicationHelper
    def filestack_js_include_tag
      v2 = -> { javascript_include_tag('https://static.filestackapi.com/v3/filestack.js', type: 'text/javascript') }
      v3 = -> { javascript_include_tag('https://static.filestackapi.com/filestack-js/1.x.x/filestack.min.js', type: 'text/javascript') }

      load_filestack_js(v2: v2, v3: v3)
    end

    def filestack_js_init_tag
      client_name, apikey = get_client_and_api_key
      signature_and_policy = get_policy_and_signature_string
      javascript_string = "var #{client_name} = filestack.init('#{apikey}', #{signature_and_policy}, '#{cname}');"
      javascript_tag javascript_string
    end

    def filestack_picker_element(content, callback, options = {})
      picker_options = options[:pickerOptions]
      options.delete(:pickerOptions)
      options[:onclick] = create_javascript_for_picker(callback, picker_options)
      options[:type] = 'button'
      button_tag content, options
    end

    def filestack_transform
      _, apikey = get_client_and_api_key
      get_transform(apikey)
    end

    def filestack_image(url, options = {})
      transform_object = options[:transform]
      options.delete(:transform)
      if transform_object
        transform_object.add_external_url url
        image_tag transform_object.fs_url, options
      else
        image_tag url
      end
    end

    private

    def cname
      ::Rails.application.config.filestack_rails.cname
    end

    def create_javascript_for_picker(callback, options)
      client_name, = get_client_and_api_key
      json_string = if options.nil?
                      ''
                    else
                      options.to_json
                    end
      v2 = -> { "(function(){
        #{client_name}.pick(#{json_string}).then(function(data){#{callback}(data)})
      })()" }

      v3 = -> { json_string = "#{json_string}".slice!(1, json_string.length-2) # removed curly brackets help to generate pickerOptions in js
                "(function(){
                  #{client_name}.picker({#{json_string}, onUploadDone: data => #{callback}(data)}).open()
                })()" }
      load_filestack_js(v2: v2, v3: v3)
    end

    def get_client_and_api_key
      client_name = ::Rails.application.config.filestack_rails.client_name
      apikey = ::Rails.application.config.filestack_rails.api_key
      [client_name, apikey]
{'fraction_non_alphanumeric': 0.10841423948220065, 'fraction_numerical': 0.006067961165048544, 'mean_word_length': 2.9214908802537667, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 4, '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': '3742313', 'n_tokens_mistral': 1573, 'n_tokens_neox': 1513, 'n_words': 248}