type UnknownObject = Record<string, unknown>;

function asObject(value: unknown): UnknownObject | undefined {
  return value && typeof value === "object"
    ? (value as UnknownObject)
    : undefined;
}

export function pickCase<T>(
  source: unknown,
  camelKey: string,
  pascalKey: string,
  fallback: T,
): T {
  const obj = asObject(source);
  if (!obj) return fallback;

  const value = obj[camelKey] ?? obj[pascalKey];
  return (value as T | undefined) ?? fallback;
}
