import { WorkflowCategoryOverlaySchema } from '../../../core/models/index.js';
function isRecord(value) {
    return !!value && typeof value === 'object' && !Array.isArray(value);
}
function parseStringNameList(raw, sourceLabel, path) {
    if (raw === undefined) {
        return [];
    }
    if (!Array.isArray(raw)) {
        throw new Error(`workflows must be an array in ${sourceLabel} at ${path.join(' > ')}`);
    }
    const names = [];
    for (const item of raw) {
        if (typeof item !== 'string' || item.trim().length === 0) {
            throw new Error(`name must be a non-empty string in ${sourceLabel} at ${path.join(' > ')}`);
        }
        names.push(item);
    }
    return names;
}
function parseWorkflows(raw, sourceLabel, path) {
    return Object.prototype.hasOwnProperty.call(raw, 'workflows')
        ? parseStringNameList(raw.workflows, sourceLabel, path)
        : [];
}
function parseCategoryNode(name, raw, sourceLabel, path) {
    if (!isRecord(raw)) {
        throw new Error(`category "${name}" must be an object in ${sourceLabel} at ${path.join(' > ')}`);
    }
    const workflows = parseWorkflows(raw, sourceLabel, path);
    const children = [];
    for (const [key, value] of Object.entries(raw)) {
        if (key === 'workflows') {
            continue;
        }
        if (!isRecord(value)) {
            throw new Error(`category "${key}" must be an object in ${sourceLabel} at ${[...path, key].join(' > ')}`);
        }
        children.push(parseCategoryNode(key, value, sourceLabel, [...path, key]));
    }
    return { name, workflows, children };
}
function parseCategoryTree(raw, sourceLabel, rootKeyLabel) {
    if (!isRecord(raw)) {
        throw new Error(`${rootKeyLabel} must be an object in ${sourceLabel}`);
    }
    return Object.entries(raw).map(([name, value]) => parseCategoryNode(name, value, sourceLabel, [name]));
}
function parseCategoryConfig(raw, sourceLabel) {
    if (!raw || typeof raw !== 'object') {
        return null;
    }
    const parsed = WorkflowCategoryOverlaySchema.parse(raw);
    const result = {};
    if (Object.prototype.hasOwnProperty.call(parsed, 'workflow_categories')) {
        if (!parsed.workflow_categories) {
            throw new Error(`workflow_categories must be an object in ${sourceLabel}`);
        }
        result.workflowCategories = parseCategoryTree(parsed.workflow_categories, sourceLabel, 'workflow_categories');
    }
    if (parsed.show_others_category !== undefined) {
        result.showOthersCategory = parsed.show_others_category;
    }
    if (parsed.others_category_name !== undefined) {
        result.othersCategoryName = parsed.others_category_name;
    }
    if (result.workflowCategories === undefined
        && result.showOthersCategory === undefined
        && result.othersCategoryName === undefined) {
        return null;
    }
    return result;
}
function convertParsedNodes(nodes) {
    return nodes.map((node) => ({
        name: node.name,
        workflows: node.workflows,
        children: convertParsedNodes(node.children),
    }));
}
export function parseWorkflowCategoryOverlay(raw, sourceLabel) {
    const parsed = parseCategoryConfig(raw, sourceLabel);
    if (!parsed) {
        return null;
    }
    return {
        workflowCategories: parsed.workflowCategories
            ? convertParsedNodes(parsed.workflowCategories)
            : undefined,
        showOthersCategory: parsed.showOthersCategory,
        othersCategoryName: parsed.othersCategoryName,
    };
}
export function parseWorkflowCategoryConfig(raw, sourceLabel) {
    const parsed = parseWorkflowCategoryOverlay(raw, sourceLabel);
    if (!parsed?.workflowCategories) {
        return null;
    }
    return {
        workflowCategories: parsed.workflowCategories,
        builtinWorkflowCategories: parsed.workflowCategories,
        userWorkflowCategories: [],
        hasUserCategories: false,
        showOthersCategory: parsed.showOthersCategory ?? true,
        othersCategoryName: parsed.othersCategoryName ?? 'Others',
    };
}
export function mergeWorkflowCategoryConfigs(builtinConfig, userConfig, builtinCategoryName) {
    const userWorkflowCategories = userConfig?.workflowCategories ?? [];
    const builtinWorkflowCategories = builtinConfig.workflowCategories;
    const hasUserCategories = userWorkflowCategories.length > 0;
    const workflowCategories = hasUserCategories
        ? [
            ...userWorkflowCategories,
            {
                name: builtinCategoryName,
                workflows: [],
                children: builtinWorkflowCategories,
            },
        ]
        : builtinWorkflowCategories;
    return {
        workflowCategories,
        builtinWorkflowCategories,
        userWorkflowCategories,
        hasUserCategories,
        showOthersCategory: userConfig?.showOthersCategory ?? builtinConfig.showOthersCategory,
        othersCategoryName: userConfig?.othersCategoryName ?? builtinConfig.othersCategoryName,
    };
}
//# sourceMappingURL=workflowCategoryParser.js.map