import { OwnerLevel as CwOwnerLevel } from "../../generated/prisma/client";
import {
  CorporateLocation as ApiCorporateLocation,
  Country,
  USState,
} from "../../../api/generated/prisma/client";
import { Translation } from "./types";

const toUSState = (value: string | null): USState | null => {
  if (!value) return null;
  const normalized = value.trim().toUpperCase();
  if (normalized in USState) {
    return normalized as USState;
  }
  return null;
};

const toCountry = (value: number | null): Country | null => {
  if (value == null) return null;
  return Country.US;
};

export const corporateLocationTranslation: Translation<
  CwOwnerLevel,
  ApiCorporateLocation
> = {
  values: [
    { from: "ownerLevelRecId", to: "id" },
    {
      from: "ownerLevelName",
      to: "name",
      process: (value) => (value ? value : "Unknown Location"),
    },
    { from: "description", to: "description" },
    { from: "updatedBy", to: "updatedById" },
    { from: "olAddressLine1", to: "addressLine1" },
    { from: "olAddressLine2", to: "addressLine2" },
    { from: "olCity", to: "city" },
    { from: "olStateId", to: "state", process: toUSState },
    { from: "olZip", to: "zipCode" },
    { from: "olCountryRecId", to: "country", process: toCountry },
    { from: "dateEnteredUtc", to: "createdAt" },
    { from: "lastUpdatedUtc", to: "updatedAt" },
  ],
};
