import { getLogger } from "../utils/logger.js";
import { BaseApiClient, BaseApiError } from "./base-client.js";

//#region src/api-client/manage-api-client.ts
const logger = getLogger("manage-api-client");
var ManageApiError = class extends BaseApiError {
	constructor(message, statusCode, responseBody) {
		super(message, statusCode, responseBody);
		this.name = "ManageApiError";
	}
};
var ManagementApiClient = class extends BaseApiClient {
	constructor(config) {
		super(config);
	}
	/**
	* Override to return ManageApiError
	*/
	createError(message, statusCode, responseBody) {
		return new ManageApiError(message, statusCode, responseBody);
	}
	/**
	* Override to extract data from { data: ... } wrapper
	*/
	async extractResponseData(response) {
		return (await response.json()).data;
	}
	async getFullProject() {
		const path = `/tenants/${this.checkTenantId()}/project-full/${this.projectId}/with-relation-ids`;
		return this.makeGetRequest(path, "Failed to fetch project config");
	}
	async getResolvedRef() {
		const tenantId = this.checkTenantId();
		logger.info({
			tenantId,
			projectId: this.projectId
		}, "Resolving ref");
		const path = `/tenants/${tenantId}/projects/${this.projectId}/refs/resolve`;
		return this.makeGetRequest(path, "Failed to resolve ref");
	}
	async getFullAgent(agentId) {
		const path = `/tenants/${this.checkTenantId()}/projects/${this.projectId}/agent/${agentId}`;
		try {
			return await this.makeGetRequest(path, "Failed to fetch full agent");
		} catch (error) {
			if (error instanceof ManageApiError && error.isNotFound) return null;
			throw error;
		}
	}
	async getMcpTool(toolId) {
		const path = `/tenants/${this.checkTenantId()}/projects/${this.projectId}/tools/${toolId}`;
		return this.makeGetRequest(path, "Failed to fetch MCP tool");
	}
	async getFunctionToolsForSubAgent(agentId, subAgentId) {
		const path = `/tenants/${this.checkTenantId()}/projects/${this.projectId}/agents/${agentId}/sub-agent-function-tools/sub-agent/${subAgentId}`;
		return this.makePaginatedGetRequest(path, "Failed to fetch function tools for sub-agent");
	}
	async getEvaluationJobConfigById(evaluationJobConfigId) {
		const path = `/tenants/${this.checkTenantId()}/projects/${this.projectId}/evals/evaluation-job-configs/${evaluationJobConfigId}`;
		try {
			return await this.makeGetRequest(path, "Failed to fetch evaluation job config");
		} catch (error) {
			if (error instanceof ManageApiError && error.isNotFound) return null;
			throw error;
		}
	}
	async getEvaluationJobConfigEvaluatorRelations(evaluationJobConfigId) {
		const path = `/tenants/${this.checkTenantId()}/projects/${this.projectId}/evals/evaluation-job-configs/${evaluationJobConfigId}/evaluators`;
		return this.makeGetRequest(path, "Failed to fetch evaluation job config evaluator relations");
	}
	async getEvaluatorById(evaluatorId) {
		const path = `/tenants/${this.checkTenantId()}/projects/${this.projectId}/evals/evaluators/${evaluatorId}`;
		try {
			return await this.makeGetRequest(path, "Failed to fetch evaluator");
		} catch (error) {
			if (error instanceof ManageApiError && error.isNotFound) return null;
			throw error;
		}
	}
	async getEvaluatorsByIds(evaluatorIds) {
		if (evaluatorIds.length === 0) return [];
		const path = `/tenants/${this.checkTenantId()}/projects/${this.projectId}/evals/evaluators/batch`;
		return this.makePostRequest(path, { evaluatorIds }, "Failed to fetch evaluators batch");
	}
	async listEvaluationRunConfigs() {
		const path = `/tenants/${this.checkTenantId()}/projects/${this.projectId}/evals/evaluation-run-configs`;
		return this.makePaginatedGetRequest(path, "Failed to list evaluation run configs");
	}
	async getEvaluationRunConfigById(evaluationRunConfigId) {
		const path = `/tenants/${this.checkTenantId()}/projects/${this.projectId}/evals/evaluation-run-configs/${evaluationRunConfigId}`;
		return this.makeGetRequest(path, "Failed to fetch evaluation run config");
	}
	async getEvaluationSuiteConfigById(evaluationSuiteConfigId) {
		const path = `/tenants/${this.checkTenantId()}/projects/${this.projectId}/evals/evaluation-suite-configs/${evaluationSuiteConfigId}`;
		return this.makeGetRequest(path, "Failed to fetch evaluation suite config");
	}
	async getEvaluationSuiteConfigEvaluatorRelations(evaluationSuiteConfigId) {
		const path = `/tenants/${this.checkTenantId()}/projects/${this.projectId}/evals/evaluation-suite-configs/${evaluationSuiteConfigId}/evaluators`;
		return this.makeGetRequest(path, "Failed to fetch evaluation suite config evaluator relations");
	}
};

//#endregion
export { ManageApiError, ManagementApiClient };