/**
 * `getApiFieldErrors` / `getApiErrorMessages` against both envelope shapes.
 *
 * Fourteen `catch` blocks are being converted from hand-rolled iteration over
 * `error.response.data.errors[field][language]` to these helpers, so the
 * helpers' selection order has to be pinned before those call sites rely on
 * it. CLAUDE.md records that both shapes are live: the current
 * `response_status.validation_errors`, and an older
 * `errors: { field: { en, ar } }` that parts of the app still hit.
 */
import { test, describe } from "node:test";
import assert from "node:assert/strict";

import {
  getApiErrorMessage,
  getApiErrorMessages,
  getApiFieldErrors,
} from "@/lib/api/errors";

const thrown = (body: unknown) => ({ response: { data: body } });

describe("getApiFieldErrors — the legacy { en, ar } shape", () => {
  const legacy = thrown({
    errors: {
      civil_id: { en: "Already used", ar: "مستخدم بالفعل" },
      email: { en: "Invalid", ar: "غير صالح" },
    },
  });

  test("picks the requested language per field", () => {
    assert.deepEqual(getApiFieldErrors(legacy, "en"), {
      civil_id: "Already used",
      email: "Invalid",
    });
    assert.deepEqual(getApiFieldErrors(legacy, "ar"), {
      civil_id: "مستخدم بالفعل",
      email: "غير صالح",
    });
  });

  test("falls back to en, then ar, when the language is missing", () => {
    assert.deepEqual(
      getApiFieldErrors(thrown({ errors: { a: { en: "only-en" } } }), "ar"),
      { a: "only-en" }
    );
    assert.deepEqual(
      getApiFieldErrors(thrown({ errors: { a: { ar: "only-ar" } } }), "en"),
      { a: "only-ar" }
    );
  });

  test("one toast per field, which is what the replaced loops produced", () => {
    assert.equal(getApiErrorMessages(legacy, "en").length, 2);
  });
});

describe("getApiFieldErrors — the current validation_errors shape", () => {
  test("reads a string[] per field and takes the first", () => {
    assert.deepEqual(
      getApiFieldErrors(
        thrown({
          response_status: {
            validation_errors: { civil_id: ["Already used", "second"] },
          },
        }),
        "en"
      ),
      { civil_id: "Already used" }
    );
  });

  test("a bare string value is used as-is", () => {
    assert.deepEqual(
      getApiFieldErrors(
        thrown({ response_status: { validation_errors: { a: "boom" } } }),
        "en"
      ),
      { a: "boom" }
    );
  });

  test("validation_errors wins over the legacy errors key when both are sent", () => {
    assert.deepEqual(
      getApiFieldErrors(
        thrown({
          response_status: { validation_errors: { a: ["new"] } },
          errors: { a: { en: "old" } },
        }),
        "en"
      ),
      { a: "new" }
    );
  });

  test("a success-shaped empty array is not treated as field errors", () => {
    assert.deepEqual(
      getApiFieldErrors(
        thrown({ response_status: { validation_errors: [] } }),
        "en"
      ),
      {}
    );
  });
});

describe("the fallback chain the converted call sites depend on", () => {
  test("field errors come before msg", () => {
    assert.deepEqual(
      getApiErrorMessages(
        thrown({ msg: "Validation error", errors: { a: { en: "field" } } }),
        "en"
      ),
      ["field"]
    );
  });

  test("msg is used when there are no field errors", () => {
    assert.deepEqual(getApiErrorMessages(thrown({ msg: "Boom" }), "en"), [
      "Boom",
    ]);
  });

  test("nothing usable yields an empty list, so the caller's fallback runs", () => {
    assert.deepEqual(getApiErrorMessages(thrown({}), "en"), []);
    assert.deepEqual(getApiErrorMessages(new Error("offline"), "en"), []);
    assert.equal(getApiErrorMessage(thrown({}), "en", "generic"), "generic");
  });

  test("an unwrapped body works as well as a thrown axios error", () => {
    assert.deepEqual(getApiErrorMessages({ errors: { a: { en: "x" } } }, "en"), [
      "x",
    ]);
  });
});
