/**
 * Default collect function that flattens segment arrays into a single array.
 */
function defaultCollect(segments) {
    return segments.flat();
}
// Module-level registry mapping $$id to collect functions.
// Populated when createHandle() runs (both server and client).
// Used by useHandle() to recover collect when handle is deserialized from RSC prop.
const collectRegistry = new Map();
/**
 * Look up a collect function from the registry by handle $$id.
 * Returns undefined if not registered (falls back to defaultCollect in useHandle).
 */
export function getCollectFn(id) {
    return collectRegistry.get(id);
}
/**
 * Create a handle definition for accumulating data across route segments.
 *
 * The $$id is auto-generated by the Vite exposeInternalIds plugin based on
 * file path and export name. No manual naming required.
 *
 * @param collect - Optional collect function (default: flatten into array)
 * @param __injectedId - Auto-injected by Vite plugin, do not provide manually
 *
 * @example
 * ```ts
 * // Default: flatten into array
 * export const Breadcrumbs = createHandle<BreadcrumbItem>();
 * // Result type: BreadcrumbItem[]
 *
 * // Custom: last value wins
 * export const PageTitle = createHandle<string, string>(
 *   (segments) => segments.flat().at(-1) ?? "Default Title"
 * );
 * // Result type: string
 *
 * // Custom: object merge
 * export const Meta = createHandle<Partial<MetaTags>, MetaTags>(
 *   (segments) => Object.assign({ robots: "index,follow" }, ...segments.flat())
 * );
 * // Result type: MetaTags
 *
 * // Custom: dedupe by href
 * export const Breadcrumbs = createHandle<BreadcrumbItem>(
 *   (segments) => {
 *     const all = segments.flat();
 *     return all.filter((item, i) => all.findIndex(x => x.href === item.href) === i);
 *   }
 * );
 * ```
 */
export function createHandle(collect, __injectedId) {
    const handleId = __injectedId ?? "";
    if (!handleId && process.env.NODE_ENV !== "production") {
        throw new Error("[rsc-router] Handle is missing $$id. " +
            "Make sure the exposeInternalIds Vite plugin is enabled and " +
            "the handle is exported with: export const MyHandle = createHandle(...)");
    }
    const collectFn = collect ??
        defaultCollect;
    // Register collect in module-level registry so useHandle() can recover it
    // when the handle is deserialized from RSC props (toJSON strips collect).
    if (handleId) {
        collectRegistry.set(handleId, collectFn);
    }
    return {
        __brand: "handle",
        $$id: handleId,
    };
}
/**
 * Type guard to check if a value is a Handle.
 */
export function isHandle(value) {
    return (typeof value === "object" &&
        value !== null &&
        "__brand" in value &&
        value.__brand === "handle");
}
//# sourceMappingURL=handle.js.map