/**
 * Abstract base class for tools providing common functionality.
 */
export class Tool {
    isReadOnly = false;
    isDestructive = false;
    isConcurrencySafe = false;
    searchHint;
    aliases;
    async checkPermissions(bash, _args) {
        // Default: allow but respect bash mode
        if (this.isDestructive && bash.getMode() === "plan") {
            return {
                behavior: "deny",
                message: `Cannot execute destructive tool '${this.name}' in plan mode.`,
            };
        }
        return { behavior: "allow" };
    }
    async validateInput(args) {
        const result = this.parameters.safeParse(args);
        if (!result.success) {
            return {
                result: false,
                message: `Invalid parameters for '${this.name}': ${result.error.message}`,
            };
        }
        return { result: true };
    }
    /**
     * Helper to truncate long results for token efficiency.
     */
    truncateResult(result, maxLength = 50000) {
        if (result.length > maxLength) {
            return `${result.substring(0, maxLength)}\n... [Result truncated]`;
        }
        return result;
    }
}
/**
 * Factory function to build a tool from a plain object.
 * Breaks circular dependencies by living in the base Tool file.
 */
export function buildTool(tool) {
    const isDestructive = typeof tool.isDestructive === "function"
        ? tool.isDestructive
        : () => !!tool.isDestructive;
    return {
        isReadOnly: false,
        isDestructive: false,
        isConcurrencySafe: false,
        ...tool,
        checkPermissions: tool.checkPermissions ||
            (async (bash, args) => {
                if (isDestructive(args) && bash.getMode() === "plan") {
                    return {
                        behavior: "deny",
                        message: `Cannot execute destructive tool '${tool.name}' in plan mode.`,
                    };
                }
                return { behavior: "allow" };
            }),
        validateInput: tool.validateInput ||
            (async (args) => {
                const result = tool.parameters.safeParse(args);
                if (!result.success) {
                    return {
                        result: false,
                        message: `Invalid parameters for '${tool.name}': ${result.error.message}`,
                    };
                }
                return { result: true };
            }),
    };
}
