Files
community-rule/lib/create/formatRecommendationWhyLine.ts
T

74 lines
1.9 KiB
TypeScript

import type { GroupAnswers, RecommendationReason, TraitKey } from "./deckTypes";
export type RecommendationWhyCopy = {
why: {
fit: string;
fitWithAnswer: string;
opened: string;
added: string;
};
group: {
kindAndSize: string;
kindOnly: string;
sizeOnly: string;
kind: Record<string, string>;
size: Record<string, string>;
};
answerThirdPerson: Record<string, string>;
answerYouSaid: Record<string, string>;
};
export function formatGroupPhrase(
answers: GroupAnswers,
copy: RecommendationWhyCopy,
): string {
const kind = answers.mainKind ? copy.group.kind[answers.mainKind] : "";
const size = answers.size ? copy.group.size[answers.size] : "";
if (kind && size) {
return copy.group.kindAndSize
.replace("{kind}", kind)
.replace("{size}", size);
}
if (kind) return copy.group.kindOnly.replace("{kind}", kind);
if (size) return copy.group.sizeOnly.replace("{size}", size);
return "";
}
function answerPhrase(
key: TraitKey,
phrases: Record<string, string>,
): string {
return phrases[key] ?? key;
}
/**
* Why line for a recommended card in the compact seven.
* Near-universal cards keep their catalog description instead.
*/
export function formatRecommendationWhyLine(
reason: RecommendationReason,
answers: GroupAnswers,
copy: RecommendationWhyCopy,
): string {
if (reason.kind === "added") {
return copy.why.added.replace(
"{answer}",
answerPhrase(reason.answer, copy.answerYouSaid),
);
}
if (reason.kind === "opened") {
return copy.why.opened.replace(
"{answer}",
answerPhrase(reason.answer, copy.answerYouSaid),
);
}
const group = formatGroupPhrase(answers, copy);
if (!group) return "";
if (reason.answer) {
return copy.why.fitWithAnswer
.replace("{group}", group)
.replace("{answer}", answerPhrase(reason.answer, copy.answerThirdPerson));
}
return copy.why.fit.replace("{group}", group);
}