/**
 * Presentational tidy for a merchant name (board 212).
 *
 * PRESENTATION ONLY. It merges nothing and must never be used to decide that
 * two rows are the same merchant. Real unification is the merchant-alias bridge
 * (ticket 176, `jobs/merchant_merge.py`), which has already merged what can be
 * merged safely; what is left on screen is one entity looking scruffy, not two
 * entities that should be one.
 *
 * `Amazon.in` MUST NOT become `amazon.com.au`. They are different marketplaces
 * with different currency and geography — a genuine country distinction, not a
 * name variant. `Kogan.com`, `GOG.com`, `AliExpress.com` and `Catch.com.au` are
 * real brand names that happen to contain a TLD and are already correct.
 *
 * Two rules, both mechanical:
 *   1. drop trailing corporate suffixes
 *   2. capitalise a name that arrived lower-cased FROM A DOMAIN
 */

const CORP_SUFFIX =
  /,?\s+(pty\.?\s+ltd\.?|pty\.?\s+limited|pte\.?\s+ltd\.?|pte\.?|p\/l|ltd\.?|limited|inc\.?|llc|pbc|gmbh|b\.?v\.?|s\.?a\.?r\.?l\.?|oü|co\.?)$/i;

/** A bare domain used as a name: all lower case, and a dotted TLD. */
const BARE_DOMAIN = /^[a-z0-9][a-z0-9.-]*\.[a-z]{2,}(\.[a-z]{2,})?$/;

/**
 * Stripping must not leave a dangling connector. `Coburger & Co` is a brand
 * whose last word happens to match the suffix list, and removing it produced
 * `Coburger &` — visibly broken, and the kind of thing that only shows up when
 * you run the rule over the real corpus rather than over examples you chose.
 */
const DANGLING = /(\s[&+-]|\sand)$/i;

export function tidyMerchant(name: string): string {
  let n = name.replace(/\s+/g, " ").trim();

  // At most two passes: "Samsung Electronics Co. Ltd." needs Ltd. then Co.
  for (let i = 0; i < 2; i++) {
    const stripped = n.replace(CORP_SUFFIX, "").trim().replace(/,$/, "");
    if (stripped === n || stripped.length < 3 || DANGLING.test(stripped)) break;
    n = stripped;
  }

  // "amazon.com.au" reads as a machine artefact; "Amazon.com.au" reads as a
  // name. Gated on BARE_DOMAIN, not on "starts with a lower-case letter": the
  // looser test turned `eBay Commerce Australia Pty Ltd.` into `EBay Commerce
  // Australia`, mangling a brand that is deliberately lower-cased.
  if (BARE_DOMAIN.test(n)) n = n[0].toUpperCase() + n.slice(1);

  return n;
}
