'use strict';

var core = require('@vueuse/core');
var index = require('../../../hooks/store/userCenter/useCurrentUser/index.js');
var is = require('../is.js');

const injectScript = (projectId) => {
  try {
    (function(c, l, a, r, i, t, y) {
      if (l.getElementById("clarity-script")) {
        return;
      }
      c[a] = c[a] || function() {
        (c[a].q = c[a].q || []).push(arguments);
      };
      t = l.createElement(r);
      t.async = 1;
      t.src = "https://www.clarity.ms/tag/" + i + "?ref=npm";
      t.id = "clarity-script";
      y = l.getElementsByTagName(r)[0];
      y.parentNode.insertBefore(t, y);
    })(window, document, "clarity", "script", projectId);
  } catch {
  }
};
const autoIdentifyUser = async () => {
  const currentUserStore = index.useCurrentUserStore();
  await core.until(() => currentUserStore.userInfo?.id).toBeTruthy();
  const userInfo = {
    id: currentUserStore.userInfo?.id?.toString() || "",
    name: currentUserStore.userInfo?.name?.toString() || ""
  };
  Clarity.identify({
    customId: userInfo.id,
    customSessionId: userInfo.id,
    customPageId: "",
    friendlyName: `${userInfo.name} - ${userInfo.id}`
  });
};
const Clarity = {
  /**
   * 初始化 Clarity
   * @param projectId 项目 ID
   * @param options 配置选项
   */
  init(projectId, options = {}) {
    const { autoIdentify = true } = options;
    if (!projectId) {
      return;
    }
    injectScript(projectId);
    if (autoIdentify) {
      autoIdentifyUser();
    }
  },
  /**
   * 判断 Clarity 是否可用
   */
  isAvailable() {
    return is.isFunction(window.clarity);
  },
  /**
   * 设置标签
   */
  setTag(key, value) {
    if (is.isFunction(window.clarity)) {
      window.clarity("set", key, value);
    } else {
      console.warn("[Clarity] window.clarity \u4E0D\u53EF\u7528");
    }
  },
  /**
   * 识别用户
   */
  identify(options) {
    if (is.isFunction(window.clarity)) {
      const { customId, customSessionId, customPageId, friendlyName } = options;
      window.clarity("identify", customId, customSessionId, customPageId, friendlyName);
    } else {
      console.warn("[Clarity] window.clarity \u4E0D\u53EF\u7528");
    }
  },
  /**
   * 同意追踪
   */
  consent(consent = true) {
    if (is.isFunction(window.clarity)) {
      window.clarity("consent", consent);
    } else {
      console.warn("[Clarity] window.clarity \u4E0D\u53EF\u7528");
    }
  },
  /**
   * 同意追踪 V2
   */
  consentV2(options = { ad_Storage: "granted", analytics_Storage: "granted" }) {
    if (is.isFunction(window.clarity)) {
      window.clarity("consentv2", options);
    } else {
      console.warn("[Clarity] window.clarity \u4E0D\u53EF\u7528");
    }
  },
  /**
   * 升级会话
   */
  upgrade(reason) {
    if (is.isFunction(window.clarity)) {
      window.clarity("upgrade", reason);
    } else {
      console.warn("[Clarity] window.clarity \u4E0D\u53EF\u7528");
    }
  },
  /**
   * 发送自定义事件
   */
  event(eventName) {
    if (is.isFunction(window.clarity)) {
      window.clarity("event", eventName);
    } else {
      console.warn("[Clarity] window.clarity \u4E0D\u53EF\u7528");
    }
  }
};

exports.Clarity = Clarity;
//# sourceMappingURL=index.js.map
