import { getMajorDictById, getMajorIdAliasMap, getServiceDictById } from '@/sql' import { toFiniteNumberOrNull } from '@/lib/number' import { useKvStore } from '@/pinia/kv' import { useZxFwPricingStore } from '@/pinia/zxFwPricing' const CONSULT_CATEGORY_FACTOR_KEY = 'xm-consult-category-factor-v1' const MAJOR_FACTOR_KEY = 'xm-major-factor-v1' type XmFactorRow = { id: string standardFactor: number | null budgetValue: number | null } type XmFactorState = { detailRows?: XmFactorRow[] } type FactorDictItem = { defCoe?: number | null } type FactorDict = Record const buildStandardFactorMap = (dict: FactorDict): Map => { const map = new Map() for (const [id, item] of Object.entries(dict)) { map.set(String(id), toFiniteNumberOrNull(item?.defCoe)) } return map } const resolveFactorValue = ( row: Partial | undefined, fallbackStandard: number | null ): number | null => { if (!row) return null const budgetValue = toFiniteNumberOrNull(row.budgetValue) if (budgetValue != null) return budgetValue const standardFactor = toFiniteNumberOrNull(row.standardFactor) if (standardFactor != null) return standardFactor return fallbackStandard } const getKvStoreSafely = () => { try { return useKvStore() } catch { return null } } const getZxFwPricingStoreSafely = () => { try { return useZxFwPricingStore() } catch { return null } } const loadFactorMap = async ( storageKey: string, dict: FactorDict, aliases?: Map ): Promise> => { const zxFwPricingStore = getZxFwPricingStoreSafely() const kvStore = getKvStoreSafely() const piniaData = zxFwPricingStore ? await zxFwPricingStore.loadKeyState(storageKey) : null const data = piniaData ?? (kvStore ? await kvStore.getItem(storageKey) : null) const map = buildStandardFactorMap(dict) for (const row of data?.detailRows || []) { if (!row?.id) continue const rowId = String(row.id) const id = map.has(rowId) ? rowId : aliases?.get(rowId) || rowId map.set(id, resolveFactorValue(row, map.get(id) ?? null)) } return map } export const loadConsultCategoryFactorMap = async (storageKey = CONSULT_CATEGORY_FACTOR_KEY) => loadFactorMap(storageKey, getServiceDictById() as FactorDict) export const loadMajorFactorMap = async (storageKey = MAJOR_FACTOR_KEY) => loadFactorMap(storageKey, getMajorDictById() as FactorDict, getMajorIdAliasMap())