const ACTIVE_STATION_KEY = 'qms_teller_active_station';

export function saveActiveStation({ stationId, stationName, branchId, branchName }) {
    sessionStorage.setItem(
        ACTIVE_STATION_KEY,
        JSON.stringify({
            stationId: Number(stationId),
            stationName,
            branchId: branchId != null ? Number(branchId) : null,
            branchName: branchName ?? null,
        })
    );
}

export function getActiveStation(stationId) {
    try {
        const raw = sessionStorage.getItem(ACTIVE_STATION_KEY);
        if (!raw) return null;
        const parsed = JSON.parse(raw);
        if (stationId != null && Number(parsed.stationId) !== Number(stationId)) {
            return null;
        }
        return parsed;
    } catch {
        return null;
    }
}

export function clearActiveStation() {
    sessionStorage.removeItem(ACTIVE_STATION_KEY);
}
