"use strict";
// SANDCODE SDK entry — wire-protocol client for Sandcode-compatible endpoints.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.UnprocessableEntityError = exports.PermissionDeniedError = exports.InternalServerError = exports.AuthenticationError = exports.BadRequestError = exports.RateLimitError = exports.ConflictError = exports.NotFoundError = exports.APIUserAbortError = exports.APIConnectionTimeoutError = exports.APIConnectionError = exports.APIError = exports.SandcodeError = exports.fileFromPath = exports.toFile = exports.AI_PROMPT = exports.HUMAN_PROMPT = exports.Sandcode = void 0;
const Core = __importStar(require("./core.js"));
const Errors = __importStar(require("./error.js"));
const Pagination = __importStar(require("./pagination.js"));
const Uploads = __importStar(require("./uploads.js"));
const API = __importStar(require("./resources/index.js"));
const completions_1 = require("./resources/completions.js");
const models_1 = require("./resources/models.js");
const beta_1 = require("./resources/beta/beta.js");
const messages_1 = require("./resources/messages/messages.js");
/**
 * API Client for the SANDCODE SDK.
 */
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}` };
    }
}
exports.Sandcode = Sandcode;
_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_1.Completions;
Sandcode.Messages = messages_1.Messages;
Sandcode.Models = models_1.Models;
Sandcode.ModelInfosPage = models_1.ModelInfosPage;
Sandcode.Beta = beta_1.Beta;
exports.HUMAN_PROMPT = Sandcode.HUMAN_PROMPT, exports.AI_PROMPT = Sandcode.AI_PROMPT;
var uploads_1 = require("./uploads.js");
Object.defineProperty(exports, "toFile", { enumerable: true, get: function () { return uploads_1.toFile; } });
Object.defineProperty(exports, "fileFromPath", { enumerable: true, get: function () { return uploads_1.fileFromPath; } });
var error_1 = require("./error.js");
Object.defineProperty(exports, "SandcodeError", { enumerable: true, get: function () { return error_1.SandcodeError; } });
Object.defineProperty(exports, "APIError", { enumerable: true, get: function () { return error_1.APIError; } });
Object.defineProperty(exports, "APIConnectionError", { enumerable: true, get: function () { return error_1.APIConnectionError; } });
Object.defineProperty(exports, "APIConnectionTimeoutError", { enumerable: true, get: function () { return error_1.APIConnectionTimeoutError; } });
Object.defineProperty(exports, "APIUserAbortError", { enumerable: true, get: function () { return error_1.APIUserAbortError; } });
Object.defineProperty(exports, "NotFoundError", { enumerable: true, get: function () { return error_1.NotFoundError; } });
Object.defineProperty(exports, "ConflictError", { enumerable: true, get: function () { return error_1.ConflictError; } });
Object.defineProperty(exports, "RateLimitError", { enumerable: true, get: function () { return error_1.RateLimitError; } });
Object.defineProperty(exports, "BadRequestError", { enumerable: true, get: function () { return error_1.BadRequestError; } });
Object.defineProperty(exports, "AuthenticationError", { enumerable: true, get: function () { return error_1.AuthenticationError; } });
Object.defineProperty(exports, "InternalServerError", { enumerable: true, get: function () { return error_1.InternalServerError; } });
Object.defineProperty(exports, "PermissionDeniedError", { enumerable: true, get: function () { return error_1.PermissionDeniedError; } });
Object.defineProperty(exports, "UnprocessableEntityError", { enumerable: true, get: function () { return error_1.UnprocessableEntityError; } });
exports = module.exports = Sandcode;
exports.default = Sandcode;
//# sourceMappingURL=index.js.map