<commit_before>@Infeedl ||= {}

class @Infeedl.Creative
  constructor: (@format, @creative, @placement) ->

  render: ->
    InfeedlTemplates[@format.replace("embedded_", "")].render(@_interpolations())

  click: (evt) ->
    if @creative.content.embedded
      @_append_embedded()
    else
      @_open_content()

  _open_content: ->
    window.open(@creative.content.location)

  _append_embedded: ->
    @loader = document.createElement("div")
    @loader.setAttribute("class", "infeedl--embedded--loader")
    Infeedl.$("body").addClass("infeedl--no-scroll")
    Infeedl.$("body")[0].appendChild(@loader)

    @wrapper = document.createElement("div")
    @wrapper.setAttribute("class", "infeedl--embedded-wrapper")

    @iframe = document.createElement("iframe")
    @iframe.setAttribute("src", @creative.content.embedded_location)
    @iframe.setAttribute("frameBorder", "0")
    @iframe.setAttribute("allowtransparency", "true")
    @iframe.setAttribute("class", "infeedl--embedded")

    Infeedl.$("body")[0].appendChild(@wrapper)
    Infeedl.$(".infeedl--embedded-wrapper")[0].appendChild(@iframe)
    # console?.log("[INFEEDL] Creative ##{@creative.id}: embedded added")

    talker = new Talker(@iframe.contentWindow, "*")
    talker.onMessage = (message) =>
      if message.namespace == "infeedl"
        if message.data.state == "loaded"
          @_remove_loader()
        if message.data.state == "closed"
          @_remove_embedded()

    setTimeout(=>
      if @loader
        @_remove_loader()
        @_remove_embedded()
    , 15000)

  _remove_loader: ->
    @loader = null
    Infeedl.$(".infeedl--embedded--loader").remove()

    @close = document.createElement("div")
    @close.setAttribute("class", "infeedl--embedded-close")
    Infeedl.$("body")[0].appendChild(@close)
    Infeedl.$(".infeedl--embedded-close").css(top: Infeedl.$(window).scrollTop() + 30)

    Infeedl.$(document).on("click", ".infeedl--embedded-close", @_remove_embedded)
    # console?.log("[INFEEDL] Creative ##{@creative.id}: loader removed")

  _remove_embedded: ->
    Infeedl.$(document).off("click", ".infeedl--embedded-close", @_remove_embedded)

    @close = null
    @wrapper = null
    @iframe = null

    Infeedl.$(".infeedl--embedded-close").remove()
    Infeedl.$(".infeedl--embedded-wrapper").remove()
    Infeedl.$("body").removeClass("infeedl--no-scroll")
    # console?.log("[INFEEDL] Creative ##{@creative.id}: embedded removed")

  _interpolations: ->
    creative: @creative
    placement: @placement<commit_msg>Add infeedl--no-scroll to html as well<commit_after>@Infeedl ||= {}

class @Infeedl.Creative
  constructor: (@format, @creative, @placement) ->

  render: ->
    InfeedlTemplates[@format.replace("embedded_", "")].render(@_interpolations())

  click: (evt) ->
    if @creative.content.embedded
      @_append_embedded()
    else
      @_open_content()

  _open_content: ->
    window.open(@creative.content.location)

  _append_embedded: ->
    @loader = document.createElement("div")
    @loader.setAttribute("class", "infeedl--embedded--loader")
    Infeedl.$("html, body").addClass("infeedl--no-scroll")
    Infeedl.$("body")[0].appendChild(@loader)

    @wrapper = document.createElement("div")
    @wrapper.setAttribute("class", "infeedl--embedded-wrapper")

    @iframe = document.createElement("iframe")
    @iframe.setAttribute("src", @creative.content.embedded_location)
    @iframe.setAttribute("frameBorder", "0")
    @iframe.setAttribute("allowtransparency", "true")
    @iframe.setAttribute("class", "infeedl--embedded")

    Infeedl.$("body")[0].appendChild(@wrapper)
    Infeedl.$(".infeedl--embedded-wrapper")[0].appendChild(@iframe)
    # console?.log("[INFEEDL] Creative ##{@creative.id}: embedded added")

    talker = new Talker(@iframe.contentWindow, "*")
    talker.onMessage = (message) =>
      if message.namespace == "infeedl"
        if message.data.state == "loaded"
          @_remove_loader()
        if message.data.state == "closed"
          @_remove_embedded()

    setTimeout(=>
      if @loader
        @_remove_loader()
        @_remove_embedded()
    , 15000)

  _remove_loader: ->
    @loader = null
    Infeedl.$(".infeedl--embedded--loader").remove()

    @close = document.createElement("div")
    @close.setAttribute("class", "infeedl--embedded-close")
    Infeedl.$("body")[0].appendChild(@close)
    Infeedl.$(".infeedl--embedded-close").css(top: Infeedl.$(window).scrollTop() + 30)

    Infeedl.$(document).on("click", ".infeedl--embedded-close", @_remove_embedded)
    # console?.log("[INFEEDL] Creative ##{@creative.id}: loader removed")

  _remove_embedded: ->
    Infeedl.$(document).off("click", ".infeedl--embedded-close", @_remove_embedded)

    @close = null
    @wrapper = null
    @iframe = null

    Infeedl.$(".infeedl--embedded-close").remove()
    Infeedl.$(".infeedl--embedded-wrapper").remove()
    Infeedl.$("html, body").removeClass("infeedl--no-scroll")
    # console?.log("[INFEEDL] Creative ##{@creative.id}: embedded removed")

  _interpolations: ->
    creative: @creative
    placement: @placement
{'fraction_non_alphanumeric': 0.1846908734052993, 'fraction_numerical': 0.004710500490677135, 'mean_word_length': 3.8030160226201697, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '6301837', 'n_tokens_mistral': 1692, 'n_tokens_neox': 1617, 'n_words': 230}