"use client";
import { apiCall, apiCallOrThrow } from "@open-mercato/ui/backend/utils/apiCall";
const RESOURCE_DICTIONARY_KEYS = {
  activityTypes: "resources.activity-types"
};
async function ensureDictionary(key, name) {
  const listCall = await apiCall("/api/dictionaries");
  const items = Array.isArray(listCall.result?.items) ? listCall.result?.items ?? [] : [];
  const existing = items.find((item) => item && item.key === key);
  if (existing) return existing;
  if (!listCall.ok) return null;
  const created = await apiCallOrThrow(
    "/api/dictionaries",
    {
      method: "POST",
      headers: { "content-type": "application/json" },
      body: JSON.stringify({ key, name })
    }
  );
  const result = created.result;
  if (result && result.id && result.key) return result;
  return null;
}
async function loadResourceDictionaryEntries(kind) {
  const { entries } = await loadResourceDictionary(kind);
  return entries;
}
async function loadResourceDictionary(kind) {
  const key = RESOURCE_DICTIONARY_KEYS[kind];
  const name = "Resource activity types";
  const dictionary = await ensureDictionary(key, name);
  if (!dictionary) return { dictionary: null, entries: [] };
  const entriesCall = await apiCall(`/api/dictionaries/${dictionary.id}/entries`);
  if (!entriesCall.ok) return { dictionary, entries: [] };
  const items = Array.isArray(entriesCall.result?.items) ? entriesCall.result?.items ?? [] : [];
  const entries = items.map((entry) => {
    if (!entry || typeof entry !== "object") return null;
    const record = entry;
    const value = typeof record.value === "string" ? record.value : null;
    if (!value) return null;
    const label = typeof record.label === "string" && record.label.trim().length ? record.label : value;
    const color = typeof record.color === "string" ? record.color : null;
    const icon = typeof record.icon === "string" ? record.icon : null;
    return { value, label, color, icon };
  }).filter((entry) => !!entry);
  return { dictionary, entries };
}
async function createResourceDictionaryEntry(kind, input) {
  const key = RESOURCE_DICTIONARY_KEYS[kind];
  const name = "Resource activity types";
  const dictionary = await ensureDictionary(key, name);
  if (!dictionary) return null;
  const response = await apiCallOrThrow(
    `/api/dictionaries/${dictionary.id}/entries`,
    {
      method: "POST",
      headers: { "content-type": "application/json" },
      body: JSON.stringify({
        value: input.value,
        label: input.label ?? void 0,
        color: input.color ?? void 0,
        icon: input.icon ?? void 0
      })
    }
  );
  const payload = response.result ?? {};
  const value = typeof payload.value === "string" && payload.value.trim().length ? payload.value : input.value;
  const label = typeof payload.label === "string" && payload.label.trim().length ? payload.label : value;
  const color = typeof payload.color === "string" ? payload.color : input.color ?? null;
  const icon = typeof payload.icon === "string" ? payload.icon : input.icon ?? null;
  return { value, label, color, icon };
}
export {
  createResourceDictionaryEntry,
  loadResourceDictionary,
  loadResourceDictionaryEntries
};
//# sourceMappingURL=dictionaries.js.map
