import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http' import type { CustomerDimensionConfig, CustomerDimensionCalculationMode, CustomerDimensionRule, UpdateCustomerDimensionConfigRequest, } from './types' const dimensionConfigPath = '/platform/v1/first-leg/customer-dimension-config' type RawRule = Omit & { id: string | number version: string | number } type RawConfig = Omit & { tenantId: string | number rules: RawRule[] } function normalizeRule(rule: RawRule): CustomerDimensionRule { return { ...rule, id: String(rule.id), version: Number(rule.version) } } function normalizeConfig(config: RawConfig): CustomerDimensionConfig { return { tenantId: String(config.tenantId), defaultCalculationMode: config.defaultCalculationMode === 'MULTIPLY' ? 'MULTIPLY' : config.defaultCalculationMode === 'ACTUAL' ? 'ACTUAL' : 'ADD', rules: config.rules.map(normalizeRule), } } export async function getCustomerDimensionConfig(tenantId: string): Promise { const response = await http.get>(dimensionConfigPath, { params: { tenantId }, }) return normalizeConfig(unwrapApiResponse(response)) } export async function updateCustomerDimensionConfig( tenantId: string, request: UpdateCustomerDimensionConfigRequest, ): Promise { const response = await http.put>(dimensionConfigPath, request, { params: { tenantId }, }) return normalizeConfig(unwrapApiResponse(response)) } export async function updateCustomerDimensionDefaultCalculationMode( tenantId: string, defaultCalculationMode: CustomerDimensionCalculationMode, ): Promise { const response = await http.put>( `${dimensionConfigPath}/default-calculation-mode`, { defaultCalculationMode }, { params: { tenantId } }, ) return normalizeConfig(unwrapApiResponse(response)) }