import type {
  DictionaryName,
  DictionaryOption,
  UiDictionaries,
} from "./dictionaryTypes";

export function dictionaryOptions(
  dictionaries: UiDictionaries,
  name: DictionaryName,
): readonly DictionaryOption[] {
  return [...dictionaries.dictionaries[name]].sort(
    (left, right) =>
      left.sortNo - right.sortNo || left.code.localeCompare(right.code),
  );
}

export function dictionaryLabel(
  dictionaries: UiDictionaries,
  name: DictionaryName,
  code: string | null | undefined,
): string {
  if (code === null || code === undefined || code === "") return "未设置";
  return (
    dictionaries.dictionaries[name].find((item) => item.code === code)?.label ??
    "未定义标签"
  );
}
