"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Client", {
    enumerable: true,
    get: function() {
        return Client;
    }
});
function _grpcjs() {
    const data = /*#__PURE__*/ _interop_require_wildcard(require("@grpc/grpc-js"));
    _grpcjs = function() {
        return data;
    };
    return data;
}
function _axios() {
    const data = /*#__PURE__*/ _interop_require_default(require("axios"));
    _axios = function() {
        return data;
    };
    return data;
}
const _packagejson = require("../../package.json");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
// 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 UserClient {
    async me() {
        const { data: { user } } = await this.client.http.get(`/api-keys/${this.client.config.apiKey}`);
        return user;
    }
    constructor(client){
        this.client = client;
    }
}
class SymClient {
    async get(query) {
        const _query = {
            ...typeof query === "string" ? {
                id: query,
                tickerId: query
            } : query,
            limit: 1
        };
        const syms = await this.syms(_query);
        if (syms.length === 0) {
            throw new Error("Symbol not found");
        }
        return syms[0];
    }
    async syms(query = {}) {
        const timeframe = query?.timeframe?.toString();
        const { data } = await this.client.http.get(`/symbols`, {
            params: {
                id_pat: query.id,
                ticker_id_pat: query.tickerId,
                limit: query?.limit,
                offset: query?.offset,
                timeframe
            }
        });
        const { Sym } = require("./index");
        return data["symbols"].map((r)=>Sym.fromJSON(r));
    }
    constructor(client){
        this.client = client;
    }
}
class Client {
    get http() {
        if (this._http == null) {
            this._http = _axios().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 (_grpcjs()).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();
    }
    constructor(config){
        this.config = config;
        this._clientInfo = {};
        this.config.apiBase ??= DEFAULT_REST_ENDPOINT;
        this.config.grpcApiBase ??= DEFAULT_GRPC_ENDPOINT;
        this.config.grpcCredentials ??= _grpcjs().ChannelCredentials.createInsecure();
        this._clientInfo = {
            ...this._clientInfo,
            qpace: _packagejson.version,
            qpaceCore: _packagejson.coreVersion,
            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
        };
        this.user = new UserClient(this);
        this.sym = new SymClient(this);
    }
}
