import {
  onLCP,
  onINP,
  onCLS
} from "web-vitals";
const getWindow = () => {
  try {
    window.top?.document;
    return window.top || window.self;
  } catch (e) {
    return window.self;
  }
};
const webvitals = (logger) => {
  const setupReporting = () => {
    const pageWindow = getWindow();
    const { title: productPageTitle } = pageWindow.document;
    const { title: appPageTitle } = document;
    return (metric) => {
      const productId = pageWindow?.emui?.appId ?? "";
      const version = pageWindow?.emui?.version ?? "";
      const appId = window?.emui?.appId ?? "";
      const { pathname: productPath, href: productUrl } = pageWindow.location;
      const { pathname: appPath, href: appUrl } = window.location;
      logger.info({
        message: "webvitals",
        ...metric,
        productId,
        productPath,
        productUrl,
        productPageTitle,
        version,
        appId,
        appPath,
        appUrl,
        appPageTitle
      });
    };
  };
  const onReport = setupReporting();
  onLCP(onReport);
  onINP(onReport);
  onCLS(onReport);
};
export {
  webvitals
};
