import { apiRequest } from "./client";

export type LabelDictionaryItem = {
  id: number;
  code: string;
  name: string;
  alias?: string;
  sortOrder?: number;
};

export type LabelDictionaryCategory = {
  id: number;
  code: string;
  name: string;
  sortOrder?: number;
  items: LabelDictionaryItem[];
};

export type LabelDictionaryResponse = {
  lib: {
    id: number;
    name: string;
    version: string;
    status: string;
    isDefault: boolean;
  };
  categories: LabelDictionaryCategory[];
};

export function getLabelDictionary(version?: string) {
  const qs = version ? `?version=${encodeURIComponent(version)}` : "";
  return apiRequest(`/api/labels/dictionary${qs}`) as Promise<LabelDictionaryResponse>;
}

export function saveMyLabels(labelItemIds: number[], customLabels: string[] = [], scope = "all") {
  return apiRequest("/api/labels/me", {
    method: "PUT",
    data: { labelItemIds, customLabels, scope },
  });
}

export function getMyLabels() {
  return apiRequest("/api/labels/me") as Promise<any>;
}
