import { parsePhoneNumberWithError } from "libphonenumber-js";
import { PhoneNumber } from "../phone-number";

describe("PhoneNumber", () => {
  const validPhone = "+14155552671"; // Número válido en formato internacional
  const invalidPhone = "12345"; // Número inválido
  const nullablePhone = "";

  test("debe crear un número de teléfono válido", () => {
    const result = PhoneNumber.create(validPhone);
    expect(result.isSuccess).toBe(true);
    expect(result.data).toBeInstanceOf(PhoneNumber);
  });

  test("debe fallar al crear un número de teléfono inválido", () => {
    const result = PhoneNumber.create(invalidPhone);
    expect(result.isFailure).toBe(true);
    expect(result.getError()?.message).toBe(
      "Please specify a valid phone number (include the international prefix)."
    );
  });

  test("debe obtener el valor del número de teléfono", () => {
    const result = PhoneNumber.create(validPhone);
    expect(result.isSuccess).toBe(true);
    const phoneNumber = result.data;
    expect(phoneNumber?.getProps()).toBe(validPhone);
  });

  test("debe obtener el código de país del número", () => {
    const result = PhoneNumber.create(validPhone);
    expect(result.isSuccess).toBe(true);
    const phoneNumber = result.data;
    expect(phoneNumber?.getCountryCode()).toBe("US");
  });

  test("debe obtener el número nacional del teléfono", () => {
    const result = PhoneNumber.create(validPhone);
    expect(result.isSuccess).toBe(true);
    const phoneNumber = result.data;
    expect(phoneNumber?.getNationalNumber()).toBe("4155552671");
  });

  test("debe obtener el número formateado", () => {
    const result = PhoneNumber.create(validPhone);
    expect(result.isSuccess).toBe(true);
    const phoneNumber = result.data;
    expect(phoneNumber?.getNumber()).toBe(parsePhoneNumberWithError(validPhone).number.toString());
  });

  test("debe devolver undefined para la extensión si no hay una", () => {
    const result = PhoneNumber.create(validPhone);
    expect(result.isSuccess).toBe(true);
    const phoneNumber = result.data;
    expect(phoneNumber?.getExtension()).toBeUndefined();
  });
});
