import omit from 'lodash/omit.js';
import { parsePropertyRef } from "../refs.js";
export const TOOL_SCHEMA_PATH = ['tools'];
export function getToolMap(projectSchema) {
    return projectSchema.tools;
}
export function getTools(projectSchema) {
    const { tools } = projectSchema;
    return tools ? Object.keys(tools).map((name) => ({ name, ...tools[name] })) : [];
}
export function getToolNames(projectSchema) {
    return projectSchema.tools ? Object.keys(projectSchema.tools) : [];
}
export function getTool(layer, toolRef) {
    const parsed = parsePropertyRef(toolRef, 'Tool');
    if (!parsed) {
        throw new Error(`Invalid tool ref "${toolRef}"`);
    }
    const toolName = parsed.propertyName;
    const tool = layer.tools?.[toolName];
    return tool ? { ...tool, name: toolName } : undefined;
}
export function setTools(layer, tools) {
    return {
        ...layer,
        tools: tools.reduce((acc, { name, ...tool }) => {
            acc[name] = tool;
            return acc;
        }, {})
    };
}
export function setTool(layer, namedTool) {
    const { name, ...tool } = namedTool;
    return { ...layer, tools: { ...(layer.tools ?? {}), [name]: tool } };
}
export function removeTools(layer, toolNames) {
    const newTools = omit(layer.tools ?? {}, toolNames);
    return { ...layer, tools: newTools };
}
