import { z } from "zod";
const dictionaryEntrySortModes = [
  "label_asc",
  "label_desc",
  "value_asc",
  "value_desc",
  "created_at_asc",
  "created_at_desc"
];
const dictionaryEntrySortModeSchema = z.enum(dictionaryEntrySortModes);
const DEFAULT_DICTIONARY_ENTRY_SORT_MODE = "label_asc";
function resolveDictionaryEntrySortMode(value) {
  const parsed = dictionaryEntrySortModeSchema.safeParse(value);
  return parsed.success ? parsed.data : DEFAULT_DICTIONARY_ENTRY_SORT_MODE;
}
function compareText(left, right) {
  const leftText = typeof left === "string" ? left : "";
  const rightText = typeof right === "string" ? right : "";
  return leftText.localeCompare(rightText, void 0, { sensitivity: "base" });
}
function timestamp(value) {
  if (value instanceof Date) return value.getTime();
  if (typeof value === "number") return Number.isFinite(value) ? value : 0;
  if (typeof value === "string") {
    const parsed = Date.parse(value);
    return Number.isFinite(parsed) ? parsed : 0;
  }
  return 0;
}
function compareByMode(left, right, mode) {
  switch (mode) {
    case "label_desc":
      return compareText(right.label ?? right.value, left.label ?? left.value);
    case "value_asc":
      return compareText(left.value, right.value);
    case "value_desc":
      return compareText(right.value, left.value);
    case "created_at_asc":
      return timestamp(left.createdAt) - timestamp(right.createdAt);
    case "created_at_desc":
      return timestamp(right.createdAt) - timestamp(left.createdAt);
    case "label_asc":
    default:
      return compareText(left.label ?? left.value, right.label ?? right.value);
  }
}
function sortDictionaryEntries(entries, mode = DEFAULT_DICTIONARY_ENTRY_SORT_MODE) {
  return entries.slice().sort((left, right) => {
    const primary = compareByMode(left, right, mode);
    if (primary !== 0) return primary;
    return compareText(left.id, right.id);
  });
}
export {
  DEFAULT_DICTIONARY_ENTRY_SORT_MODE,
  dictionaryEntrySortModeSchema,
  dictionaryEntrySortModes,
  resolveDictionaryEntrySortMode,
  sortDictionaryEntries
};
//# sourceMappingURL=entrySort.js.map
