import { a as __vitePreload } from "./TheAppHeader-DSsf6DXw.js";
const getMiGlobal = (key, defaultValue = null) => {
  return typeof window !== "undefined" && window.monsterinsights && typeof window.monsterinsights === "object" && Object.hasOwn(window.monsterinsights, key) ? window.monsterinsights[key] : defaultValue;
};
async function ensureBearerToken(minValidityMs = 5 * 60 * 1e3) {
  if (typeof window === "undefined") {
    return false;
  }
  const bearerToken = getMiGlobal("bearer_token");
  const bearerExpires = getMiGlobal("bearer_expires");
  if (bearerToken && bearerExpires) {
    const expiresAtMs = bearerExpires * 1e3;
    const now = Date.now();
    const remainingMs = expiresAtMs - now;
    if (remainingMs > minValidityMs) {
      return true;
    }
  }
  if (!window.wp || !window.wp.ajax) {
    return false;
  }
  const nonce = getMiGlobal("nonce");
  try {
    const response = await window.wp.ajax.post("monsterinsights_get_bearer_token", {
      nonce
    });
    const body = response?.data && typeof response.data === "object" ? response.data : response;
    const inner = body?.data && typeof body.data === "object" ? body.data : body;
    const token = inner?.token ?? body?.token;
    const expiresAt = inner?.expires_at ?? body?.expires_at;
    if (token && typeof token === "string") {
      if (window.monsterinsights && typeof window.monsterinsights === "object") {
        window.monsterinsights.bearer_token = token;
        window.monsterinsights.bearer_expires = expiresAt ?? 0;
      }
      return true;
    }
  } catch (_e) {
  }
  return false;
}
const isPro = () => {
  return true;
};
const isAddonActive = (addon) => {
  const addons = getMiGlobal("addons", {});
  return !!addons[addon];
};
const isAddonInstalled = (addon) => {
  const info = getMiGlobal("addons_info", {});
  return !!info[addon]?.installed;
};
const getAddonBasename = (addon) => {
  const info = getMiGlobal("addons_info", {});
  return info[addon]?.basename || "";
};
const getAddonsPageUrl = () => {
  return getMiGlobal("addons_page_url", "/wp-admin/admin.php?page=monsterinsights_settings#/addons");
};
const isAuthed = () => {
  return getMiGlobal("authed", false);
};
const sampleDataModules = /* @__PURE__ */ Object.assign({ "../stores/sample-data/custom-dashboard/sample-view.json": () => __vitePreload(() => import("./sample-view-DCgpbF5N.js"), true ? [] : void 0, import.meta.url), "../stores/sample-data/custom-dashboard/widgets-data.json": () => __vitePreload(() => import("./widgets-data-TjimP1Ju.js"), true ? [] : void 0, import.meta.url) });
const getSampleData = async (type) => {
  if (!type) {
    return null;
  }
  const path = `../stores/sample-data/${type}.json`;
  if (sampleDataModules[path]) {
    try {
      const module = await sampleDataModules[path]();
      return module.default?.data || module.default;
    } catch (error) {
      console.error(`Error loading sample data for '${type}':`, error);
      return null;
    }
  }
  return null;
};
function addQueryArg(uri, key, value) {
  let hash = "";
  const re = new RegExp(`([?&])${key}=.*?(&|#|$)`, "i");
  const separator = uri.indexOf("?") !== -1 ? "&" : "?";
  if (uri.match(re)) {
    return uri.replace(re, `$1${key}=${value}$2`);
  } else {
    if (uri.indexOf("#") !== -1) {
      hash = uri.replace(/.*#/, "#");
      uri = uri.replace(/#.*/, "");
    }
    return `${uri + separator + key}=${value}${hash}`;
  }
}
function getUrl(medium, campaign, url) {
  const source = "proplugin", default_url = "my-account/", content = getMiGlobal("plugin_version", "1.0.0");
  medium = medium ? medium : "defaultmedium";
  campaign = campaign ? campaign : "defaultcampaign";
  url = url ? url : `https://www.monsterinsights.com/${default_url}`;
  url = addQueryArg(url, "utm_source", source);
  url = addQueryArg(url, "utm_medium", medium);
  url = addQueryArg(url, "utm_campaign", campaign);
  url = addQueryArg(url, "utm_content", content);
  return url;
}
function getUpgradeUrl(medium, campaign, url) {
  const upgrade_url = getUrl(medium, campaign, url);
  {
    return upgrade_url;
  }
}
export {
  isAuthed as a,
  getMiGlobal as b,
  isAddonInstalled as c,
  getAddonsPageUrl as d,
  ensureBearerToken as e,
  getUpgradeUrl as f,
  getAddonBasename as g,
  getUrl as h,
  isAddonActive as i,
  getSampleData as j,
  isPro as k
};
