"use client";
import { apiCall } from "@open-mercato/ui/backend/utils/apiCall";
function normalizeDictionaryList(items) {
  if (!Array.isArray(items)) return [];
  return items.map((item) => {
    if (!item || typeof item !== "object") return null;
    const record = item;
    const id = typeof record.id === "string" ? record.id : null;
    const key = typeof record.key === "string" ? record.key : null;
    if (!id || !key) return null;
    return { id, key };
  }).filter((item) => !!item);
}
function normalizeDictionaryEntries(items) {
  if (!Array.isArray(items)) return [];
  return items.map((item) => {
    if (!item || typeof item !== "object") return null;
    const record = item;
    const id = typeof record.id === "string" ? record.id : null;
    const value = typeof record.value === "string" ? record.value : null;
    if (!id || !value) return null;
    return {
      id,
      value,
      label: typeof record.label === "string" && record.label.trim().length > 0 ? record.label : value,
      color: typeof record.color === "string" ? record.color : null,
      icon: typeof record.icon === "string" ? record.icon : null
    };
  }).filter((item) => !!item);
}
async function loadDictionaryEntriesByKey(key) {
  const dictionariesCall = await apiCall("/api/dictionaries");
  if (!dictionariesCall.ok) return [];
  const dictionary = normalizeDictionaryList(dictionariesCall.result?.items).find((item) => item.key === key);
  if (!dictionary) return [];
  const entriesCall = await apiCall(`/api/dictionaries/${dictionary.id}/entries`);
  if (!entriesCall.ok) return [];
  return normalizeDictionaryEntries(entriesCall.result?.items);
}
export {
  loadDictionaryEntriesByKey
};
//# sourceMappingURL=clientEntries.js.map
