// SANDCODE SDK entry — wire-protocol client for Sandcode-compatible endpoints.
var _a;
import * as Core from "./core.mjs";
import * as Errors from "./error.mjs";
import * as Pagination from "./pagination.mjs";
import * as Uploads from "./uploads.mjs";
import * as API from "./resources/index.mjs";
import { Completions, } from "./resources/completions.mjs";
import { ModelInfosPage, Models } from "./resources/models.mjs";
import { Beta, } from "./resources/beta/beta.mjs";
import { Messages, } from "./resources/messages/messages.mjs";
/**
 * API Client for the SANDCODE SDK.
 */
export class Sandcode extends Core.APIClient {
    /**
     * API Client for the SANDCODE SDK.
     *
     * @param {string | null | undefined} [opts.apiKey=process.env['SANDCODE_API_KEY'] ?? null]
     * @param {string | null | undefined} [opts.authToken=process.env['SANDCODE_AUTH_TOKEN'] ?? null]
     * @param {string} [opts.baseURL=process.env['SANDCODE_BASE_URL']] - Base URL for the API. Required — no default.
     * @param {number} [opts.timeout=10 minutes] - Maximum time (ms) to wait for a response before timing out.
     * @param {number} [opts.httpAgent] - HTTP agent for managing connections.
     * @param {Core.Fetch} [opts.fetch] - Custom `fetch` implementation.
     * @param {number} [opts.maxRetries=2] - Maximum retry attempts.
     * @param {Core.Headers} opts.defaultHeaders - Default request headers.
     * @param {Core.DefaultQuery} opts.defaultQuery - Default query parameters.
     * @param {boolean} [opts.dangerouslyAllowBrowser=false] - Allow browser usage. Disabled by default to avoid exposing credentials.
     */
    constructor({ baseURL = Core.readEnv('SANDCODE_BASE_URL'), apiKey = Core.readEnv('SANDCODE_API_KEY') ?? null, authToken = Core.readEnv('SANDCODE_AUTH_TOKEN') ?? null, ...opts } = {}) {
        const options = {
            apiKey,
            authToken,
            ...opts,
            baseURL: baseURL || '',
        };
        if (!options.baseURL) {
            throw new Errors.SandcodeError('SANDCODE SDK: `baseURL` is required. Set SANDCODE_BASE_URL or pass `baseURL` explicitly.');
        }
        if (!options.dangerouslyAllowBrowser && Core.isRunningInBrowser()) {
            throw new Errors.SandcodeError("It looks like you're running in a browser-like environment.\n\nThis is disabled by default, as it risks exposing your secret API credentials to attackers.\nIf you understand the risks and have appropriate mitigations in place,\nyou can set the `dangerouslyAllowBrowser` option to `true`.\n");
        }
        super({
            baseURL: options.baseURL,
            timeout: options.timeout ?? 600000 /* 10 minutes */,
            httpAgent: options.httpAgent,
            maxRetries: options.maxRetries,
            fetch: options.fetch,
        });
        this.completions = new API.Completions(this);
        this.messages = new API.Messages(this);
        this.models = new API.Models(this);
        this.beta = new API.Beta(this);
        this._options = options;
        this.apiKey = apiKey;
        this.authToken = authToken;
    }
    defaultQuery() {
        return this._options.defaultQuery;
    }
    defaultHeaders(opts) {
        return {
            ...super.defaultHeaders(opts),
            ...(this._options.dangerouslyAllowBrowser ?
                { 'anthropic-dangerous-direct-browser-access': 'true' }
                : undefined),
            'anthropic-version': '2023-06-01',
            ...this._options.defaultHeaders,
        };
    }
    validateHeaders(headers, customHeaders) {
        if (this.apiKey && headers['x-api-key']) {
            return;
        }
        if (customHeaders['x-api-key'] === null) {
            return;
        }
        if (this.authToken && headers['authorization']) {
            return;
        }
        if (customHeaders['authorization'] === null) {
            return;
        }
        throw new Error('Could not resolve authentication method. Expected either apiKey or authToken to be set. Or for one of the "X-Api-Key" or "Authorization" headers to be explicitly omitted');
    }
    authHeaders(opts) {
        const apiKeyAuth = this.apiKeyAuth(opts);
        const bearerAuth = this.bearerAuth(opts);
        if (apiKeyAuth != null && !Core.isEmptyObj(apiKeyAuth)) {
            return apiKeyAuth;
        }
        if (bearerAuth != null && !Core.isEmptyObj(bearerAuth)) {
            return bearerAuth;
        }
        return {};
    }
    apiKeyAuth(opts) {
        if (this.apiKey == null) {
            return {};
        }
        return { 'X-Api-Key': this.apiKey };
    }
    bearerAuth(opts) {
        if (this.authToken == null) {
            return {};
        }
        return { Authorization: `Bearer ${this.authToken}` };
    }
}
_a = Sandcode;
Sandcode.Sandcode = _a;
Sandcode.HUMAN_PROMPT = '\n\nHuman:';
Sandcode.AI_PROMPT = '\n\nAssistant:';
Sandcode.DEFAULT_TIMEOUT = 600000; // 10 minutes
Sandcode.SandcodeError = Errors.SandcodeError;
Sandcode.APIError = Errors.APIError;
Sandcode.APIConnectionError = Errors.APIConnectionError;
Sandcode.APIConnectionTimeoutError = Errors.APIConnectionTimeoutError;
Sandcode.APIUserAbortError = Errors.APIUserAbortError;
Sandcode.NotFoundError = Errors.NotFoundError;
Sandcode.ConflictError = Errors.ConflictError;
Sandcode.RateLimitError = Errors.RateLimitError;
Sandcode.BadRequestError = Errors.BadRequestError;
Sandcode.AuthenticationError = Errors.AuthenticationError;
Sandcode.InternalServerError = Errors.InternalServerError;
Sandcode.PermissionDeniedError = Errors.PermissionDeniedError;
Sandcode.UnprocessableEntityError = Errors.UnprocessableEntityError;
Sandcode.toFile = Uploads.toFile;
Sandcode.fileFromPath = Uploads.fileFromPath;
Sandcode.Completions = Completions;
Sandcode.Messages = Messages;
Sandcode.Models = Models;
Sandcode.ModelInfosPage = ModelInfosPage;
Sandcode.Beta = Beta;
export const { HUMAN_PROMPT, AI_PROMPT } = Sandcode;
export { toFile, fileFromPath } from "./uploads.mjs";
export { SandcodeError, APIError, APIConnectionError, APIConnectionTimeoutError, APIUserAbortError, NotFoundError, ConflictError, RateLimitError, BadRequestError, AuthenticationError, InternalServerError, PermissionDeniedError, UnprocessableEntityError, } from "./error.mjs";
export default Sandcode;
//# sourceMappingURL=index.mjs.map