import _defineProperty from "@babel/runtime/helpers/defineProperty";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

import ReactGA from "react-ga4";
import { getStorageInstance } from "../storage";
var storage = getStorageInstance();
export var event = function event(eventName, data) {
  var force = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  var ttl = 2000;
  var lastEvent = JSON.parse(storage.getItem("LAST_GA4_EVENT"));
  var now = new Date();

  var item = _objectSpread(_objectSpread({
    name: eventName
  }, data), {}, {
    expiry: now.getTime() + ttl
  });

  if (force || !lastEvent || item.name !== lastEvent.name || now.getTime() > lastEvent.expiry) {
    storage.setItem("LAST_GA4_EVENT", JSON.stringify(item));
    ReactGA.event(eventName, data);
  }
};
export var pageView = function pageView(page, data) {
  var ttl = 2000;
  var lastEvent = JSON.parse(storage.getItem("LAST_GA4_PAGE_VIEW"));
  var now = new Date();

  var item = _objectSpread(_objectSpread({
    name: page
  }, data), {}, {
    expiry: now.getTime() + ttl
  });

  if (!lastEvent || item.name !== lastEvent.name || now.getTime() > lastEvent.expiry) {
    storage.setItem("LAST_GA4_PAGE_VIEW", JSON.stringify(item));
    ReactGA.send(_objectSpread({
      hitType: "pageview",
      page: page
    }, data));
  }
};
export var conversion = function conversion(eventName, value, data) {
  ReactGA.event(eventName, _objectSpread({
    value: value
  }, data));
};
export var setUserProperty = function setUserProperty(userData) {
  ReactGA.gtag("set", "user_properties", userData);
};