import { CompanyAddress as CwCompanyAddress } from "../../generated/prisma/client";
import {
  CompanyAddress as ApiCompanyAddress,
  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 companyAddressTranslation: Translation<
  CwCompanyAddress,
  ApiCompanyAddress
> = {
  values: [
    { from: "companyAddressRecId", to: "id" },
    {
      from: "description",
      to: "name",
      process: (value) => (value ? value : "Primary Site"),
    },
    { from: "description", to: "description" },
    { from: "addressLine1", to: "addressLine1" },
    { from: "addressLine2", to: "addressLine2" },
    { from: "city", to: "city" },
    { from: "stateId", to: "state", process: toUSState },
    { from: "zip", to: "zipCode" },
    { from: "countryRecId", to: "country", process: toCountry },
    { from: "phoneNbr", to: "phone" },
    { from: "phoneNbrFax", to: "fax" },
    { from: "inactiveFlag", to: "inactiveFlag" },
    { from: "defaultFlag", to: "defaultFlag" },
    { from: "defaultMailFlag", to: "defaultMailFlag" },
    { from: "defaultBillFlag", to: "defaultBillFlag" },
    { from: "defaultShipFlag", to: "defaultShipFlag" },
    { from: "updatedBy", to: "updatedById" },
    { from: "companyRecId", to: "companyId" },
    { from: "dateEnteredUtc", to: "createdAt" },
    { from: "lastUpdatedUtc", to: "updatedAt" },
  ],
};
