"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SendbirdClient = void 0;
const axios_1 = __importDefault(require("axios"));
class SendbirdBaseAPI {
    constructor({ applicationId, baseUrl, token, tokenHeaderKey, }) {
        this.post = async (url, { body }) => {
            return await axios_1.default.post(`${this._baseUrl}/${url}`, body, {
                headers: this._headers,
            });
        };
        this.put = async (url, { body }) => {
            return await axios_1.default.put(`${this._baseUrl}/${url}`, body, {
                headers: this._headers,
            });
        };
        this.patch = async (url, { body }) => {
            return await axios_1.default.patch(`${this._baseUrl}/${url}`, body, {
                headers: this._headers,
            });
        };
        this.get = async (url) => {
            return await axios_1.default.get(`${this._baseUrl}/${url}`, {
                headers: this._headers,
            });
        };
        this.delete = async (url) => {
            return await axios_1.default.delete(`${this._baseUrl}/${url}`, {
                headers: this._headers,
            });
        };
        this._applicationId = applicationId;
        this._baseUrl = baseUrl;
        this._token = token;
        this._headers = {
            'Content-Type': 'application/json; charset=utf8',
            [tokenHeaderKey]: this._token,
        };
    }
}
class SendbirdChatAPI {
    constructor({ applicationId, token, }) {
        this.createUser = async (user) => {
            return await this._baseApi.post('users', {
                body: user,
            });
        };
        this.updateUser = async ({ user_id, ...user }) => {
            return await this._baseApi.put(`users/${encodeURIComponent(user_id)}`, {
                body: user,
            });
        };
        this.getUser = async (userId) => {
            return await this._baseApi.get(`users/${encodeURIComponent(userId)}`);
        };
        this.deleteUser = async (userId) => {
            return await this._baseApi.delete(`users/${encodeURIComponent(userId)}`);
        };
        this.updateMetadata = async (userId, metadata) => {
            return await this._baseApi.put(`users/${encodeURIComponent(userId)}/metadata`, {
                body: { metadata, upsert: true },
            });
        };
        /**
         * @description if `key` is empty -> removes all metadata
         */
        this.deleteMetadata = async (userId, key) => {
            const deleteAllKeys = key === undefined || key === '';
            return await this._baseApi.delete(`users/${encodeURIComponent(userId)}/metadata${deleteAllKeys ? '' : `/${encodeURIComponent(key)}`}`);
        };
        this._baseApi = new SendbirdBaseAPI({
            applicationId,
            baseUrl: `https://api-${applicationId}.sendbird.com/v3`,
            token,
            tokenHeaderKey: 'Api-Token',
        });
    }
}
class SendbirdDeskAPI {
    constructor({ applicationId, token, }) {
        this.createCustomer = async (customer) => {
            return await this._baseApi.post('customers', {
                body: customer,
            });
        };
        this.getCustomer = async (customerId) => {
            return await this._baseApi.get(`customers/${customerId}`);
        };
        this.updateCustomerCustomFields = async (customerId, { customFields }) => {
            return await this._baseApi.patch(`customers/${customerId}/custom_fields`, {
                body: { customFields },
            });
        };
        this.createTicket = async (ticket) => {
            return await this._baseApi.post('tickets', {
                body: ticket,
            });
        };
        this.updateTicket = async (ticketId, ticket) => {
            return await this._baseApi.patch(`tickets/${ticketId}`, {
                body: ticket,
            });
        };
        this._baseApi = new SendbirdBaseAPI({
            applicationId,
            baseUrl: `https://desk-api-${applicationId}.sendbird.com/platform/v1`,
            token,
            tokenHeaderKey: 'SENDBIRDDESKAPITOKEN',
        });
    }
}
class SendbirdClient {
    constructor({ applicationId, chatApiToken, deskApiToken, }) {
        this.chatApi = new SendbirdChatAPI({ applicationId, token: chatApiToken });
        this.deskApi = new SendbirdDeskAPI({ applicationId, token: deskApiToken });
    }
}
exports.SendbirdClient = SendbirdClient;
//# sourceMappingURL=client.js.map