<commit_before>import Integration from './../Integration';
import deleteProperty from './../functions/deleteProperty';
import { getProp } from './../functions/dotProp';
import each from './../functions/each';
import after from './../functions/after';
import {
  VIEWED_PAGE,
  SUBSCRIBED,
} from './../events';
import { DIGITALDATA_VAR } from './../variableTypes';

function isHttps() {
  return (window.location.href.indexOf('https:') >= 0);
}

function clickListenerWorkaround() {
  function getDialogBody() {
    return document.querySelector('.onesignal-bell-launcher-dialog-body');
  }

  function getSubscribeButton() {
    return document.querySelector('#onesignal-bell-container .onesignal-bell-launcher #subscribe-button');
  }

  function getUnsubscribeButton() {
    return document.querySelector('#onesignal-bell-container .onesignal-bell-launcher #unsubscribe-button');
  }

  function onSubscribeButtonClicked() {
    window.OneSignal.event.trigger('notifyButtonSubscribeClick');
  }

  function onUnsubscribeButtonClicked() {
    window.OneSignal.event.trigger('notifyButtonUnsubscribeClick');
  }

  function onDialogBodyModified() {
    if (getSubscribeButton()) {
      getSubscribeButton().removeEventListener('click', onSubscribeButtonClicked);
      getSubscribeButton().addEventListener('click', onSubscribeButtonClicked);
    }
    if (getUnsubscribeButton()) {
      getUnsubscribeButton().removeEventListener('click', onUnsubscribeButtonClicked);
      getUnsubscribeButton().addEventListener('click', onUnsubscribeButtonClicked);
    }
  }

  function hookTreeModified() {
    if (getDialogBody()) {
      getDialogBody().addEventListener('DOMSubtreeModified', onDialogBodyModified);
    }
  }

  window.OneSignal = window.OneSignal || [];
  window.OneSignal.push(() => {
    window.OneSignal.once('notifyButtonLauncherClick', () => {
      hookTreeModified();
    });
  });
}

class OneSignal extends Integration {

  constructor(digitalData, options) {
    const optionsWithDefaults = Object.assign({
      appId: '',
      autoRegister: false,
      subdomainName: undefined,
      path: '/',
      safariWebId: undefined,
      pushSubscriptionTriggerEvent: 'Agreed to Receive Push Notifications',
      tagVars: {},
      welcomeNotification: {
        disable: true,
      },
    }, options);

    super(digitalData, optionsWithDefaults);

    this.userTags = {}; // not to conflict with this.tags named it userTags
    this.enrichableTagProps = [];

    this.SEMANTIC_EVENTS = [
      VIEWED_PAGE,
      SUBSCRIBED,
      this.getOption('pushSubscriptionTriggerEvent'),
    ];

    this.addTag('manifest', {
      type: 'link',
      attr: {
        rel: 'manifest',
        href: optionsWithDefaults.path.replace(/\/$/, '') + '/manifest.json',
      },
    });
    this.addTag({
      type: 'script',
      attr: {
        src: 'https://cdn.onesignal.com/sdks/OneSignalSDK.js',
      },
    });
  }

  getSemanticEvents() {
    return this.SEMANTIC_EVENTS;
  }

  allowCustomEvents() {
    return false;
  }

  getEnrichableEventProps(event) {
    const enrichableProps = ['user.email'];

    if (this.SEMANTIC_EVENTS.indexOf(event.name) >= 0) {
      const enrichableTagProps = this.getEnrichableTagProps();
      for (const enrichableTagProp of enrichableTagProps) {
        enrichableProps.push(enrichableTagProp);
      }
    }

    return enrichableProps;
  }

  getEnrichableTagProps() {
    return this.enrichableTagProps;
  }

  prepareEnrichableTagProps() {
    const tagsSettings = this.getOption('tagVars');
    each(tagsSettings, (key, variable) => {
      if (variable.type === DIGITALDATA_VAR) {
        this.enrichableTagProps.push(variable.value);
      }
    });
  }

