import { usePage } from '@inertiajs/react';
import type { ReleaseState } from '@/types';

/**
 * Whether a part of the system is released, for deciding what to render.
 *
 * The answer comes from the server's shared prop, which is built from the same
 * Release class EnforceRelease asks, so the sidebar cannot offer a link the
 * server would answer with 404. A page rendered before the prop exists (or a
 * test that does not share it) treats everything as visible, which fails open
 * in the safe direction: the route still refuses, so nothing is exposed.
 */
export function useRelease() {
    const { release } = usePage<{ release?: ReleaseState }>().props;

    const state: ReleaseState = release ?? {
        full: false,
        parts: {},
        progress: { enabled: 0, total: 0 },
    };

    const enabled = (part?: string): boolean =>
        part === undefined ? true : (state.parts[part]?.enabled ?? true);

    return { release: state, enabled, full: state.full };
}
