function uniqueNormalizedValues(values: Array<string | undefined>): string[] {
  const result: string[] = [];
  const seen = new Set<string>();
  for (const value of values) {
    const normalized = value?.trim();
    if (!normalized) continue;
    const dedupeKey = normalized.toLocaleLowerCase();
    if (seen.has(dedupeKey)) continue;
    seen.add(dedupeKey);
    result.push(normalized);
  }
  return result;
}

export function buildPriorityHintText({
  attachmentNames,
  skillIds,
}: {
  attachmentNames: string[];
  skillIds: string[];
}): string {
  const attachments = uniqueNormalizedValues(attachmentNames);
  const skills = uniqueNormalizedValues(skillIds);
  if (attachments.length === 0 && skills.length === 0) {
    return "";
  }

  const attachmentPart =
    attachments.length > 0 ? `【${attachments.join("、")}】` : "";
  const skillPart = skills.length > 0 ? `【${skills.join("、")}】` : "";

  if (attachmentPart && skillPart) {
    return `XClaw优先使用${attachmentPart}和${skillPart}`;
  }
  return `XClaw优先使用${attachmentPart || skillPart}`;
}

export function composeSubmitText({
  baseText,
  attachmentNames,
  skillIds,
}: {
  baseText: string;
  attachmentNames: string[];
  skillIds: string[];
}): string {
  const trimmedBase = baseText.trim();
  if (!trimmedBase) return trimmedBase;
  const priorityHint = buildPriorityHintText({
    attachmentNames,
    skillIds,
  });
  if (!priorityHint) return trimmedBase;
  return `${trimmedBase}\n${priorityHint}`;
}
