<commit_before>import cookies from 'js-cookie';

import addonActions from '../actions/addon';
import experimentActions from '../actions/experiments';


const RESTART_NEEDED = false; // TODO

const mam = navigator.mozAddonManager;

function mozAddonManagerInstall(url) {
  return mam.createInstall({ url }).then(install => {
    return new Promise((resolve, reject) => {
      install.addEventListener('onInstallEnded', () => resolve());
      install.addEventListener('onInstallFailed', () => reject());
      install.install();
    });
  });
}

export function installAddon(
  requireRestart,
  sendToGA,
  eventCategory,
  eventLabel
) {
  const { protocol, hostname, port } = window.location;
  const path = '/static/addon/addon.xpi';
  const downloadUrl = `${protocol}//${hostname}${port ? ':' + port : ''}${path}`;

  const gaEvent = {
    eventCategory: eventCategory,
    eventAction: 'install button click',
    eventLabel: eventLabel
  };

  cookies.set('first-run', 'true');

  return mozAddonManagerInstall(downloadUrl).then(() => {
    gaEvent.dimension7 = RESTART_NEEDED ? 'restart required' : 'no restart';
    sendToGA('event', gaEvent);
    if (RESTART_NEEDED) {
      requireRestart();
    }
  });
}

export function uninstallAddon() {
  mam.getAddonByID('@testpilot-addon').then(addon => {
    if (addon) {
      addon.uninstall();
    }
  });
}

export function setupAddonConnection(store) {
  mam.addEventListener('onEnabled', addon => {
    console.warn('onEnabled', addon);
    if (addon) {
      const { experiments } = store.getState();
      const i = experiments.data.map(x => x.addon_id).indexOf(addon.id);
      if (i > -1) {
        const x = experiments.data[i];
        store.dispatch(addonActions.enableExperiment(x));
        store.dispatch(
          experimentActions.updateExperiment(x.addon_id, {
            inProgress: false,
            error: false
          })
        );
      }
    }
  });
  mam.addEventListener('onDisabled', addon => {
    console.warn('onDisabled', addon);
    if (addon) {
      const { experiments } = store.getState();
      const i = experiments.data.map(x => x.addon_id).indexOf(addon.id);
      if (i > -1) {
        const x = experiments.data[i];
        store.dispatch(addonActions.disableExperiment(x));
        store.dispatch(
          experimentActions.updateExperiment(x.addon_id, {
            inProgress: false,
            error: false
          })
        );
      }
    }
  });
  mam.addEventListener('onEnabling', (addon, restart) => {
    console.warn('onEnabling', addon, restart);
  });
  mam.addEventListener('onDisabling', (addon, restart) => {
    console.warn('onDisabling', addon, restart);
  });
  mam.addEventListener('onInstalling', (addon, restart) => {
    console.warn('onInstalling', addon, restart);
  });
  mam.addEventListener('onInstalled', addon => {
    console.warn('onInstalled', addon);
  });
  mam.addEventListener('onUninstalling', (addon, restart) => {
    // TODO similar logic to txp addon AddonListener.js
    console.warn('onUninstalling', addon, restart);
  });
  mam.addEventListener('onOperationCancelled', addon => {
    // TODO similar logic to txp addon AddonListener.js
    console.warn('onOperationCancelled', addon);
  });
  mam.addEventListener('onPropertyChanged', (addon, p) => {
    console.warn('onPropertyChanged', addon, p);
  });
  getExperimentAddons(store.getState().experiments.data)
    .then(addons => {
      const enabled = addons.filter(a => a && a.isEnabled);
      const installed = {};
      enabled.forEach(a => {<commit_msg>Remove some console.warn, and comment out some event handler stubs that might be useful to implement some day<commit_after>import cookies from 'js-cookie';

import addonActions from '../actions/addon';
import experimentActions from '../actions/experiments';


const RESTART_NEEDED = false; // TODO

const mam = navigator.mozAddonManager;

function mozAddonManagerInstall(url) {
  return mam.createInstall({ url }).then(install => {
    return new Promise((resolve, reject) => {
      install.addEventListener('onInstallEnded', () => resolve());
      install.addEventListener('onInstallFailed', () => reject());
      install.install();
    });
  });
}

export function installAddon(
  requireRestart,
  sendToGA,
  eventCategory,
  eventLabel
) {
  const { protocol, hostname, port } = window.location;
  const path = '/static/addon/addon.xpi';
  const downloadUrl = `${protocol}//${hostname}${port ? ':' + port : ''}${path}`;

  const gaEvent = {
    eventCategory: eventCategory,
    eventAction: 'install button click',
    eventLabel: eventLabel
  };

  cookies.set('first-run', 'true');

  return mozAddonManagerInstall(downloadUrl).then(() => {
    gaEvent.dimension7 = RESTART_NEEDED ? 'restart required' : 'no restart';
    sendToGA('event', gaEvent);
    if (RESTART_NEEDED) {
      requireRestart();
    }
  });
}

export function uninstallAddon() {
  mam.getAddonByID('@testpilot-addon').then(addon => {
    if (addon) {
      addon.uninstall();
    }
  });
}

export function setupAddonConnection(store) {
  mam.addEventListener('onEnabled', addon => {
    if (addon) {
      const { experiments } = store.getState();
      const i = experiments.data.map(x => x.addon_id).indexOf(addon.id);
      if (i > -1) {
        const x = experiments.data[i];
        store.dispatch(addonActions.enableExperiment(x));
        store.dispatch(
          experimentActions.updateExperiment(x.addon_id, {
            inProgress: false,
            error: false
          })
        );
      }
    }
  });
  mam.addEventListener('onDisabled', addon => {
    if (addon) {
      const { experiments } = store.getState();
      const i = experiments.data.map(x => x.addon_id).indexOf(addon.id);
      if (i > -1) {
        const x = experiments.data[i];
        store.dispatch(addonActions.disableExperiment(x));
        store.dispatch(
          experimentActions.updateExperiment(x.addon_id, {
            inProgress: false,
            error: false
          })
        );
      }
    }
  });
/*
  mam.addEventListener('onEnabling', (addon, restart) => {
  });
  mam.addEventListener('onDisabling', (addon, restart) => {
  });
  mam.addEventListener('onInstalling', (addon, restart) => {
  });
  mam.addEventListener('onInstalled', addon => {
  });
  mam.addEventListener('onUninstalling', (addon, restart) => {
    // TODO similar logic to txp addon AddonListener.js
  });
  mam.addEventListener('onOperationCancelled', addon => {
    // TODO similar logic to txp addon AddonListener.js
  });
  mam.addEventListener('onPropertyChanged', (addon, p) => {
  });
*/
  getExperimentAddons(store.getState().experiments.data)
    .then(addons => {
      const enabled = addons.filter(a => a && a.isEnabled);
      const installed = {};
      enabled.forEach(a => {
{'fraction_non_alphanumeric': 0.15181711606096132, 'fraction_numerical': 0.0008792497069167644, 'mean_word_length': 3.1743119266055047, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 47, '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': '6408811', 'n_tokens_mistral': 2126, 'n_tokens_neox': 2121, 'n_words': 400}