import type { CreateEventCategoryDto, EventCategoryResponseDto, EventCategorySummaryResponseDto } from '@/api/generated/models' import { createServiceError, errorResult, handleServiceError, type ServiceCallOptions, type ServiceResult, shouldBubbleErrorToParent, successResult, } from '@/services/errorHandler' import { unwrapApiData } from '@/services/apiResponse' import { getAdminEventCategories } from '@/api/generated/admin-event-categories/admin-event-categories' import { texts } from '@/texts' export type EventCategory = EventCategoryResponseDto export type EventCategorySummary = EventCategorySummaryResponseDto export type EventCategoryPayload = CreateEventCategoryDto const adminEventCategoriesApi = getAdminEventCategories() /** Flat admin category list for filters/selects (id + name). */ export const LIST_ADMIN_CATEGORIES_FLAT = async ( options?: ServiceCallOptions ): Promise> => { try { const res = await adminEventCategoriesApi.adminEventCategoriesControllerListFlat() const payload = res.data if (!payload.success) { throw createServiceError(payload.message || texts.events.categoriesListFailed) } const items = unwrapApiData(payload) return successResult({ items: items ?? [] }) } catch (error) { const normalizedError = handleServiceError(error, options) if (shouldBubbleErrorToParent(options)) { throw normalizedError } return errorResult(normalizedError) } } /** * One "level" of the category tree: every direct child of `parentId` * (or every root category, when `parentId` is null). */ export const GET_CATEGORY_CHILDREN = async ( parentId: number | null, options?: ServiceCallOptions ): Promise> => { try { const res = await adminEventCategoriesApi.adminEventCategoriesControllerList({ pageSize: 100, filters: { parentId: parentId === null ? 'null' : String(parentId) }, }) const payload = res.data if (!payload.success) { throw createServiceError(payload.message || texts.events.categoriesListFailed) } const list = unwrapApiData(payload) return successResult(list?.items ?? []) } catch (error) { const normalizedError = handleServiceError(error, options) if (shouldBubbleErrorToParent(options)) { throw normalizedError } return errorResult(normalizedError) } } export const CREATE_CATEGORY = async ( payload: EventCategoryPayload, options?: ServiceCallOptions ): Promise> => { try { const res = await adminEventCategoriesApi.adminEventCategoriesControllerCreate(payload) const body = res.data if (!body.success) { throw createServiceError(body.message || texts.events.categoryCreateFailed) } const category = unwrapApiData(body) if (!category?.id) { throw createServiceError(texts.common.invalidServerResponse) } return successResult(category) } catch (error) { const normalizedError = handleServiceError(error, options) if (shouldBubbleErrorToParent(options)) { throw normalizedError } return errorResult(normalizedError) } } export const UPDATE_CATEGORY = async ( id: number, payload: Omit, options?: ServiceCallOptions ): Promise> => { try { const res = await adminEventCategoriesApi.adminEventCategoriesControllerUpdate(id, payload) const body = res.data if (!body.success) { throw createServiceError(body.message || texts.events.categoryUpdateFailed) } const category = unwrapApiData(body) if (!category?.id) { throw createServiceError(texts.common.invalidServerResponse) } return successResult(category) } catch (error) { const normalizedError = handleServiceError(error, options) if (shouldBubbleErrorToParent(options)) { throw normalizedError } return errorResult(normalizedError) } } export const DELETE_CATEGORY = async (id: number, options?: ServiceCallOptions): Promise> => { try { const res = await adminEventCategoriesApi.adminEventCategoriesControllerRemove(id) const body = res.data if (!body.success) { throw createServiceError(body.message || texts.events.categoryDeleteFailed) } return successResult(unwrapApiData(body) ?? { success: true }) } catch (error) { const normalizedError = handleServiceError(error, options) if (shouldBubbleErrorToParent(options)) { throw normalizedError } return errorResult(normalizedError) } }