import type { AddressAvailability } from "../services/practiceAddressApi"; declare const AKASHIC_STACK: string | undefined; // The host a practice answers at on this build's stack: a founder is shown it before it is permanent. // Read from the build constant, not `environment`, so this module stays free of `window`. export const SUFFIX_BY_STACK: Record = { staging: ".staging.akashic.health", sandbox: ".staging.akashic.health", prod: ".akashic.health", }; export const ADDRESS_SUFFIX = SUFFIX_BY_STACK[typeof AKASHIC_STACK === "string" ? AKASHIC_STACK : "prod"] ?? ".akashic.health"; // A check belongs to the label it was asked about, so an answer for an earlier label never // enables saving a newer one (permanent hostname ruling, 2026-09-18). export type AddressCheck = { label: string; state: AddressAvailability | "checking" }; export const normalizeAddressLabel = (value: string): string => value.trim().toLowerCase(); export const isAddressReady = (value: string, check: AddressCheck | undefined): boolean => check !== undefined && check.state === "available" && check.label === normalizeAddressLabel(value); export type AddressStatus = { tone: "ok" | "pending" | "problem"; text: string; retry?: boolean }; export const addressStatus = ( value: string, check: AddressCheck | undefined, suggested: boolean, ): AddressStatus | undefined => { const label = normalizeAddressLabel(value); if (!label) return undefined; if (!check || check.label !== label || check.state === "checking") { return { tone: "pending", text: "Checking availability…" }; } switch (check.state) { case "available": return { tone: "ok", text: suggested ? "Available · suggested from your practice name" : "Available" }; case "taken": return { tone: "problem", text: "This name is already taken. Choose another name." }; case "reserved": return { tone: "problem", text: "This name is reserved. Choose another name." }; case "invalid": return { tone: "problem", text: "Use three to sixty-three letters a–z, with no numbers, spaces or other characters." }; default: return { tone: "problem", text: "We could not check this name just now.", retry: true }; } };