/* eslint-disable @typescript-eslint/no-explicit-any */

/**
 * Deep renames all keys in an object to lowercase
 * @param obj - The object to transform
 * @returns A new object with all keys converted to lowercase
 */
function deepLowerCaseKeys<T = any>(obj: any): T {
  if (!obj || typeof obj !== "object") {
    return obj;
  }

  // Handle arrays
  if (Array.isArray(obj)) {
    return obj.map((item) => deepLowerCaseKeys(item)) as unknown as T;
  }

  // Handle objects
  return Object.keys(obj).reduce(
    (result, key) => {
      const value = obj[key];
      const lowercaseKey = key.toLowerCase();

      result[lowercaseKey] =
        typeof value === "object" &&
        value !== null &&
        Object.keys(value).length > 0
          ? deepLowerCaseKeys(value)
          : value;

      return result;
    },
    {} as Record<string, any>,
  ) as T;
}

export default deepLowerCaseKeys;
