text: import { noop } from './util';
  import { getStorage } from './storage';
  
  function getBelterExperimentStorage() {
    return getStorage({
      name: 'belter_experiment'
    });
  }
  
  function isEventUnique(name) {
    return getBelterExperimentStorage().getSessionState(function (state) {
      state.loggedBeacons = state.loggedBeacons || [];
  
      if (state.loggedBeacons.indexOf(name) === -1) {
        state.loggedBeacons.push(name);
        return true;
      }
  
      return false;
    });
  }
  
  function getRandomInteger(range) {
    return Math.floor(Math.random() * range);
  }
  
  function getThrottlePercentile(name) {
    return getBelterExperimentStorage().getState(function (state) {
      state.throttlePercentiles = state.throttlePercentiles || {};
      state.throttlePercentiles[name] = state.throttlePercentiles[name] || getRandomInteger(100);
      return state.throttlePercentiles[name];
    });
  }
  
  var THROTTLE_GROUP = {
    TEST: 'test',
    CONTROL: 'control',
    THROTTLE: 'throttle'
  };
  export function experiment(_ref) {
    var name = _ref.name,
        _ref$sample = _ref.sample,
        sample = _ref$sample === void 0 ? 50 : _ref$sample,
        _ref$logTreatment = _ref.logTreatment,
        logTreatment = _ref$logTreatment === void 0 ? noop : _ref$logTreatment,
        _ref$logCheckpoint = _ref.logCheckpoint,
        logCheckpoint = _ref$logCheckpoint === void 0 ? noop : _ref$logCheckpoint,
        _ref$sticky = _ref.sticky,
        sticky = _ref$sticky === void 0 ? true : _ref$sticky;
    var throttle = sticky ? getThrottlePercentile(name) : getRandomInteger(100);
    var group;
  
    if (throttle < sample && !__TEST__) {
      group = THROTTLE_GROUP.TEST;
    } else if (sample >= 50 || sample <= throttle && throttle < sample * 2) {
      group = THROTTLE_GROUP.CONTROL;
    } else {
      group = THROTTLE_GROUP.THROTTLE;
    }
  
    var treatment = name + "_" + group;
    var started = false;
    var forced = false;
  
    try {
      if (window.localStorage && window.localStorage.getItem(name)) {
        forced = true;
      }
    } catch (err) {// pass
    }
  
    var exp = {
      isEnabled: function isEnabled() {
        return group === THROTTLE_GROUP.TEST || forced;
      },
      isDisabled: function isDisabled() {
        return group !== THROTTLE_GROUP.TEST && !forced;
      },
      getTreatment: function getTreatment() {
        return treatment;
      },
      log: function log(checkpoint, payload) {
        if (payload === void 0) {
          payload = {};
        }
  
        if (!started) {
          return exp;
        }
  
        if (isEventUnique(treatment + "_" + JSON.stringify(payload))) {
          logTreatment({
            name: name,
            treatment: treatment,
            payload: payload,
            throttle: throttle
          });
        }
  
        if (isEventUnique(treatment + "_" + checkpoint + "_" + JSON.stringify(payload))) {
          logCheckpoint({
            name: name,
            treatment: treatment,
            checkpoint: checkpoint,
            payload: payload,
            throttle: throttle
          });
        }
  
        return exp;
      },
      logStart: function logStart(payload) {
        if (payload === void 0) {
          payload = {};
        }
  
        started = true;
        return exp.log("start", payload);
      },
      logComplete: function logComplete(payload) {
        if (payload === void 0) {
          payload = {};
        }
  
        return exp.log("complete", payload);
      }
    };
    return exp;
  }