function normalizeSubExpertiseLabel(value?: string | null) {
  return String(value || "").trim();
}

export function subExpertiseListIncludes(
  list: string[] | undefined,
  value?: string | null
) {
  const needle = normalizeSubExpertiseLabel(value);
  if (!needle) return false;
  return (list || []).some(
    (item) => normalizeSubExpertiseLabel(item) === needle
  );
}

export function resolveDisplaySubExpertise(
  subExpertise: string[] | undefined,
  displaySubExpertise?: string | null
): string | null {
  const list = (subExpertise || [])
    .map((item) => normalizeSubExpertiseLabel(item))
    .filter(Boolean);
  if (!list.length) return null;

  const display = normalizeSubExpertiseLabel(displaySubExpertise);
  if (display && list.includes(display)) return display;
  return list[0];
}

export function getPostSubExpertiseLabels(
  _mainExpertise?: string | null,
  subExpertise: string[] = [],
  displaySubExpertise?: string | null
): string[] {
  const display = resolveDisplaySubExpertise(subExpertise, displaySubExpertise);
  return display ? [display] : [];
}
