import { describe, expect, it } from "vitest";

import {
  build1688ContactPageUrls,
  extractDetectedContacts,
  hasDetectedPhoneContact,
  isCompletePhoneContactValue,
  mergeDetectedContacts,
} from "./contact-utils.js";

describe("extractDetectedContacts", () => {
  it("keeps phone empty when the contact page only exposes a labeled mobile number", () => {
    const contacts = extractDetectedContacts(
      "联系方式 河北乘润建材有限公司 电话：暂无 手机：17736998988 传真：暂无 地址：河北省沧州市新华区开发区赵家赵家坟村",
      "contact_page",
    );

    expect(contacts.find((item) => item.type === "phone")).toBeUndefined();
    expect(contacts.find((item) => item.type === "mobile")?.value).toBe("17736998988");
  });

  it("separates labeled landline and mobile values", () => {
    const contacts = extractDetectedContacts(
      "电话：0317-88886666 手机：17736998988",
      "contact_page",
    );

    expect(contacts.find((item) => item.type === "phone")?.value).toBe("0317-88886666");
    expect(contacts.find((item) => item.type === "mobile")?.value).toBe("17736998988");
  });

  it("builds https contactinfo urls with both supported spm variants", () => {
    expect(build1688ContactPageUrls("http://shop1435947366z75.1688.com/?tracelog=p4p")).toEqual([
      "https://shop1435947366z75.1688.com/page/contactinfo.htm?spm=a2615.7691456.wp_pc_common_topnav.0",
      "https://shop1435947366z75.1688.com/page/contactinfo.htm?spm=a2615.2177701.wp_pc_common_topnav.0",
      "https://shop1435947366z75.1688.com/page/contactinfo.htm",
    ]);
  });

  it("treats only real phone or mobile values as valid contacts", () => {
    expect(hasDetectedPhoneContact([{ type: "email", value: "sales@example.com" }])).toBe(false);
    expect(hasDetectedPhoneContact([{ type: "phone", value: "暂无" }])).toBe(false);
    expect(hasDetectedPhoneContact([{ type: "mobile", value: "86" }])).toBe(false);
    expect(hasDetectedPhoneContact([{ type: "mobile", value: "+86" }])).toBe(false);
    expect(hasDetectedPhoneContact([{ type: "phone", value: "086" }])).toBe(false);
    expect(hasDetectedPhoneContact([{ type: "mobile", value: "1380013" }])).toBe(false);
    expect(hasDetectedPhoneContact([{ type: "mobile", value: "17736998988" }])).toBe(true);
  });

  it("validates complete mobile and landline contact values", () => {
    expect(isCompletePhoneContactValue("mobile", "13800138000")).toBe(true);
    expect(isCompletePhoneContactValue("mobile", "+86 13800138000")).toBe(true);
    expect(isCompletePhoneContactValue("phone", "0571-88886666")).toBe(true);
    expect(isCompletePhoneContactValue("phone", "010-12345678")).toBe(true);
    expect(isCompletePhoneContactValue("mobile", "86")).toBe(false);
    expect(isCompletePhoneContactValue("phone", "+86")).toBe(false);
    expect(isCompletePhoneContactValue("phone", "12345")).toBe(false);
    expect(isCompletePhoneContactValue("phone", "0000-0000008")).toBe(false);
    expect(isCompletePhoneContactValue("phone", "0000-00000008")).toBe(false);
    expect(isCompletePhoneContactValue("phone", "0571-00000000")).toBe(false);
  });

  it("merges and deduplicates contacts across sources", () => {
    expect(
      mergeDetectedContacts(
        [
          { type: "mobile", value: "17736998988", source: "product_detail", context: "手机: 17736998988" },
          { type: "email", value: "sales@example.com", source: "product_detail", context: "邮箱" },
        ],
        [
          { type: "mobile", value: "17736998988", source: "contact_page", context: "联系手机: 17736998988" },
          { type: "phone", value: "0317-88886666", source: "contact_page", context: "电话" },
        ],
      ),
    ).toEqual([
      { type: "mobile", value: "17736998988", source: "product_detail", context: "手机: 17736998988" },
      { type: "email", value: "sales@example.com", source: "product_detail", context: "邮箱" },
      { type: "phone", value: "0317-88886666", source: "contact_page", context: "电话" },
    ]);
  });
});
