/** Stronger BC webhook scopes win when deduping queue rows for the same entity. */
const SCOPE_RANK: Record<string, number> = {
	"product/deleted": 100,
	"category/deleted": 100,
	"category/updated": 70,
	"category/created": 75,
	"product/created": 80,
	"product/updated": 70,
	"product/inventory/updated": 30,
	"product/inventory/order/updated": 25,
};

function rank(scope: string): number {
	if (SCOPE_RANK[scope] != null) return SCOPE_RANK[scope];
	if (scope.endsWith("/deleted")) return 90;
	if (scope.includes("inventory")) return 20;
	return 50;
}

export function mergeScope(existing: string, incoming: string): string {
	if (!existing) return incoming;
	if (!incoming) return existing;
	return rank(incoming) >= rank(existing) ? incoming : existing;
}
