import type { RuleEngineRecord, RuleEngineResourceSlug } from "@/types/rule-engine";

export const RULE_ENGINE_POSITION_END = "__end__";

export function getOrderItemLabel(
  record: RuleEngineRecord,
  slug: RuleEngineResourceSlug,
): string {
  const code = String(record.code ?? "").trim();
  switch (slug) {
    case "cargo-types":
      return String(record.cargoTypeName ?? (code || record.id));
    case "container-types":
    case "yards":
    case "shipping-lines":
      return String(record.label ?? (code || record.id));
    case "service-types":
      return String(record.serviceName ?? (code || record.id));
    case "approval-rules":
      return String(record.actionLabel ?? record.requiredRole ?? record.id);
    default:
      return String(record.label ?? record.code ?? record.id);
  }
}

export function getOrderValue(
  record: RuleEngineRecord,
  field: "displayOrder" | "stepOrder",
): number {
  const raw = record[field];
  return typeof raw === "number" ? raw : Number(raw ?? 0);
}
