<commit_before>Handlebars = require('handlebars')
_ = require('underscore')

Utils = require('./utils.coffee')
Clients = require('./clients.coffee')
Panel = require('./panel.coffee')

class ClientsPanel extends Panel
  constructor: (@container, @parent) ->
    @podcast = @parent.podcast
    @platform = @parent.platform
    @clients = new Clients(@platform)
    @osDefault = new Clients(@platform, true)
    @cloudClients = new Clients('cloud')
    if @prepareClients(@parent.options.scriptPath)
      @render()
    else
      text = 'No usable feed found. Please add at least one feed.'
      console.warn(text)

  context: -> {
    cover: @podcast.cover,
    title: @podcast.title,
    subtitle: @podcast.subtitle,
    clients: @clients,
    platform: @platform,
    otherClient: @otherClient,
    cloudClients: @cloudClients,
    osDefault: @osDefault,
    scriptPath: @parent.options.scriptPath,
    podcastTitle: @podcast.title,
    podcastSubtitle: @podcast.subtitle,
    podcastCover: @podcast.cover,
  }

  detectBestFormat: () ->
    capabilities = if @platform == 'android'
      ['mp3', 'aac', 'ogg', 'opus']
    else
      ['aac', 'mp3', 'ogg', 'opus']

    _(capabilities).find (cap) =>
      _(@podcast.feeds).findWhere({format: cap})

  chooseFeed: () ->
    format = @detectBestFormat()
    feed = _(@podcast.feeds).findWhere({format: format})

  findCustomFeed: (type) =>
    (_(@podcast.feeds).findWhere({type: type}) || {}).url

  prepareClients: (pathPrefix) ->
    feed = @chooseFeed() || {}
    feedUrlWithOutHttp = feed.url.replace(/^(http|https):\/\//, '')
    return false unless feed.url

    for client in @clients
      Utils.fixIconPath(client, pathPrefix)

      standardUrl = "#{client.scheme}#{feedUrlWithOutHttp}"
      client.url = if type = client.customFeedType
        if customUrl = feed["directory-url-#{type}"]
          customUrl
        else
          standardUrl
      else
        standardUrl


    _(@clients).shuffle()

    for client in @cloudClients
      Utils.fixIconPath(client, pathPrefix)
      cloudFeedUrl = if client.http then feed.url else feedUrlWithOutHttp
      if client.post
        client.url = client.scheme
        client.feedUrl = cloudFeedUrl
      else
        client.url = "#{client.scheme}#{cloudFeedUrl}"

    _(@cloudClients).shuffle()

    Utils.fixIconPath(@osDefault, pathPrefix)
    @osDefault.title = @parent.I18n.t('clients_panel.let_device_decide')
    @osDefault.originalUrl = feed.url
    unless @osDefault.scheme == null
      @osDefault.url = "#{@osDefault.scheme}#{feedUrlWithOutHttp}"
      # necessary for displaying the right finish panel content
      @osDefault.scheme = null

    @otherClient = new Clients('rss')
    Utils.fixIconPath(@otherClient, pathPrefix)
    @otherClient.originalUrl = feed.url

  render: () ->
    html = @template(@context())
    @container.insertAdjacentHTML('beforeend', html)
    @elem = @container.querySelector('.podlove-subscribe-button-clients-panel')

    items = @elem.querySelectorAll('li a')
    Array.prototype.forEach.call items, (item) =><commit_msg>Encode feed URLs when client scheme includes query arguments<commit_after>Handlebars = require('handlebars')
_ = require('underscore')

Utils = require('./utils.coffee')
Clients = require('./clients.coffee')
Panel = require('./panel.coffee')

class ClientsPanel extends Panel
  constructor: (@container, @parent) ->
    @podcast = @parent.podcast
    @platform = @parent.platform
    @clients = new Clients(@platform)
    @osDefault = new Clients(@platform, true)
    @cloudClients = new Clients('cloud')
    if @prepareClients(@parent.options.scriptPath)
      @render()
    else
      text = 'No usable feed found. Please add at least one feed.'
      console.warn(text)

  context: -> {
    cover: @podcast.cover,
    title: @podcast.title,
    subtitle: @podcast.subtitle,
    clients: @clients,
    platform: @platform,
    otherClient: @otherClient,
    cloudClients: @cloudClients,
    osDefault: @osDefault,
    scriptPath: @parent.options.scriptPath,
    podcastTitle: @podcast.title,
    podcastSubtitle: @podcast.subtitle,
    podcastCover: @podcast.cover,
  }

  detectBestFormat: () ->
    capabilities = if @platform == 'android'
      ['mp3', 'aac', 'ogg', 'opus']
    else
      ['aac', 'mp3', 'ogg', 'opus']

    _(capabilities).find (cap) =>
      _(@podcast.feeds).findWhere({format: cap})

  chooseFeed: () ->
    format = @detectBestFormat()
    feed = _(@podcast.feeds).findWhere({format: format})

  findCustomFeed: (type) =>
    (_(@podcast.feeds).findWhere({type: type}) || {}).url

  prepareClients: (pathPrefix) ->
    feed = @chooseFeed() || {}
    return false unless feed.url
    feedUrlWithOutHttp = feed.url.replace(/^(http|https):\/\//, '')

    for client in @clients
      Utils.fixIconPath(client, pathPrefix)

      standardUrl = if -1 == client.scheme.indexOf('?')
        "#{client.scheme}#{feedUrlWithOutHttp}"
      else
        "#{client.scheme}#{encodeURIComponent(feedUrlWithOutHttp)}"

      client.url = if type = client.customFeedType
        if customUrl = feed["directory-url-#{type}"]
          customUrl
        else
          standardUrl
      else
        standardUrl


    _(@clients).shuffle()

    for client in @cloudClients
      Utils.fixIconPath(client, pathPrefix)
      cloudFeedUrl = if client.http then feed.url else feedUrlWithOutHttp
      cloudFeedUrl = if -1 == client.scheme.indexOf('?')
        cloudFeedUrl
      else
        encodeURIComponent(cloudFeedUrl)

      if client.post
        client.url = client.scheme
        client.feedUrl = cloudFeedUrl
      else
        client.url = "#{client.scheme}#{cloudFeedUrl}"

    _(@cloudClients).shuffle()

    Utils.fixIconPath(@osDefault, pathPrefix)
    @osDefault.title = @parent.I18n.t('clients_panel.let_device_decide')
    @osDefault.originalUrl = feed.url
    unless @osDefault.scheme == null
      @osDefault.url = "#{@osDefault.scheme}#{feedUrlWithOutHttp}"
      # necessary for displaying the right finish panel content
      @osDefault.scheme = null

    @otherClient = new Clients('rss')
    Utils.fixIconPath(@otherClient, pathPrefix)
    @otherClient.originalUrl = feed.url

  render: () ->
    html = @template(@context())
    @container.insertAdjacentHTML('beforeend', html)
    @elem = @container.querySelector('.podlove-subscribe-button-clients-panel')

    items = @elem.querySelectorAll('li a')
    Array.prototype.forEach.call items, (item) =>
{'fraction_non_alphanumeric': 0.1395420792079208, 'fraction_numerical': 0.001547029702970297, 'mean_word_length': 3.2560895325872283, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3080422', 'n_tokens_mistral': 2027, 'n_tokens_neox': 1988, 'n_words': 410}