  initialize() {
    window.OneSignal = window.OneSignal || [];

    if (this.getOption('notifyButton') && this.getOption('notifyButton').displayPredicate) {
      try {
        this.getOption('notifyButton').displayPredicate =
          Function(this.getOption('notifyButton').displayPredicate); // eslint-disable-line no-new-func
      } catch (e) {
        deleteProperty(this.getOption('notifyButton'), 'displayPredicate');
      }
    }

    if (this.getOption('notifyButton') && this.getOption('notifyButton').enable) {
      clickListenerWorkaround(); // temporary fix of notify bell click listener
    }

    window.OneSignal.push(['init', {
      appId: this.getOption('appId'),
      autoRegister: this.getOption('autoRegister'),
      subdomainName: this.getOption('subdomainName'),
      path: this.getOption('path'),
      safari_web_id: this.getOption('safariWebId'),
      promptOptions: this.getOption('promptOptions'),
      notifyButton: this.getOption('notifyButton'),
      welcomeNotification: this.getOption('welcomeNotification'),
    }]);

    window.OneSignal.push(['getRegistrationId', (registrationId) => {
      if (registrationId) {
        window.OneSignal.push(['getTags', (tags) => {
          this.currentTags = tags;
          this.emit('getTags');
        }]);
      } else {
        this.currentTags = {};
        this.emit('getTags');
      }
    }]);

    this.enrichDigitalData();
    this.prepareEnrichableTagProps();

    const loaded = after((isHttps() ? 2 : 1), this.onLoad);
    if (isHttps()) {
      this.load('manifest', loaded);
    }
    this.load(loaded);
  }

  onGetTags(fn) {
    if (this.currentTags) {
      fn(this.currentTags);
    } else {
      this.on('getTags', () => {
        fn(this.currentTags);
      });
    }
  }

