import { Company } from "../../types/ConnectWiseTypes";
import {
  CompanySourceRecord,
  NormalizedCompanyRecord,
} from "../../types/CompanySourceTypes";

const normalizeCompany = (
  company: Company,
): NormalizedCompanyRecord | null => {
  if (!company.identifier || !company.name) {
    return null;
  }

  return {
    id: company.id,
    identifier: company.identifier,
    name: company.name,
  };
};

export const normalizeCompanyRecord = (
  source: CompanySourceRecord,
): NormalizedCompanyRecord | null => {
  return normalizeCompany(source);
};

export const normalizeCompanyRecords = (
  sourceRecords: CompanySourceRecord[],
): NormalizedCompanyRecord[] => {
  return sourceRecords
    .map(normalizeCompanyRecord)
    .filter((company): company is NormalizedCompanyRecord => company !== null);
};
