"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/env.ts
var env_exports = {};
__export(env_exports, {
  RiverbankEnvError: () => RiverbankEnvError,
  getRiverbankEnv: () => getRiverbankEnv,
  getRiverbankTarget: () => getRiverbankTarget,
  isPreviewMode: () => isPreviewMode
});
module.exports = __toCommonJS(env_exports);
var RiverbankEnvError = class extends Error {
  constructor(message, missingVars) {
    super(message);
    this.missingVars = missingVars;
    this.name = "RiverbankEnvError";
  }
};
function getEnvPrefix(target) {
  return target === "remote" ? "RIVERBANK_REMOTE" : "RIVERBANK_LOCAL";
}
function detectTarget() {
  const env = process.env.RIVERBANK_ENV?.toLowerCase();
  if (env === "remote" || env === "production" || env === "prod") {
    return "remote";
  }
  return "local";
}
function detectPreviewMode() {
  const previewMode = process.env.RIVERBANK_PREVIEW_MODE;
  if (previewMode !== void 0 && previewMode !== "") {
    return previewMode === "true";
  }
  const previewLegacy = process.env.RIVERBANK_PREVIEW;
  if (previewLegacy !== void 0 && previewLegacy !== "") {
    return previewLegacy === "true";
  }
  return process.env.VERCEL_ENV === "preview";
}
function validateKeyType(key, expectedType) {
  const isPreviewKey = key.startsWith("bld_preview_sk_");
  const isContentKey = key.startsWith("bld_live_sk_");
  if (expectedType === "preview" && !isPreviewKey) {
    if (isContentKey) {
      throw new RiverbankEnvError(
        `Preview mode is enabled (RIVERBANK_PREVIEW_MODE=true) but a content key was provided. Preview mode requires a preview API key (bld_preview_sk_...) to access draft content. Either:
  1. Set RIVERBANK_*_PREVIEW_API_KEY with your preview key, or
  2. Set RIVERBANK_PREVIEW_MODE=false to fetch published content instead.`,
        []
      );
    }
    throw new RiverbankEnvError(
      `Invalid API key format for preview mode. Expected key starting with bld_preview_sk_`,
      []
    );
  }
  if (expectedType === "content" && !isContentKey) {
    if (isPreviewKey) {
      console.debug(
        "[getRiverbankEnv] Using preview key for published content. Consider using a content key (bld_live_sk_...) for production."
      );
      return;
    }
    throw new RiverbankEnvError(
      `Invalid API key format for content access. Expected key starting with bld_live_sk_ or bld_preview_sk_`,
      []
    );
  }
}
function isPreviewMode() {
  return detectPreviewMode();
}
function getRiverbankTarget() {
  return detectTarget();
}
function getRiverbankEnv() {
  const target = detectTarget();
  const previewMode = detectPreviewMode();
  const prefix = getEnvPrefix(target);
  const siteId = process.env[`${prefix}_SITE_ID`];
  const dashboardUrl = process.env[`${prefix}_DASHBOARD_URL`];
  const contentKey = process.env[`${prefix}_API_KEY`];
  const previewKey = process.env[`${prefix}_PREVIEW_API_KEY`];
  const missingVars = [];
  if (!siteId) missingVars.push(`${prefix}_SITE_ID`);
  if (!dashboardUrl) missingVars.push(`${prefix}_DASHBOARD_URL`);
  let apiKey;
  let keyType;
  if (previewMode) {
    apiKey = previewKey;
    keyType = "preview";
    if (!apiKey) {
      missingVars.push(`${prefix}_PREVIEW_API_KEY`);
    }
  } else {
    apiKey = contentKey || previewKey;
    keyType = contentKey ? "content" : "preview";
    if (!apiKey) {
      missingVars.push(`${prefix}_API_KEY`);
    }
  }
  if (missingVars.length > 0) {
    throw new RiverbankEnvError(
      `Missing required environment variables for Riverbank SDK:
` + missingVars.map((v) => `  - ${v}`).join("\n") + `

Current settings: RIVERBANK_ENV=${process.env.RIVERBANK_ENV || "local"}, RIVERBANK_PREVIEW_MODE=${process.env.RIVERBANK_PREVIEW_MODE || "false"}`,
      missingVars
    );
  }
  if (!apiKey || !dashboardUrl || !siteId) {
    throw new RiverbankEnvError("Required environment variables missing", missingVars);
  }
  validateKeyType(apiKey, keyType);
  let baseUrl = dashboardUrl;
  if (!baseUrl.endsWith("/api")) {
    baseUrl = baseUrl.replace(/\/$/, "") + "/api";
  }
  return {
    target,
    previewMode,
    siteId,
    baseUrl,
    apiKey,
    keyType
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  RiverbankEnvError,
  getRiverbankEnv,
  getRiverbankTarget,
  isPreviewMode
});
//# sourceMappingURL=env.js.map