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

import {
  getFirstPaymentSearchParam,
  parsePaymentPayChannel,
  parsePaymentReturnSearchParams,
  parseSubscriptionReturnTo,
} from "../payment_search_params";

describe("payment search params", () => {
  it("takes the first value from repeated query parameters", () => {
    expect(getFirstPaymentSearchParam(["first", "second"])).toBe("first");
    expect(getFirstPaymentSearchParam([])).toBeNull();
  });

  it("accepts only supported payment channels", () => {
    expect(parsePaymentPayChannel("stripe")).toBe("stripe");
    expect(parsePaymentPayChannel(["ezpay", "stripe"])).toBe("ezpay");
    expect(parsePaymentPayChannel("paypal")).toBeNull();
    expect(parsePaymentPayChannel(undefined)).toBeNull();
  });

  it("parses the shared payment return context", () => {
    expect(
      parsePaymentReturnSearchParams({
        payChannel: ["ezpay", "stripe"],
        paymentReturn: "1",
      }),
    ).toEqual({
      initialPayChannel: "ezpay",
      shouldResumePendingOrder: true,
    });

    expect(parsePaymentReturnSearchParams({ paymentReturn: "0" })).toEqual({
      initialPayChannel: null,
      shouldResumePendingOrder: false,
    });
  });

  it("accepts only supported subscription return targets", () => {
    expect(parseSubscriptionReturnTo("chat")).toBe("chat");
    expect(parseSubscriptionReturnTo("private-zone")).toBe("private-zone");
    expect(parseSubscriptionReturnTo("profile")).toBe("profile");
    expect(parseSubscriptionReturnTo(["private-zone", "chat"])).toBe(
      "private-zone",
    );
    expect(parseSubscriptionReturnTo("tip")).toBeNull();
    expect(parseSubscriptionReturnTo(undefined)).toBeNull();
  });
});
