import { defineAiTool } from "./ai-tool-definition.js";
import {
  createAiApiOperationRunner
} from "./ai-api-operation-runner.js";
function defineApiBackedAiTool(config) {
  const {
    name,
    displayName,
    description,
    inputSchema,
    requiredFeatures,
    isMutation,
    toOperation,
    mapResponse,
    loadBeforeRecord,
    loadBeforeRecords
  } = config;
  let definition;
  const handler = async (input, context) => {
    const toolCtx = {
      ...context,
      tool: definition
    };
    const operation = await toOperation(input, toolCtx);
    const runner = createAiApiOperationRunner(toolCtx);
    const response = await runner.run(operation);
    if (!response.success) {
      throw new Error(response.error ?? `API operation failed for tool "${name}"`);
    }
    return await mapResponse(response, input, toolCtx);
  };
  definition = defineAiTool({
    name,
    description,
    inputSchema,
    requiredFeatures,
    handler,
    ...displayName !== void 0 ? { displayName } : {},
    ...isMutation !== void 0 ? { isMutation } : {},
    ...loadBeforeRecord !== void 0 ? { loadBeforeRecord } : {},
    ...loadBeforeRecords !== void 0 ? { loadBeforeRecords } : {}
  });
  return definition;
}
export {
  defineApiBackedAiTool
};
//# sourceMappingURL=api-backed-tool.js.map