  extractTagValuesFromEvent(event) {
    const tagsSettings = this.getOption('tagVars');
    const newTags = {};
    each(tagsSettings, (key, variable) => {
      let tagVal = getProp(event, variable.value);
      if (tagVal !== undefined) {
        if (typeof tagVal === 'boolean') tagVal = tagVal.toString();
        newTags[key] = tagVal;
      }<commit_msg>OneSignal https/http hotfix<commit_after>import Integration from './../Integration';
import deleteProperty from './../functions/deleteProperty';
import { getProp } from './../functions/dotProp';
import each from './../functions/each';
import after from './../functions/after';
import {
  VIEWED_PAGE,
  SUBSCRIBED,
} from './../events';
import { DIGITALDATA_VAR } from './../variableTypes';

function isHttps() {
  return (window.location.href.indexOf('https:') >= 0);
}

function clickListenerWorkaround() {
  function getDialogBody() {
    return document.querySelector('.onesignal-bell-launcher-dialog-body');
  }

  function getSubscribeButton() {
    return document.querySelector('#onesignal-bell-container .onesignal-bell-launcher #subscribe-button');
  }

  function getUnsubscribeButton() {
    return document.querySelector('#onesignal-bell-container .onesignal-bell-launcher #unsubscribe-button');
  }

  function onSubscribeButtonClicked() {
    window.OneSignal.event.trigger('notifyButtonSubscribeClick');
  }

  function onUnsubscribeButtonClicked() {
    window.OneSignal.event.trigger('notifyButtonUnsubscribeClick');
  }

  function onDialogBodyModified() {
    if (getSubscribeButton()) {
      getSubscribeButton().removeEventListener('click', onSubscribeButtonClicked);
      getSubscribeButton().addEventListener('click', onSubscribeButtonClicked);
    }
    if (getUnsubscribeButton()) {
      getUnsubscribeButton().removeEventListener('click', onUnsubscribeButtonClicked);
      getUnsubscribeButton().addEventListener('click', onUnsubscribeButtonClicked);
    }
  }

  function hookTreeModified() {
    if (getDialogBody()) {
      getDialogBody().addEventListener('DOMSubtreeModified', onDialogBodyModified);
    }
  }

  window.OneSignal = window.OneSignal || [];
  window.OneSignal.push(() => {
    window.OneSignal.once('notifyButtonLauncherClick', () => {
      hookTreeModified();
    });
  });
}

class OneSignal extends Integration {

  constructor(digitalData, options) {
    const optionsWithDefaults = Object.assign({
      appId: '',
      autoRegister: false,
      subdomainName: undefined,
      path: '/',
      safariWebId: undefined,
      pushSubscriptionTriggerEvent: 'Agreed to Receive Push Notifications',
      tagVars: {},
      welcomeNotification: {
        disable: true,
      },
    }, options);

    super(digitalData, optionsWithDefaults);

    this.userTags = {}; // not to conflict with this.tags named it userTags
    this.enrichableTagProps = [];

    this.SEMANTIC_EVENTS = [
      VIEWED_PAGE,
      SUBSCRIBED,
      this.getOption('pushSubscriptionTriggerEvent'),
    ];

    this.addTag('manifest', {
      type: 'link',
      attr: {
        rel: 'manifest',
        href: optionsWithDefaults.path.replace(/\/$/, '') + '/manifest.json',
      },
    });
    this.addTag({
      type: 'script',
      attr: {
        src: 'https://cdn.onesignal.com/sdks/OneSignalSDK.js',
      },
    });
  }

  getSemanticEvents() {
    if (this.initialized) {
      return this.SEMANTIC_EVENTS;
    }
    return [];
  }

  allowCustomEvents() {
    return false;
  }

  getEnrichableEventProps(event) {
    const enrichableProps = ['user.email'];

    if (this.SEMANTIC_EVENTS.indexOf(event.name) >= 0) {
      const enrichableTagProps = this.getEnrichableTagProps();
      for (const enrichableTagProp of enrichableTagProps) {
        enrichableProps.push(enrichableTagProp);
      }
    }

    return enrichableProps;
  }

  getEnrichableTagProps() {
    return this.enrichableTagProps;
  }

  prepareEnrichableTagProps() {
    const tagsSettings = this.getOption('tagVars');
    each(tagsSettings, (key, variable) => {
      if (variable.type === DIGITALDATA_VAR) {
        this.enrichableTagProps.push(variable.value);
      }
    });
  }

  initialize() {
    if (!this.getOption('subdomainName') && !isHttps()) {
      return;
    }

    window.OneSignal = window.OneSignal || [];

    if (this.getOption('notifyButton') && this.getOption('notifyButton').displayPredicate) {
      try {
        this.getOption('notifyButton').displayPredicate =
          Function(this.getOption('notifyButton').displayPredicate); // eslint-disable-line no-new-func
      } catch (e) {
        deleteProperty(this.getOption('notifyButton'), 'displayPredicate');
      }
    }

    if (this.getOption('notifyButton') && this.getOption('notifyButton').enable) {
      clickListenerWorkaround(); // temporary fix of notify bell click listener
    }

    window.OneSignal.push(['init', {
      appId: this.getOption('appId'),
      autoRegister: this.getOption('autoRegister'),
      subdomainName: this.getOption('subdomainName'),
      path: this.getOption('path'),
      safari_web_id: this.getOption('safariWebId'),
      promptOptions: this.getOption('promptOptions'),
      notifyButton: this.getOption('notifyButton'),
      welcomeNotification: this.getOption('welcomeNotification'),
    }]);

    window.OneSignal.push(['getRegistrationId', (registrationId) => {
      if (registrationId) {
        window.OneSignal.push(['getTags', (tags) => {
          this.currentTags = tags;
          this.emit('getTags');
        }]);
      } else {
        this.currentTags = {};
        this.emit('getTags');
      }
    }]);

    this.enrichDigitalData();
    this.prepareEnrichableTagProps();

    const loaded = after((isHttps() ? 2 : 1), this.onLoad);
    if (isHttps()) {
      this.load('manifest', loaded);
    }
    this.load(loaded);
    this.initialized = true;
  }

  onGetTags(fn) {
    if (this.currentTags) {
      fn(this.currentTags);
    } else {
      this.on('getTags', () => {
        fn(this.currentTags);
      });
    }
  }

  extractTagValuesFromEvent(event) {
    const tagsSettings = this.getOption('tagVars');
    const newTags = {};
    each(tagsSettings, (key, variable) => {
      let tagVal = getProp(event, variable.value);
      if (tagVal !== undefined) {
        if (typeof tagVal === 'boolean') tagVal = tagVal.toString();
        newTags[key] = tagVal;
      }
{'fraction_non_alphanumeric': 0.14322938555938472, 'fraction_numerical': 0.0006724384298562663, 'mean_word_length': 3.2221433640880055, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '4307013', 'n_tokens_mistral': 3606, 'n_tokens_neox': 3497, 'n_words': 569}