/** Base UI Select 会直接显示 `value`；筛选用哨兵时需手动映射展示文案。 */
export const ADMIN_SELECT_FILTER_ALL = "__all__";

export function isAdminSelectFilterAll(raw: unknown): boolean {
  const v = raw == null ? "" : String(raw);
  return v === "" || v === ADMIN_SELECT_FILTER_ALL;
}

export function adminSelectFilterLabel(
  raw: unknown,
  allLabel: string,
  resolveOptionLabel: (value: string) => string,
): string {
  const v = raw == null ? "" : String(raw);
  if (isAdminSelectFilterAll(v)) {
    return allLabel;
  }
  return resolveOptionLabel(v);
}

export type AdminSiteOption = { code: string; name?: string | null };

export function adminSiteSelectLabel(
  raw: unknown,
  sites: readonly AdminSiteOption[],
  allLabel: string,
): string {
  return adminSelectFilterLabel(raw, allLabel, (code) => {
    const site = sites.find((s) => s.code === code);
    if (!site) {
      return code;
    }
    return site.name ? `${site.name} (${site.code})` : site.code;
  });
}

export function adminSiteCodeLabel(
  raw: unknown,
  sites: readonly AdminSiteOption[],
  placeholder: string,
): string {
  const v = raw == null ? "" : String(raw);
  if (v === "") {
    return placeholder;
  }
  const site = sites.find((s) => s.code === v);
  if (!site) {
    return v;
  }
  return site.name ? `${site.name} (${site.code})` : site.code;
}
