export interface PlimiRuntimeConfig {
  VITE_PLIMI_REPO_URL?: string;
  VITE_SITE_URL?: string;
  VITE_LEGAL_NAME?: string;
  VITE_LEGAL_COUNTRY?: string;
  VITE_PRIVACY_EMAIL?: string;
  VITE_PRIVACY_EFFECTIVE_DATE?: string;
  VITE_ANALYTICS_PROVIDER_NAME?: string;
  VITE_ANALYTICS_SCRIPT_URL?: string;
  VITE_ANALYTICS_HOST_URL?: string;
  VITE_ANALYTICS_WEBSITE_ID?: string;
  VITE_ANALYTICS_HOST?: string;
  VITE_ANALYTICS_HOST_COUNTRY?: string;
  VITE_ANALYTICS_RETENTION_MONTHS?: string;
}

declare global {
  interface Window {
    __PLIMI_CONFIG__?: PlimiRuntimeConfig;
  }
}

export function runtimeConfigValue(
  name: keyof PlimiRuntimeConfig,
  fallback: string
): string {
  const runtimeValue = window.__PLIMI_CONFIG__?.[name];
  if (typeof runtimeValue === "string" && runtimeValue.trim()) {
    return runtimeValue.trim();
  }

  const buildValue = import.meta.env[name];
  if (typeof buildValue === "string" && buildValue.trim()) {
    return buildValue.trim();
  }

  return fallback;
}
