function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { LogComplianceExtendKeys, KEYS } from "./keys";
export var LogComplianceProcessor = /*#__PURE__*/function () {
  function LogComplianceProcessor() {
    _classCallCheck(this, LogComplianceProcessor);
  }
  _createClass(LogComplianceProcessor, null, [{
    key: "extractComplianceParams",
    value: function extractComplianceParams(eventName, params, errorLogCallback) {
      var _this = this;
      try {
        // 触发防抖上报
        logDebounceTracker.track(function () {
          _this.flushAbnormalInfo(errorLogCallback);
        });
        // 事件名检测
        var isSpmId = eventName ? eventName.includes('.') : false;
        if (!isSpmId) {
          // 非SPM，说明是未做映射的，需要加到埋点里面
          this.reportHasAbnormalEventName(eventName);
        }
        var newParams = {};
        // 逐个字段检查
        for (var _i = 0, _Object$keys = Object.keys(params); _i < _Object$keys.length; _i++) {
          var key = _Object$keys[_i];
          // 如果当前字段不在合规表中，说明是未合规字段，剔除并且上报异常
          if (key in LogComplianceExtendKeys.complianceExtParams) {
            newParams[key] = "".concat(params[key]);
          } else {
            // 如果当前字段不在合规表中，说明是未合规字段，剔除并且上报异常
            this.reportHasIllegalExtendInfoKey(eventName, key);
          }
        }
        return newParams;
      } catch (error) {
        console.error(error);
        return {};
      }
    }
  }, {
    key: "reportHasAbnormalEventName",
    value: function reportHasAbnormalEventName(eventName) {
      this.abnormalEventNameSet.add(eventName);
    }
  }, {
    key: "reportHasIllegalExtendInfoKey",
    value: function reportHasIllegalExtendInfoKey(eventName, key) {
      if (!this.nonComplianceEventKeys[eventName]) {
        this.nonComplianceEventKeys[eventName] = new Set();
      }
      this.nonComplianceEventKeys[eventName].add(key);
    }
  }, {
    key: "flushAbnormalInfo",
    value: function flushAbnormalInfo(errorLogCallback) {
      try {
        var _this$abnormalEventNa;
        if (((_this$abnormalEventNa = this.abnormalEventNameSet) === null || _this$abnormalEventNa === void 0 ? void 0 : _this$abnormalEventNa.size) > 0) {
          // 不正常的事件名按｜分割
          // Abnormal event use event name with divider "|"
          var abnormalEventNames = Array.from(this.abnormalEventNameSet).join('|');
          errorLogCallback && errorLogCallback({
            title: KEYS.EVENT_SDK_ON_ABNORMAL_EVENT_NAME.spmId
          }, {
            eventAlias: KEYS.EVENT_SDK_ON_ABNORMAL_EVENT_NAME.alias,
            errorMessage: abnormalEventNames
          });
          this.abnormalEventNameSet.clear();
        }
        // process nonComplianceEventKeys
        // eg: if sdk_event_createComponent has key1,key2,key3
        // sdk_event_createComponent>key1+key2+key3|otherEventName>key1+key2+key3
        var nonComplianceEventKeysList = [];
        if (Object.keys(this.nonComplianceEventKeys).length === 0) {
          return;
        }
        for (var eventName in this.nonComplianceEventKeys) {
          var keys = this.nonComplianceEventKeys[eventName];
          var keyStr = "".concat(eventName, ">").concat(Array.from(keys).join('+'));
          nonComplianceEventKeysList.push(keyStr);
        }
        var nonComplianceEventKeysLogString = nonComplianceEventKeysList.join('|');
        errorLogCallback && errorLogCallback({
          title: KEYS.EVENT_SDK_ON_ABNORMAL_EVENT_EXT_INFO.spmId
        }, {
          eventAlias: KEYS.EVENT_SDK_ON_ABNORMAL_EVENT_EXT_INFO.alias,
          errorMessage: nonComplianceEventKeysLogString
        });
        this.nonComplianceEventKeys = {};
      } catch (error) {
        console.error(error);
      }
    }
  }]);
  return LogComplianceProcessor;
}();
// 记录不正常的事件名
_defineProperty(LogComplianceProcessor, "abnormalEventNameSet", new Set());
// 记录不合规的扩展字段，KEY是事件名，VALUE是非合规扩展字段列表
_defineProperty(LogComplianceProcessor, "nonComplianceEventKeys", {});
var LogDebounceTracker = /*#__PURE__*/function () {
  function LogDebounceTracker() {
    var debounceTime = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 5000;
    _classCallCheck(this, LogDebounceTracker);
    _defineProperty(this, "timeoutId", void 0);
    _defineProperty(this, "debounceTime", void 0);
    this.timeoutId = null;
    this.debounceTime = debounceTime;
  }
  _createClass(LogDebounceTracker, [{
    key: "track",
    value: function track(callback) {
      this.debounce(callback);
    }

    // 防抖实现
  }, {
    key: "debounce",
    value: function debounce(callback) {
      var _this2 = this;
      if (this.timeoutId) {
        clearTimeout(this.timeoutId);
      }
      this.timeoutId = setTimeout(function () {
        _this2.report(callback);
        _this2.reset();
      }, this.debounceTime);
    }
  }, {
    key: "report",
    value: function report(callback) {
      callback && callback();
    }
  }, {
    key: "reset",
    value: function reset() {
      this.timeoutId = null;
    }
  }]);
  return LogDebounceTracker;
}();
var logDebounceTracker = new LogDebounceTracker();