const getSafeCount = (value) => {
    const count = Number(value !== null && value !== void 0 ? value : 0);
    return Number.isFinite(count) && count > 0 ? Math.floor(count) : 0;
};
const getCountFromMap = (code, countByCode, fallbackCount, useMockFallbackWhenMissing) => {
    const fromApi = getSafeCount(countByCode[code]);
    if (fromApi > 0)
        return fromApi;
    return useMockFallbackWhenMissing ? getSafeCount(fallbackCount) : 0;
};
const findParentCodeByLevel = (currentCode, currentLevel, parentLevelMap) => {
    const parentCodes = parentLevelMap[currentLevel - 1] || [];
    if (parentCodes.length === 0)
        return null;
    const matchedParent = parentCodes
        .filter((parentCode) => {
        if (currentCode === parentCode)
            return false;
        return (currentCode.startsWith(`${parentCode}_`) ||
            currentCode.startsWith(`${parentCode}-`) ||
            currentCode.startsWith(`${parentCode}.`) ||
            currentCode.startsWith(parentCode));
    })
        .sort((a, b) => b.length - a.length)[0];
    return matchedParent || null;
};
export const parseCodeLevelCsv = (raw) => {
    if (!raw)
        return [];
    return raw
        .split('\n')
        .map((line) => line.trim())
        .filter(Boolean)
        .map((line) => {
        const [code, levelRaw] = line.split(',');
        return {
            code: (code || '').trim(),
            level: Number((levelRaw || '').trim()),
        };
    })
        .filter((item) => item.code && Number.isFinite(item.level));
};
export const aggregateCountByCodeLevel = (countByCode, codeLevels) => {
    if (codeLevels.length === 0)
        return { ...countByCode };
    const totalsByCode = {};
    const normalizedCodeLevels = codeLevels
        .filter((item) => item.code && Number.isFinite(item.level))
        .map((item) => ({ code: item.code, level: Number(item.level) }));
    normalizedCodeLevels.forEach((item) => {
        totalsByCode[item.code] = getSafeCount(countByCode[item.code]);
    });
    const parentLevelMap = normalizedCodeLevels.reduce((acc, item) => {
        if (!acc[item.level])
            acc[item.level] = [];
        acc[item.level].push(item.code);
        return acc;
    }, {});
    const sortedByDepth = [...normalizedCodeLevels].sort((a, b) => b.level - a.level);
    sortedByDepth.forEach((item) => {
        const parentCode = findParentCodeByLevel(item.code, item.level, parentLevelMap);
        if (!parentCode)
            return;
        totalsByCode[parentCode] = (totalsByCode[parentCode] || 0) + (totalsByCode[item.code] || 0);
    });
    return totalsByCode;
};
export const applyRequiredActionCount = (menu, countByCode, options) => {
    var _a;
    const countByCodeWithRollup = options.aggregateByCodeLevel && ((_a = options.codeLevels) === null || _a === void 0 ? void 0 : _a.length)
        ? aggregateCountByCodeLevel(countByCode, options.codeLevels)
        : countByCode;
    const walk = (node) => {
        const children = node.child || [];
        const nextChildren = children.map(walk);
        const childTotal = nextChildren.reduce((sum, item) => sum + item.total, 0);
        const hasChild = nextChildren.length > 0;
        const ownCount = getCountFromMap(node.code, countByCodeWithRollup, options.fallbackCount, options.useMockFallbackWhenMissing);
        const total = hasChild ? (options.aggregateToParent ? childTotal : ownCount) : ownCount;
        const shouldHideParentCount = options.displayMode === 'leaf-only' && hasChild;
        return {
            total,
            node: {
                ...node,
                child: nextChildren.map((item) => item.node),
                requiredActionCount: shouldHideParentCount ? 0 : total,
            },
        };
    };
    return menu.map((item) => walk(item).node);
};
//# sourceMappingURL=required-action.js.map