import { SIM_PLAN_CODES, SIM_PLAN_LABELS, SIM_STATUS } from "./contract.js"; import type { SimStatus, SimFeaturesUpdateRequest } from "./schema.js"; export function canManageActiveSim(status: SimStatus): boolean { return status === SIM_STATUS.ACTIVE; } export function canReissueEsim(status: SimStatus): boolean { return canManageActiveSim(status); } export function canCancelSim(status: SimStatus): boolean { return canManageActiveSim(status); } export function canTopUpSim(status: SimStatus): boolean { return canManageActiveSim(status); } export function formatSimPlanShort(planCode?: string): string { if (!planCode) return "—"; const normalized = planCode.trim(); const matchPrimary = normalized.match(/(?:^|[_-])(\d+(?:\.\d+)?)\s*G(?:B)?\b/i); if (matchPrimary?.[1]) { return `${matchPrimary[1]}G`; } const matchFallback = normalized.match(/(\d+(?:\.\d+)?)\s*G(?:B)?\b/i); if (matchFallback?.[1]) { return `${matchFallback[1]}G`; } return normalized; } export type SimPlanOption = { code: (typeof SIM_PLAN_CODES)[number]; label: string; shortLabel: string; }; export const SIM_PLAN_OPTIONS: SimPlanOption[] = SIM_PLAN_CODES.map(code => ({ code, label: SIM_PLAN_LABELS[code], shortLabel: formatSimPlanShort(code), })); export function getSimPlanLabel(planCode?: string): string { if (!planCode) return "Unknown plan"; if (planCode in SIM_PLAN_LABELS) { return SIM_PLAN_LABELS[planCode as keyof typeof SIM_PLAN_LABELS]; } return formatSimPlanShort(planCode); } export type SimFeatureToggleSnapshot = { voiceMailEnabled: boolean; callWaitingEnabled: boolean; internationalRoamingEnabled: boolean; networkType: "4G" | "5G"; }; export function buildSimFeaturesUpdatePayload( current: SimFeatureToggleSnapshot, next: SimFeatureToggleSnapshot ): SimFeaturesUpdateRequest | null { const payload: SimFeaturesUpdateRequest = {}; if (current.voiceMailEnabled !== next.voiceMailEnabled) { payload.voiceMailEnabled = next.voiceMailEnabled; } if (current.callWaitingEnabled !== next.callWaitingEnabled) { payload.callWaitingEnabled = next.callWaitingEnabled; } if (current.internationalRoamingEnabled !== next.internationalRoamingEnabled) { payload.internationalRoamingEnabled = next.internationalRoamingEnabled; } if (current.networkType !== next.networkType) { payload.networkType = next.networkType; } return Object.keys(payload).length > 0 ? payload : null; }