"use client";
import { apiCall, apiCallOrThrow } from "@open-mercato/ui/backend/utils/apiCall";
import {
  resolveUnavailabilityReasonDictionary
} from "@open-mercato/core/modules/planner/lib/unavailabilityReasons";
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 loadUnavailabilityReasonEntries(subjectType) {
  const dictionary = resolveUnavailabilityReasonDictionary(subjectType);
  const resolved = await ensureDictionary(dictionary.key, dictionary.name);
  if (!resolved) return [];
  const entriesCall = await apiCall(`/api/dictionaries/${resolved.id}/entries`);
  if (!entriesCall.ok) return [];
  const items = Array.isArray(entriesCall.result?.items) ? entriesCall.result?.items ?? [] : [];
  return items.map((entry) => {
    if (!entry || typeof entry !== "object") return null;
    const record = entry;
    const id = typeof record.id === "string" ? record.id : null;
    const value = typeof record.value === "string" ? record.value : null;
    if (!id || !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 { id, value, label, color, icon };
  }).filter((entry) => !!entry);
}
async function createUnavailabilityReasonEntry(subjectType, input) {
  const dictionary = resolveUnavailabilityReasonDictionary(subjectType);
  const resolved = await ensureDictionary(dictionary.key, dictionary.name);
  if (!resolved) return null;
  const response = await apiCallOrThrow(
    `/api/dictionaries/${resolved.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 id = typeof payload.id === "string" ? payload.id : null;
  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;
  if (!id) return null;
  return { id, value, label, color, icon };
}
export {
  createUnavailabilityReasonEntry,
  loadUnavailabilityReasonEntries
};
//# sourceMappingURL=unavailabilityReasons.js.map
