<commit_before>import "./application.styl"
import template from "./application.pug"

import autobind from "autobind-decorator"
import BaseComponent from "components/base-component"
import {highlight} from "highlight.js"
import uniqueID from "lib/unique-id"
import {postJson} from "simple-fetch"

const ENTITY_ID = "data-entity-id"

export default class Application extends BaseComponent {
  static template = template;

  constructor(mountPoint, options) {
    super(options)

    Object.assign(this, {
      entities: null,
      parsedEmbedCode: null
    })

    const element = this.compileTemplate()
    const {stepsContainer, embedCodeInput, createPluginButton, nextButton, previousButton} = this.refs

    embedCodeInput.addEventListener("input", this.handleEntry)
    mountPoint.appendChild(element)

    // TODO: Remove after testing is done.
    embedCodeInput.value = `<script>
  (function(){
  var handle = '@placeholder';
  var a = document.createElement('script');
  var m = document.getElementsByTagName('script')[0];
  a.async = 1;
  a.src = 'https://nectar.ninja/api/v1/' + handle.slice(1);
  m.parentNode.insertBefore(a, m);
  })();
</script>`
    embedCodeInput.value = ""
    this.parseInput()

    createPluginButton.addEventListener("click", this.createPlugin)
    nextButton.addEventListener("click", () => stepsContainer.setAttribute("data-active-step", "attribute"))
    previousButton.addEventListener("click", () => {
      stepsContainer.setAttribute("data-active-step", "embed-code")
      embedCodeInput.select()
      embedCodeInput.focus()
    })
  }

  @autobind
  createPlugin() {
    const trackedIDs = this.getTrackedEntitiesIDs()
    const embedCodeDOM = this.refs.attributePicker.cloneNode(true)

    trackedIDs.forEach(id => {
      const current = embedCodeDOM.querySelector(`[${ENTITY_ID}="${id}"]`)
      const schemaFiller = document.createTextNode(`OPTIONS["${id}"]`)

      current.parentNode.insertBefore(schemaFiller, current)
      current.parentNode.removeChild(current)
    })

    const embedCode = embedCodeDOM.textContent

    console.log(embedCode)

    // TODO: flesh out
    postJson(`${API_BASE}/instant-plugin`, {embedCode})<commit_msg>Enable test code.<commit_after>import "./application.styl"
import template from "./application.pug"

import autobind from "autobind-decorator"
import BaseComponent from "components/base-component"
import {highlight} from "highlight.js"
import uniqueID from "lib/unique-id"
import {postJson} from "simple-fetch"

const ENTITY_ID = "data-entity-id"

export default class Application extends BaseComponent {
  static template = template;

  constructor(mountPoint, options) {
    super(options)

    Object.assign(this, {
      entities: null,
      parsedEmbedCode: null
    })

    const element = this.compileTemplate()
    const {stepsContainer, embedCodeInput, createPluginButton, nextButton, previousButton} = this.refs

    embedCodeInput.addEventListener("input", this.handleEntry)
    mountPoint.appendChild(element)

    // TODO: Remove after testing is done.
    embedCodeInput.value = `<script>
  (function(){
  var handle = '@placeholder';
  var a = document.createElement('script');
  var m = document.getElementsByTagName('script')[0];
  a.async = 1;
  a.src = 'https://nectar.ninja/api/v1/' + handle.slice(1);
  m.parentNode.insertBefore(a, m);
  })();
</script>`
    // embedCodeInput.value = ""
    this.parseInput()

    createPluginButton.addEventListener("click", this.createPlugin)
    nextButton.addEventListener("click", () => stepsContainer.setAttribute("data-active-step", "attribute"))
    previousButton.addEventListener("click", () => {
      stepsContainer.setAttribute("data-active-step", "embed-code")
      embedCodeInput.select()
      embedCodeInput.focus()
    })
  }

  @autobind
  createPlugin() {
    const trackedIDs = this.getTrackedEntitiesIDs()
    const embedCodeDOM = this.refs.attributePicker.cloneNode(true)

    trackedIDs.forEach(id => {
      const current = embedCodeDOM.querySelector(`[${ENTITY_ID}="${id}"]`)
      const schemaFiller = document.createTextNode(`OPTIONS["${id}"]`)

      current.parentNode.insertBefore(schemaFiller, current)
      current.parentNode.removeChild(current)
    })

    const embedCode = embedCodeDOM.textContent

    console.log(embedCode)

    // TODO: flesh out
    postJson(`${API_BASE}/instant-plugin`, {embedCode})
{'fraction_non_alphanumeric': 0.1331664010926474, 'fraction_numerical': 0.001821078989301161, 'mean_word_length': 4.224732461355529, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 2, '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': '2345741', 'n_tokens_mistral': 1298, 'n_tokens_neox': 1271, 'n_words': 249}