// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only

import React from 'react';
import lodash from 'lodash';

import { I18n } from './I18n.dom.tsx';
import type { LocalizerType } from '../types/Util.std.ts';
import { UserText } from './UserText.dom.tsx';

const { take } = lodash;

type PropsType = {
  i18n: LocalizerType;
  nameClassName?: string;
  sharedGroupNames: ReadonlyArray<string>;
};

export function SharedGroupNames({
  i18n,
  nameClassName,
  sharedGroupNames,
}: PropsType): React.JSX.Element {
  const firstThreeGroups = take(sharedGroupNames, 3).map((group, i) => (
    // We cannot guarantee uniqueness of group names
    // oxlint-disable-next-line react/no-array-index-key
    <strong key={i} className={nameClassName}>
      <UserText text={group} />
    </strong>
  ));

  if (sharedGroupNames.length >= 5) {
    const remainingCount = sharedGroupNames.length - 3;
    return (
      <I18n
        i18n={i18n}
        id="icu:member-of-more-than-3-groups--multiple-more"
        components={{
          // oxlint-disable-next-line typescript/no-non-null-assertion
          group1: firstThreeGroups[0]!,
          // oxlint-disable-next-line typescript/no-non-null-assertion
          group2: firstThreeGroups[1]!,
          // oxlint-disable-next-line typescript/no-non-null-assertion
          group3: firstThreeGroups[2]!,
          remainingCount,
        }}
      />
    );
  }
  if (sharedGroupNames.length === 4) {
    return (
      <I18n
        i18n={i18n}
        id="icu:member-of-more-than-3-groups--one-more"
        components={{
          // oxlint-disable-next-line typescript/no-non-null-assertion
          group1: firstThreeGroups[0]!,
          // oxlint-disable-next-line typescript/no-non-null-assertion
          group2: firstThreeGroups[1]!,
          // oxlint-disable-next-line typescript/no-non-null-assertion
          group3: firstThreeGroups[2]!,
        }}
      />
    );
  }
  if (firstThreeGroups.length === 3) {
    return (
      <I18n
        i18n={i18n}
        id="icu:member-of-3-groups"
        components={{
          // oxlint-disable-next-line typescript/no-non-null-assertion
          group1: firstThreeGroups[0]!,
          // oxlint-disable-next-line typescript/no-non-null-assertion
          group2: firstThreeGroups[1]!,
          // oxlint-disable-next-line typescript/no-non-null-assertion
          group3: firstThreeGroups[2]!,
        }}
      />
    );
  }
  if (firstThreeGroups.length >= 2) {
    return (
      <I18n
        i18n={i18n}
        id="icu:member-of-2-groups"
        components={{
          // oxlint-disable-next-line typescript/no-non-null-assertion
          group1: firstThreeGroups[0]!,
          // oxlint-disable-next-line typescript/no-non-null-assertion
          group2: firstThreeGroups[1]!,
        }}
      />
    );
  }
  if (firstThreeGroups.length >= 1) {
    return (
      <I18n
        i18n={i18n}
        id="icu:member-of-1-group"
        components={{
          // oxlint-disable-next-line typescript/no-non-null-assertion
          group: firstThreeGroups[0]!,
        }}
      />
    );
  }

  return <>{i18n('icu:no-groups-in-common')}</>;
}
