/** 交易相关英文码 → 中文展示（库内仍存英文）。 */

const STATUS_ZH: Record<string, string> = {
  open: "持仓中",
  closed: "已平仓",
  option_residual: "期权残留待到期",
};

const SIDE_ZH: Record<string, string> = {
  long: "多",
  short: "空",
  call: "看涨",
  put: "看跌",
};

const LEG_ZH: Record<string, string> = {
  perp: "永续",
  option: "期权",
};

const ACTION_ZH: Record<string, string> = {
  open: "开仓",
  close: "平仓",
};

const CLOSE_REASON_ZH: Record<string, string> = {
  fixed_usdt: "固定净盈利达标·双腿全平",
  premium_multiple: "权利金倍数达标·双腿全平",
  target_perp_only: "净盈利达标·只平永续(期权归档)",
  expiry: "到期结算",
  emergency: "紧急全平",
  manual: "手动平仓",
  liquidity_retry: "等待流动性后全平",
  unknown: "未知",
};

export function statusZh(v: string | null | undefined): string {
  if (!v) return "—";
  return STATUS_ZH[v] || v;
}

export function sideZh(v: string | null | undefined): string {
  if (!v) return "—";
  return SIDE_ZH[v] || v;
}

/** 永续方向 / 期权方向，如「多/看跌」 */
export function positionSidesZh(
  perp: string | null | undefined,
  option: string | null | undefined,
): string {
  return `${sideZh(perp)}/${sideZh(option)}`;
}

export function fillDescZh(
  leg: string,
  action: string,
  side: string,
  closeReason?: string | null,
): string {
  const l = LEG_ZH[leg] || leg;
  const a = ACTION_ZH[action] || action;
  const s = SIDE_ZH[side] || side;
  if (leg === "option" && action === "close" && closeReason === "expiry") {
    return "期权到期结算";
  }
  // 期权买入开仓：「期权开多」；永续：「永续开多/开空」
  if (leg === "option" && action === "open") return "期权开多";
  if (leg === "option" && action === "close") return "期权平多";
  if (leg === "perp" && action === "open") return s === "多" ? "永续开多" : "永续开空";
  if (leg === "perp" && action === "close") return s === "多" ? "永续平多" : "永续平空";
  return `${l}${a}${s}`;
}

export function closeReasonZh(v: string | null | undefined): string {
  if (!v) return "—";
  return CLOSE_REASON_ZH[v] || v;
}
