import { Base64, Token } from "authly";
import { Card } from "@payfunc/model-card";
export async function from(error, merchant, value) {
  var _a, _b;
  return {
    show: error.content.details.visible,
    url: await getIframeUrl((_b = (_a = merchant.card) === null || _a === void 0 ? void 0 : _a.url) !== null && _b !== void 0 ? _b : "", error.content.details.url, error.content.details.data, merchant, value),
    data: error.content.details.data,
  };
}
export async function getIframeUrl(issuer, url, data, merchant, value) {
  var _a, _b, _c, _d, _e, _f, _g, _h;
  let card;
  if (Token.is(value))
    card = (_b = (_a = (await Card.V1.Token.verify(value))) === null || _a === void 0 ? void 0 : _a.card) !== null && _b !== void 0 ? _b : (await Card.Token.verify(value));
  const version = ["2.1.0", "2.2.0"].includes(data.messageVersion)
    ? data.messageVersion
    : undefined;
  const callback = Card.Token.is(card) && issuer
    ? Card.Token.getVerificationTarget(card, issuer, encodeURIComponent(window.location.origin), version)
    : `${issuer}/card/${card}/verification?mode=iframe&merchant=${(_e = (_d = (_c = merchant === null || merchant === void 0 ? void 0 : merchant.card) === null || _c === void 0 ? void 0 : _c.id) !== null && _d !== void 0 ? _d : merchant === null || merchant === void 0 ? void 0 : merchant.sub) !== null && _e !== void 0 ? _e : "error"}&parent=${encodeURIComponent(window.location.origin)}${version ? `&v=${version}` : ""}`;
  data = (_h = (_g = (_f = ch3d1Data(data, callback, value)) !== null && _f !== void 0 ? _f : ch3d2MethodData(data, callback)) !== null && _g !== void 0 ? _g : ch3d2ChallengeData(data)) !== null && _h !== void 0 ? _h : data;
  const result = typeof data == "object" && Object.entries(data).length > 0
    ? Object.entries(data).reduce((a, b) => a + "&" + encodeURIComponent(b[0]) + "=" + encodeURIComponent(b[1]), "")
    : "";
  return "https://api.payfunc.com/redirect/post?target=" + encodeURIComponent(url) + result;
}
function ch3d1Data(data, callback, value) {
  let result;
  if (data.PaReq || data.pareq) {
    result = Object.assign({}, data);
    if (result.pareq) {
      result.PaReq = result.pareq;
      delete result.pareq;
    }
    if (!result.TermUrl)
      result.TermUrl = callback;
    if (!result.MD)
      result.MD = value !== null && value !== void 0 ? value : "error";
  }
  return result;
}
function ch3d2MethodData(data, callback) {
  let result;
  if (data.type == "method" && data.threeDSServerTransID)
    result = {
      threeDSMethodData: Base64.encode(JSON.stringify({
        threeDSServerTransID: data.threeDSServerTransID,
        threeDSMethodNotificationURL: callback,
      }), "url"),
    };
  return result;
}
function ch3d2ChallengeData(data) {
  let result;
  if (data.type == "challenge" && data.threeDSServerTransID)
    result = {
      creq: Base64.encode(JSON.stringify({
        threeDSServerTransID: data.threeDSServerTransID,
        acsTransID: data.acsTransID,
        messageVersion: data.messageVersion,
        messageType: data.messageType,
        challengeWindowSize: data.challengeWindowSize,
      }), "url"),
    };
  return result;
}
