export interface BurstLocationErrorNotice {
  message: string;
  description: string;
}

const DATA_GAP_PATTERN =
  /^(爆管压力数据|正常压力数据|爆管流量数据|正常流量数据) 在时间窗内无有效模拟数据: (.+)$/;

const extractErrorDetail = (error: unknown): string => {
  const candidate = error as {
    message?: string;
    response?: { data?: { detail?: unknown } };
  };
  const detail = candidate?.response?.data?.detail;

  if (typeof detail === "string" && detail.trim()) {
    return detail.trim();
  }
  if (typeof candidate?.message === "string" && candidate.message.trim()) {
    return candidate.message.trim();
  }
  return "请求失败";
};

export const getBurstLocationErrorNotice = (
  error: unknown,
): BurstLocationErrorNotice => {
  const detail = extractErrorDetail(error);
  const match = detail.match(DATA_GAP_PATTERN);

  if (!match) {
    return {
      message: "提交分析失败",
      description: detail,
    };
  }

  const [, dataLabel, missingIds] = match;
  if (dataLabel.startsWith("正常")) {
    return {
      message: "缺少正常模拟基线",
      description: `所选方案时间窗内没有可用的实时模拟${dataLabel}，缺失点位：${missingIds}。请选择已有正常基线覆盖的方案或时间窗，系统不会自动补数据。`,
    };
  }

  return {
    message: "所选方案数据不完整",
    description: `所选方案时间窗内没有可用的${dataLabel}，缺失点位：${missingIds}。请选择数据完整的模拟方案。`,
  };
};
