export const sortByProperty = <T>(
  firstEntity: T,
  secondEntity: T,
  property: keyof T,
) => {
  if (!firstEntity[property]) return 1;
  if (!secondEntity[property]) return -1;
  return firstEntity[property]
    .toString()
    .localeCompare(secondEntity[property].toString());
};

export const sortByEntityAndDomain = <
  T extends { Entity: string; Domain: string },
>(
  firstEntity: T,
  secondEntity: T,
) => {
  const property = 'Entity';

  if (!firstEntity[property]) {
    return 1;
  }
  if (!secondEntity[property]) {
    return -1;
  }

  if (firstEntity[property] === secondEntity[property]) {
    return sortByProperty(firstEntity, secondEntity, 'Domain');
  }

  return firstEntity[property].localeCompare(secondEntity[property]);
};

export const sortFontInfo = <
  T extends { 'Use Case': string; 'Website/App Title': string },
>(
  firstEntity: T,
  secondEntity: T,
) => {
  const property = 'Use Case';
  const nestedProperty = 'Website/App Title';
  if (!firstEntity[property]) {
    return 1;
  }
  if (!secondEntity[property]) {
    return -1;
  }

  if (firstEntity[property] === secondEntity[property]) {
    if (!firstEntity[nestedProperty]) {
      return 1;
    }
    if (!secondEntity[nestedProperty]) {
      return -1;
    }

    return firstEntity[nestedProperty].localeCompare(
      secondEntity[nestedProperty],
    );
  }

  return firstEntity[property].localeCompare(secondEntity[property]);
};
