import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
function platformBaseUrl() {
  const fromEnv = process.env.PLATFORM_PORTAL_BASE_URL?.trim();
  if (fromEnv && fromEnv.length > 0) return fromEnv.replace(/\/+$/, "");
  if (process.env.NODE_ENV === "production") {
    throw new Error(
      "PLATFORM_PORTAL_BASE_URL is required in production. Configure it before sending customer-portal emails."
    );
  }
  return "http://localhost:3000";
}
function resolveContainer(container) {
  if (container) return Promise.resolve(container);
  return createRequestContainer();
}
async function urlForCustomerOrg(orgId, path, options) {
  const safePath = path.startsWith("/") ? path : `/${path}`;
  const container = await resolveContainer(options?.container);
  let active = null;
  try {
    const service = container.resolve("domainMappingService");
    if (service && typeof service.resolveActiveByOrg === "function") {
      active = await service.resolveActiveByOrg(orgId);
    }
  } catch {
    active = null;
  }
  if (active && active.hostname) {
    return `https://${active.hostname}${safePath}`;
  }
  let orgSlug = null;
  try {
    const orgService = container.resolve("orgService");
    if (orgService && typeof orgService.findById === "function") {
      const org = await orgService.findById(orgId);
      orgSlug = org?.slug ?? null;
    }
  } catch {
    orgSlug = null;
  }
  const base = platformBaseUrl();
  if (orgSlug) return `${base}/${orgSlug}/portal${safePath}`;
  return `${base}${safePath}`;
}
export {
  urlForCustomerOrg
};
//# sourceMappingURL=customerUrl.js.map
