import { b as private_env } from './shared-server-DaWdgxVh.js';
import { z } from 'zod';

const TelescopeSourceInputSchema = z.object({
  id: z.string().optional(),
  label: z.string().min(1),
  // Accept either camelCase or the user-friendly names ("url"/"secret")
  baseUrl: z.string().optional(),
  url: z.string().optional(),
  apiKey: z.string().optional(),
  secret: z.string().optional()
}).superRefine((value, ctx) => {
  const baseUrl = (value.baseUrl ?? value.url ?? "").trim();
  const apiKey = (value.apiKey ?? value.secret ?? "").trim();
  if (!baseUrl) {
    ctx.addIssue({
      code: z.ZodIssueCode.custom,
      path: ["baseUrl"],
      message: "baseUrl/url is required"
    });
  }
  if (!apiKey) {
    ctx.addIssue({
      code: z.ZodIssueCode.custom,
      path: ["apiKey"],
      message: "apiKey/secret is required"
    });
  }
});
const TelescopeSourcesJsonSchema = z.array(TelescopeSourceInputSchema);
function slugify(value) {
  return value.trim().toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
}
function cleanBaseUrl(value) {
  return value.trim().replace(/\/+$/, "");
}
function normalizeSources(inputs) {
  const usedIds = /* @__PURE__ */ new Set();
  return inputs.map((input, idx) => {
    const label = input.label.trim();
    const baseUrl = cleanBaseUrl(input.baseUrl ?? input.url ?? "");
    const apiKey = (input.apiKey ?? input.secret ?? "").trim();
    const preferred = typeof input.id === "string" && input.id.trim() ? input.id.trim() : label;
    let id = slugify(preferred) || `source-${idx + 1}`;
    let deduped = id;
    let i = 2;
    while (usedIds.has(deduped)) {
      deduped = `${id}-${i++}`;
    }
    id = deduped;
    usedIds.add(id);
    return { id, label, baseUrl, apiKey };
  });
}
function getTelescopeSources() {
  const rawJson = private_env.TELESCOPE_SOURCES_JSON;
  if (typeof rawJson === "string" && rawJson.trim()) {
    let parsed;
    try {
      parsed = JSON.parse(rawJson);
    } catch {
      throw new Error("TELESCOPE_SOURCES_JSON is not valid JSON");
    }
    const inputs = TelescopeSourcesJsonSchema.parse(parsed);
    return normalizeSources(inputs);
  }
  return [];
}

export { getTelescopeSources as g };
//# sourceMappingURL=sources.server-C2PLxQXh.js.map
