/**
 * The dial-code default, now that it belongs to the forms rather than to
 * `CountryCodeSelect`'s mount effect.
 *
 * Two things are worth holding still. The first is that every form that
 * renders the control starts on a real dial code: the field is
 * `Yup.string().concat(YupRequiredString)` in four of them, so a builder that
 * reverts to `""` fails validation for a visitor who never touched the
 * control — and would do so silently, because the flag button would still show
 * Kuwait. The second is that moving the default in did not start sending a
 * guardian's dial code for an adult, which is the one behaviour the change
 * could have altered on the wire.
 */
import { test, describe } from "node:test";
import assert from "node:assert/strict";

import { DEFAULT_COUNTRY_SHORT_CODE, DEFAULT_DIAL_CODE } from "@/data/defaultCountry";
import { countryLabels } from "@/data/countryLabels";
import {
  EMERGENCY_CONTACT_FIELDS,
  emergencyContactPayload,
} from "@/features/auth/emergencyContact";
import { individualInitialValues } from "@/features/auth/components/individualRegistrationValues";
import { entitiesRegistrationInitialValues } from "@/features/auth/components/entitiesRegistrationValues";
import { registerVolunteerInitialValues } from "@/features/auth/components/registerVolunteerValues";

describe("DEFAULT_DIAL_CODE", () => {
  test("agrees with the country it names", () => {
    /* Derived from `countryLabels`, so it cannot drift — but the short code is
       written down, and `CountryCodeSelect` maps one to the other to pick the
       flag. This is the assertion that keeps the pair honest. */
    assert.equal(
      countryLabels[DEFAULT_COUNTRY_SHORT_CODE].secondary,
      DEFAULT_DIAL_CODE
    );
  });

  test("is the only entry with that dial code", () => {
    /* `CountryCodeSelect` resolves a flag by finding the *first* label whose
       dial code matches, so a shared code would show the wrong flag for the
       default. (`+1` is genuinely shared by US and CA — this asserts the
       default is not one of those.) */
    const matches = Object.entries(countryLabels).filter(
      ([, label]) => label.secondary === DEFAULT_DIAL_CODE
    );
    assert.deepEqual(matches.map(([code]) => code), [DEFAULT_COUNTRY_SHORT_CODE]);
  });
});

describe("registration initial values", () => {
  test("every form that renders the control starts on a real dial code", () => {
    for (const [name, values] of [
      ["individual", individualInitialValues],
      ["entities", entitiesRegistrationInitialValues],
      ["register-volunteer", registerVolunteerInitialValues],
    ] as const) {
      assert.equal(
        (values as { country_code: string }).country_code,
        DEFAULT_DIAL_CODE,
        `${name} must not start on an empty country_code`
      );
    }
  });

  test("the guardian dial code is seeded too, for when the block appears", () => {
    for (const values of [individualInitialValues, registerVolunteerInitialValues]) {
      assert.equal(
        (values as { emergency_contact_country_code: string })
          .emergency_contact_country_code,
        DEFAULT_DIAL_CODE
      );
    }
  });
});

describe("emergencyContactPayload", () => {
  const filled = {
    emergency_contact_name: "Noura",
    emergency_contact_phone: "50000000",
    emergency_contact_country_code: DEFAULT_DIAL_CODE,
    emergency_contact_civil_id: "299010100000",
    emergency_contact_relationship: "3",
  };

  test("passes a minor's answers through", () => {
    assert.deepEqual(emergencyContactPayload(filled, true), filled);
  });

  test("blanks all five for an adult", () => {
    const payload = emergencyContactPayload(filled, false);
    /* The behaviour the default could have changed: before it, an adult sent
       five empty strings because nothing had ever written to the fields. */
    for (const field of EMERGENCY_CONTACT_FIELDS) {
      assert.equal(payload[field], "", field);
    }
  });

  test("covers every field, so a caller cannot forget one", () => {
    assert.deepEqual(
      Object.keys(emergencyContactPayload(filled, true)).sort(),
      [...EMERGENCY_CONTACT_FIELDS].sort()
    );
  });

  test("a missing answer becomes an empty string, not undefined", () => {
    const partial = { ...filled, emergency_contact_civil_id: undefined as unknown as string };
    assert.equal(emergencyContactPayload(partial, true).emergency_contact_civil_id, "");
  });
});
