"use client";
import { apiCall } from "@open-mercato/ui/backend/utils/apiCall";
import { useQuery } from "@tanstack/react-query";
import {
  createDictionaryMap,
  normalizeDictionaryEntries
} from "../dictionaryAppearance.js";
const DICTIONARY_ENTRIES_STALE_TIME = 5 * 60 * 1e3;
const BASE_DICTIONARY_ENTRIES_KEY = ["dictionaries", "entries"];
const dictionaryEntriesQueryKey = (dictionaryId, scopeVersion = 0) => [...BASE_DICTIONARY_ENTRIES_KEY, dictionaryId, `scope:${scopeVersion}`];
const dictionaryEntriesQueryOptions = (dictionaryId, scopeVersion = 0) => ({
  queryKey: dictionaryEntriesQueryKey(dictionaryId, scopeVersion),
  staleTime: DICTIONARY_ENTRIES_STALE_TIME,
  gcTime: DICTIONARY_ENTRIES_STALE_TIME,
  queryFn: async () => {
    const call = await apiCall(`/api/dictionaries/${dictionaryId}/entries`);
    const payload = call.result ?? {};
    if (!call.ok) {
      const message = typeof payload.error === "string" ? payload.error : "Failed to load dictionary entries.";
      throw new Error(message);
    }
    const items = Array.isArray(payload.items) ? payload.items : [];
    const parsed = [];
    for (const item of items) {
      if (!item || typeof item !== "object") continue;
      const data = item;
      const id = typeof data.id === "string" ? data.id : "";
      const value = typeof data.value === "string" ? data.value.trim() : "";
      if (!id || !value) continue;
      const label = typeof data.label === "string" && data.label.trim().length ? data.label.trim() : value;
      const color = typeof data.color === "string" && /^#([0-9a-fA-F]{6})$/.test(data.color) ? `#${data.color.slice(1).toLowerCase()}` : null;
      const icon = typeof data.icon === "string" && data.icon.trim().length ? data.icon.trim() : null;
      const createdAt = typeof data.createdAt === "string" ? data.createdAt : null;
      const updatedAt = typeof data.updatedAt === "string" ? data.updatedAt : null;
      parsed.push({
        id,
        value,
        label,
        color,
        icon,
        createdAt,
        updatedAt
      });
    }
    const normalized = normalizeDictionaryEntries(
      parsed.map(({ value, label, color, icon }) => ({ value, label, color, icon })),
      { sort: false }
    );
    return {
      entries: normalized,
      map: createDictionaryMap(normalized),
      fullEntries: parsed
    };
  }
});
function useDictionaryEntries(dictionaryId, scopeVersion = 0) {
  return useQuery(dictionaryEntriesQueryOptions(dictionaryId, scopeVersion));
}
async function invalidateDictionaryEntries(queryClient, dictionaryId) {
  await queryClient.invalidateQueries({ queryKey: [...BASE_DICTIONARY_ENTRIES_KEY, dictionaryId] });
}
async function ensureDictionaryEntries(queryClient, dictionaryId, scopeVersion = 0) {
  return queryClient.ensureQueryData(dictionaryEntriesQueryOptions(dictionaryId, scopeVersion));
}
export {
  dictionaryEntriesQueryKey,
  dictionaryEntriesQueryOptions,
  ensureDictionaryEntries,
  invalidateDictionaryEntries,
  useDictionaryEntries
};
//# sourceMappingURL=useDictionaryEntries.js.map
