import type { User } from '$lib/auth';
import { userHasPermission } from '$lib/auth';

/** Prefijos RBAC por valor de `catalog` en TariffFractionList (sin sufijos .view/.create). */
export const CATALOG_TO_BASE_PREFIX: Record<string, string> = {
	mex: 'frac_sitar',
	usa: 'frac_sitar_us',
	american: 'frac_american',
	canadian: 'frac_canadian',
	historical: 'frac_historical'
};

/**
 * Resuelve el prefijo RBAC (p. ej. `frac_sitar`, `frac_sitar_7`) a partir de catálogo
 * y override opcional (Séptima enmienda, etc.).
 */
export function resolveTariffFractionBasePrefix(
	catalog: string,
	override?: string | null
): string {
	if (override) return override;
	return CATALOG_TO_BASE_PREFIX[catalog] ?? 'frac_sitar';
}

export function canViewTariffFractionCatalog(user: User | null, basePrefix: string): boolean {
	return userHasPermission(user, `${basePrefix}.view`);
}

export function canCreateTariffFractionCatalog(user: User | null, basePrefix: string): boolean {
	return userHasPermission(user, `${basePrefix}.create`);
}

export function canEditTariffFractionCatalog(user: User | null, basePrefix: string): boolean {
	return userHasPermission(user, `${basePrefix}.edit`);
}

export function canDeleteTariffFractionCatalog(user: User | null, basePrefix: string): boolean {
	return userHasPermission(user, `${basePrefix}.delete`);
}
