import { getDefaultTheme } from "@elliemae/pui-theme";
const getWindow = () => {
  try {
    window.parent.document;
    return window.parent;
  } catch (err) {
    return window;
  }
};
const getViewportSize = () => ({
  width: Math.max(
    document.documentElement.clientWidth || 0,
    window.innerWidth || 0
  ),
  height: Math.max(
    document.documentElement.clientHeight || 0,
    window.innerHeight || 0
  )
});
const convertBreakpointToNumber = (breakpoint) => Number(breakpoint?.replace("px", ""));
const getCurrentBreakpoint = () => {
  const { width } = getViewportSize();
  const { breakpoints } = getDefaultTheme();
  if (width <= convertBreakpointToNumber(breakpoints.small)) return "small";
  if (width <= convertBreakpointToNumber(breakpoints.medium)) return "medium";
  if (width <= convertBreakpointToNumber(breakpoints.xsmall)) return "xsmall";
  return "large";
};
const getAssetPath = () => {
  const assetPath = window?.emui?._ASSET_PATH || "latest/";
  if (!window.emui?.version) return assetPath;
  const majorMinorVersion = window.emui?.version.match(/^(?:\d+\.\d+)*/g);
  return majorMinorVersion && majorMinorVersion[0] ? assetPath.replace("latest/", `${majorMinorVersion[0]}/`) : assetPath;
};
export {
  getAssetPath,
  getCurrentBreakpoint,
  getViewportSize,
  getWindow
};
