const getEnvValue = (primaryKey, legacyKey = "") => {
  const primaryValue = import.meta.env[primaryKey];
  if (primaryValue !== undefined && primaryValue !== "") {
    return primaryValue;
  }

  if (legacyKey) {
    const legacyValue = import.meta.env[legacyKey];
    if (legacyValue !== undefined && legacyValue !== "") {
      return legacyValue;
    }
  }

  return "";
};

export const appConfig = {
  directusBaseUrl:
    getEnvValue("VITE_DIRECTUS_BASE_URL", "_API_URL") || "https://mktcms.ose.tw",
  mktapiBaseUrl:
    getEnvValue("VITE_MKTAPI_BASE_URL") ||
    (import.meta.env.DEV ? "http://127.0.0.1:8000" : "https://mktapi.ose.tw"),
  directusDebugToken: getEnvValue("VITE_DIRECTUS_DEBUG_TOKEN", "_API_TOKEN"),
};

export const buildDirectusAssetUrl = (assetId) => {
  return `${appConfig.directusBaseUrl}/assets/${assetId}`;
};
