"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Client = void 0;
const tslib_1 = require("tslib");
const grpc = tslib_1.__importStar(require("@grpc/grpc-js"));
const axios_1 = tslib_1.__importDefault(require("axios"));
const index_1 = require("./index");
// export const DEFAULT_REST_ENDPOINT = `http://0.0.0.0:3000/v1`;
// export const DEFAULT_GRPC_ENDPOINT = `0.0.0.0:3001`;
const DEFAULT_REST_ENDPOINT = `https://api.qpace.dev/v1`;
const DEFAULT_GRPC_ENDPOINT = `grpc.qpace.dev`;
class Client {
    constructor(config) {
        this.config = config;
        this._clientInfo = {};
        this.config.apiBase ??= DEFAULT_REST_ENDPOINT;
        this.config.grpcApiBase ??= DEFAULT_GRPC_ENDPOINT;
        this.config.grpcCredentials ??= grpc.ChannelCredentials.createInsecure();
        this._clientInfo = {
            ...this._clientInfo,
            qpace: index_1.VERSION,
            qpaceCore: index_1.CORE_VERSION,
            client: "node",
            node: process.versions.node,
            platform: process.platform,
            arch: process.arch,
        };
        this._grpcOptions = {
            "grpc.max_receive_message_length": -1,
            "grpc.max_send_message_length": -1,
        };
    }
    get http() {
        if (this._http == null) {
            this._http = axios_1.default.create({
                baseURL: this.config.apiBase,
                withCredentials: true,
                headers: {
                    "Content-Type": "application/json",
                    "x-api-key": this.config.apiKey,
                    "x-info": JSON.stringify(this._clientInfo),
                },
            });
        }
        return this._http;
    }
    get grpcMetadata() {
        if (this._grpcMetadata == null) {
            const metadata = new grpc.Metadata();
            metadata.set("x-api-key", `${this.config.apiKey}`);
            if (this._clientInfo != null) {
                metadata.set("x-info", JSON.stringify(this._clientInfo));
            }
            this._grpcMetadata = metadata;
        }
        return this._grpcMetadata.clone();
    }
    async me() {
        const { data: { user }, } = await this.http.get(`/api_keys/${this.config.apiKey}`);
        return user;
    }
}
exports.Client = Client;
//# sourceMappingURL=client.js.map