export function checkMsgBase(ev, data) {
  if (typeof data === "object") {
    const { func, from, to } = data;
    if ([func, from, to].includes(void 0))
      throw new Error("6");
    if (from === ev)
      throw new Error("-1");
  } else
    throw new Error("7");
}
export function checkSendMsg(data) {
  const { func, token, from, to, message } = data;
  if ([func, from, to].includes(void 0))
    throw new Error("6");
  if ([func, from, to].some((item) => typeof item !== "string"))
    throw new Error("6");
  if ([from, to].some((item) => !["PAGE", "CONTENT", "BACKGROUND", "POPUP"].includes(item)))
    throw new Error("6");
  if (message && typeof message !== "object")
    throw new Error("6");
  try {
    if (message && typeof message === "object" && typeof message.jsonString === "string")
      JSON.parse(message.jsonString);
  } catch (error) {
    throw new Error("9");
  }
  return data;
}
export function checkRecvMsg(data) {
  const { func, from, to, code, codeInfo, message } = data;
  if ([func, from, to, code, codeInfo].includes(void 0))
    throw new Error("6");
  if ([func, from, to, codeInfo].some((item) => typeof item !== "string"))
    throw new Error("6");
  if (typeof code !== "number")
    throw new Error("6");
  if ([from, to].some((item) => !["PAGE", "CONTENT", "BACKGROUND", "POPUP"].includes(item)))
    throw new Error("6");
  if (message && typeof message !== "object")
    throw new Error("6");
  try {
    if (message && typeof message === "object" && typeof message.jsonString === "string")
      JSON.parse(message.jsonString);
  } catch (error) {
    throw new Error("9");
  }
  return data;
}
const errorCodeMap = /* @__PURE__ */ new Map();
errorCodeMap.set(0, "ok");
errorCodeMap.set(1, "token_error");
errorCodeMap.set(2, "no_permission");
errorCodeMap.set(3, "request_timeout");
errorCodeMap.set(4, "user_cancellation");
errorCodeMap.set(5, "uninitialized_sql_database");
errorCodeMap.set(6, "missing_parameter");
errorCodeMap.set(7, "invalid_parameters");
errorCodeMap.set(8, "sql_execution_error");
errorCodeMap.set(9, "jsonString_error");
errorCodeMap.set(10, "mnemonic_phrase_incorrect");
errorCodeMap.set(11, "public_key_error");
export function transToErrMsg(func, from, err, addInfo = "") {
  if (err instanceof Error) {
    const code = Number(err.message);
    if (code < 0)
      return;
    return {
      func,
      from,
      to: "PAGE",
      code,
      codeInfo: errorCodeMap.get(code) + addInfo
    };
  }
}
export function responseOk(func, from, obj) {
  const code = 0;
  return {
    func,
    from,
    to: "PAGE",
    code,
    codeInfo: errorCodeMap.get(code),
    ...obj
  };
}
export function catchErrMsg(data, checkFn) {
  let errRecv;
  try {
    checkFn(data);
  } catch (err) {
    const code = Number(err.value);
    if (code < 0)
      return;
    errRecv = {
      func: data.func,
      from: data.to,
      to: data.from,
      code,
      codeInfo: errorCodeMap.get(code)
    };
  }
  return errRecv;
}
