/**
 * Formats a name by combining first name, last name, and organisation name.
 *
 * The function follows these formatting rules:
 * - Names are formatted as "LastName FirstName"
 * - Organisation name is separated from the personal name with " - "
 * - Empty or undefined values are filtered out
 * - If all values are empty/undefined, returns an empty string
 *
 * @param params - The name components to format
 * @param params.firstName - The person's first name (optional)
 * @param params.lastName - The person's last name (optional)
 * @param params.organisationName - The organisation name (optional)
 * @returns The formatted name string
 *
 * @example
 * formatName({ firstName: 'John', lastName: 'Doe' })
 * // Returns: "Doe John"
 *
 * @example
 * formatName({ firstName: 'John', lastName: 'Doe', organisationName: 'Acme Corp' })
 * // Returns: "Acme Corp - Doe John"
 *
 * @example
 * formatName({ organisationName: 'Acme Corp' })
 * // Returns: "Acme Corp"
 */
export const formatName = ({
  firstName,
  lastName,
  organisationName,
}: {
  firstName?: string;
  lastName?: string;
  organisationName?: string;
}): string => {
  const nameCombined = [lastName, firstName].filter((f) => !!f);
  const organisation = [organisationName].filter((f) => !!f);

  return (
    [organisation.join(), nameCombined.join(' ')]
      .filter((f) => !!f)
      .join(' - ') ?? ''
  );
};
