export type TransferFilters = {
  playerId: string;
  playerAccount: string;
  transferNo: string;
  externalRefNo: string;
  createdFrom: string;
  createdTo: string;
  statusCsv: string;
  abnormalOnly: boolean;
};

export const emptyTransferFilters: TransferFilters = {
  playerId: "",
  playerAccount: "",
  transferNo: "",
  externalRefNo: "",
  createdFrom: "",
  createdTo: "",
  statusCsv: "",
  abnormalOnly: false,
};

export function transferFiltersFromSearchParams(searchParams: URLSearchParams): TransferFilters {
  return {
    ...emptyTransferFilters,
    playerId: (searchParams.get("player_id") ?? "").trim(),
    transferNo: (searchParams.get("transfer_no") ?? "").trim(),
    externalRefNo: (searchParams.get("external_ref_no") ?? "").trim(),
    abnormalOnly: searchParams.get("abnormal") === "1",
  };
}

export type TxnFilters = {
  playerId: string;
  playerAccount: string;
  txnNo: string;
  externalRefNo: string;
  bizType: string;
  statusCsv: string;
  createdFrom: string;
  createdTo: string;
  abnormalOnly: boolean;
};

export const emptyTxnFilters: TxnFilters = {
  playerId: "",
  playerAccount: "",
  txnNo: "",
  externalRefNo: "",
  bizType: "",
  statusCsv: "",
  createdFrom: "",
  createdTo: "",
  abnormalOnly: false,
};

export function txnFiltersFromSearchParams(searchParams: URLSearchParams): TxnFilters {
  const abnormalOnly = searchParams.get("abnormal") === "1";
  return {
    ...emptyTxnFilters,
    playerId: (searchParams.get("player_id") ?? "").trim(),
    txnNo: (searchParams.get("txn_no") ?? "").trim(),
    externalRefNo: (searchParams.get("external_ref_no") ?? "").trim(),
    abnormalOnly,
    statusCsv: abnormalOnly ? "" : emptyTxnFilters.statusCsv,
  };
}

export function txnFiltersHaveUrlPreset(filters: TxnFilters): boolean {
  return (
    filters.txnNo !== "" ||
    filters.playerId !== "" ||
    filters.externalRefNo !== "" ||
    filters.abnormalOnly
  );
}

/** 下拉「不限」值；请求时转为空串不传参 */
export const WALLET_FILTER_ALL = "__all__";

/** 与玩家端资金流水公共类型一致；接口会展开到底层 wallet_txns.biz_type */
export const WALLET_TXN_BIZ_OPTIONS: { value: string; label: string }[] = [
  { value: "transfer_in", label: "transferIn" },
  { value: "transfer_out", label: "transferOut" },
  { value: "bet", label: "bizBetDeduct" },
  { value: "prize", label: "bizPrize" },
  { value: "refund", label: "bizRefund" },
  { value: "reversal", label: "bizReversal" },
];

/** 与 {@see WalletTransactionListController::ALLOWED_STATUS} 一致 */
export const WALLET_TXN_STATUS_OPTIONS: { value: string; label: string }[] = [
  { value: "posted", label: "statusPosted" },
  { value: "pending_reconcile", label: "statusPendingReconcile" },
  { value: "reversed", label: "statusReversed" },
];

/** 与 {@see TransferOrderListController::ALLOWED_STATUS} 一致 */
export const TRANSFER_ORDER_STATUS_OPTIONS: { value: string; label: string }[] = [
  { value: "processing", label: "statusProcessing" },
  { value: "success", label: "statusSuccess" },
  { value: "failed", label: "statusFailed" },
  { value: "pending_reconcile", label: "statusPendingReconcile" },
  { value: "reversed", label: "statusReversed" },
  { value: "manually_processed", label: "statusCaseClosed" },
];

export function walletAdminSelectDisplayedLabel(
  raw: unknown,
  options: readonly { value: string; label: string }[],
  t?: (key: string) => string,
): string {
  const v = raw == null ? "" : String(raw);
  if (v === "" || v === WALLET_FILTER_ALL) {
    return t ? t("filterAll") : "All";
  }
  const key = options.find((o) => o.value === v)?.label;
  return key ? (t ? t(key) : key) : v;
}
