/**!
 *
 * Copyright (c) 2015-2016 Cisco Systems, Inc. See LICENSE file.
 * @private
 */

import {SparkPlugin} from '@ciscospark/spark-core';
import Batcher from './batcher';
import ClientMetricsBatcher from './client-metrics-batcher';
import {deprecated} from 'core-decorators';

const Metrics = SparkPlugin.extend({
  children: {
    batcher: Batcher,
    clientMetricsBatcher: ClientMetricsBatcher
  },

  namespace: `Metrics`,

  @deprecated(`Metrics#sendUnstructured() is deprecated; please use Metrics#submit()`)
  sendUnstructured(key, value) {
    return this.submit(key, value);
  },

  submit(key, value) {
    return this.batcher.request(Object.assign({key}, value));
  },

  submitClientMetrics(eventName, props) {
    const payload = {metricName: eventName};
    if (props.tags) {
      payload.tags = props.tags;
    }
    if (props.fields) {
      payload.fields = props.fields;
    }
    return this.clientMetricsBatcher.request(payload);
  }
});

export default Metrics;

@@ -716,20 +716,32 @@
 e, props
+, preLoginId
 ) %7B%0A
-
     cons
@@ -913,62 +913,1348 @@
     
-return this.clientMetricsBatcher.request(payload);%0A  %7D
+if (props.type) %7B%0A      payload.type = props.type;%0A    %7D%0A    if (preLoginId) %7B%0A      const _payload = %7B%0A        metrics: %5B%0A          payload%0A        %5D%0A      %7D;%0A      // Do not batch these because pre-login events occur during onboarding, so we will be partially blind%0A      // to users' progress through the reg flow if we wait to persist pre-login metrics for people who drop off because%0A      // their metrics will not post from a queue flush in time%0A      this.postPreLoginMetric(_payload, preLoginId);%0A    %7D else %7B%0A      return this.clientMetricsBatcher.request(payload);%0A    %7D%0A  %7D,%0A%0A%0A  /**%0A   * Issue request to alias a user's pre-login ID with their CI UUID%0A   * @param %7Bstring%7D preLoginId%0A   */%0A  alias: function(preLoginId) %7B%0A    var req = this.request(%7B%0A      method: %60POST%60,%0A      api: %60metrics%60,%0A      resource: %60clientmetrics%60,%0A      headers: %7B%0A        %22X-Prelogin-UserId%22: preLoginId%0A      %7D,%0A      body: %7B%7D%0A    %7D);%0A    req.setParameter(%22alias%22, true);%0A    return req;%0A  %7D,%0A%0A  postPreLoginMetric: function(payload, preLoginId) %7B%0A    return this.request(%7B%0A      method: %60POST%60,%0A      url: 'https://metrics-a.wbx2.com/metrics/api/v1/clientmetrics-prelogin',%0A      headers: %7B%0A        %22X-Prelogin-UserId%22: preLoginId,%0A        %22content-type%22: %22application/json%22,%0A        %22charset%22: %22utf-8%22%0A      %7D,%0A      body: payload%0A    %7D);%0A  %7D%0A%0A
 %0A%7D);

add support for pre-login and aliasing endpoints for metrics