"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseClient = void 0;
const axios_1 = require("axios");
class BaseClient {
    constructor(config) {
        this.config = config;
        this.instance = axios_1.default.create(Object.assign(Object.assign({ paramsSerializer: this.paramSerializer.bind(this) }, config.baseRequestConfig), { baseURL: 'https://api.trello.com/1' }));
    }
    paramSerializer(parameters) {
        const parts = [];
        Object.entries(parameters).forEach(([key, value]) => {
            if (value === null || typeof value === 'undefined') {
                return undefined;
            }
            if (Array.isArray(value)) {
                // eslint-disable-next-line no-param-reassign
                value = value.join(',');
            }
            if (value instanceof Date) {
                // eslint-disable-next-line no-param-reassign
                value = value.toISOString();
            }
            else if (value !== null && typeof value === 'object') {
                // eslint-disable-next-line no-param-reassign
                value = JSON.stringify(value);
            }
            parts.push(`${this.encode(key)}=${this.encode(value)}`);
            return undefined;
        });
        return parts.join('&');
    }
    encode(value) {
        return encodeURIComponent(value)
            .replace(/%3A/gi, ':')
            .replace(/%24/g, '$')
            .replace(/%2C/gi, ',')
            .replace(/%20/g, '+')
            .replace(/%5B/gi, '[')
            .replace(/%5D/gi, ']');
    }
    async sendRequest(rawRequestConfig, callback) {
        var _a, _b, _c, _d;
        const requestConfig = Object.assign(Object.assign({}, rawRequestConfig), { params: Object.assign(Object.assign({}, rawRequestConfig.params), { key: this.config.key, token: this.config.token }) });
        try {
            const response = await this.instance.request(requestConfig);
            const callbackResponseHandler = callback && ((data) => callback(null, data));
            const defaultResponseHandler = (data) => data;
            const responseHandler = callbackResponseHandler !== null && callbackResponseHandler !== void 0 ? callbackResponseHandler : defaultResponseHandler;
            (_b = (_a = this.config.middlewares) === null || _a === void 0 ? void 0 : _a.onResponse) === null || _b === void 0 ? void 0 : _b.call(_a, response.data);
            return responseHandler(response.data);
        }
        catch (e) {
            const callbackErrorHandler = callback && ((error) => callback(error));
            const defaultErrorHandler = (error) => {
                throw error;
            };
            const errorHandler = callbackErrorHandler !== null && callbackErrorHandler !== void 0 ? callbackErrorHandler : defaultErrorHandler;
            (_d = (_c = this.config.middlewares) === null || _c === void 0 ? void 0 : _c.onError) === null || _d === void 0 ? void 0 : _d.call(_c, e);
            return errorHandler(e);
        }
    }
}
exports.BaseClient = BaseClient;
