/**
 * Shared location state utilities - works in both RSC and client contexts
 * No "use client" directive so it can be imported from RSC
 */
// Track used keys to detect duplicates in development
const usedKeys = new Set();
/**
 * Create a type-safe location state definition
 *
 * The key is auto-generated by the Vite exposeInternalIds plugin based on
 * file path and export name. No manual key required.
 *
 * @param key Auto-injected by Vite plugin, do not provide manually
 * @returns A typed state definition for use with Link and useLocationState
 *
 * @example
 * ```typescript
 * // Define typed state (key auto-generated from file + export)
 * export const ProductState = createLocationState<{ name: string; price: number }>();
 *
 * // Use in Link - state is captured at click time
 * <Link to="/product/123" state={[ProductState({ name: product.name, price: product.price })]}>
 *   View Product
 * </Link>
 *
 * // Multiple states
 * <Link to="/checkout" state={[ProductState(productData), CartState(cartData)]}>
 *   Checkout
 * </Link>
 *
 * // For lazy evaluation (click-time), pass a getter
 * <Link to="/product" state={[ProductState(() => ({ name: product.name }))]}>
 *
 * // Read with type safety
 * const productState = useLocationState(ProductState);
 * // productState: { name: string; price: number } | undefined
 * ```
 */
export function createLocationState(key) {
    if (!key && process.env.NODE_ENV !== "production") {
        throw new Error("[rsc-router] createLocationState is missing a key. " +
            "Make sure the exposeInternalIds Vite plugin is enabled and " +
            "the state is exported with: export const MyState = createLocationState(...)");
    }
    const fullKey = `__rsc_ls_${key}`;
    // Warn about duplicate keys in development
    if (process.env.NODE_ENV !== "production" && usedKeys.has(fullKey)) {
        console.warn(`[rsc-router] Duplicate location state key "${key}". ` +
            `Each createLocationState call should have a unique key.`);
    }
    usedKeys.add(fullKey);
    const definition = Object.assign((stateOrGetter) => ({
        __rsc_ls_key: fullKey,
        // Resolve getter immediately - lazy evaluation happens via Link's stateRef pattern
        __rsc_ls_value: typeof stateOrGetter === "function"
            ? stateOrGetter()
            : stateOrGetter,
    }), { __rsc_ls_key: fullKey });
    return definition;
}
/**
 * Check if a value is a LocationStateEntry
 */
export function isLocationStateEntry(value) {
    return (value !== null &&
        typeof value === "object" &&
        "__rsc_ls_key" in value &&
        "__rsc_ls_value" in value &&
        typeof value.__rsc_ls_key === "string");
}
/**
 * Resolve state entries into a flat object for history.state
 */
export function resolveLocationStateEntries(entries) {
    const result = {};
    for (const entry of entries) {
        result[entry.__rsc_ls_key] = entry.__rsc_ls_value;
    }
    return result;
}
//# sourceMappingURL=location-state-shared.js.map