'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var logger = require('../utils/logger.js');
var restClient = require('./restClient.js');

class ReportingClient {
    restClient;
    constructor(config) {
        this.restClient = new restClient.RestClient('Reporting API Client', config);
    }
    getCount(resourceName, token, countRequest) {
        logger.default.info(`Reporting client: Get count. { resourceName: ${resourceName} }`);
        return this.restClient
            .get({
            path: `/${resourceName}/count`,
            query: countRequest.toRecordWithFilterPrefix(true),
            token,
        })
            .then((response) => response.count);
    }
    getList(resourceName, token, listRequest) {
        return this.getListWithWarnings(resourceName, token, listRequest).then((response) => response.data);
    }
    getListWithWarnings(resourceName, token, listRequest) {
        logger.default.info(`Reporting client: Get list. { resourceName: ${resourceName} }`);
        return this.restClient
            .getWithHeaders({
            path: `/${resourceName}`,
            query: listRequest.toRecordWithFilterPrefix(true),
            token,
        })
            .then((response) => ({
            data: response.data,
            warnings: {
                noDataAvailable: response.headers['x-no-data-warning'],
            },
        }));
    }
    getDefinitionSummary(token, reportId, definitionsPath) {
        this.logInfo('Get definition summary', { reportId });
        const queryParams = {
            ...(definitionsPath && { dataProductDefinitionsPath: definitionsPath }),
        };
        return this.restClient
            .get({
            path: `/definitions/${reportId}`,
            query: queryParams,
            token,
        })
            .then((response) => response);
    }
    getDefinitions(token, definitionsPath) {
        this.logInfo('Get definitions');
        const queryParams = {
            renderMethod: 'HTML',
            ...(definitionsPath && { dataProductDefinitionsPath: definitionsPath }),
        };
        return this.restClient
            .get({
            path: '/definitions',
            query: queryParams,
            token,
        })
            .then((response) => response);
    }
    getDefinition(token, reportId, variantId, definitionsPath, queryData) {
        const query = {
            ...queryData,
            dataProductDefinitionsPath: definitionsPath,
        };
        this.logInfo('Get definition', { reportId, variantId, ...query });
        return this.restClient
            .get({
            path: `/definitions/${reportId}/${variantId}`,
            query,
            token,
        })
            .then((response) => response);
    }
    requestAsyncReport(token, reportId, variantId, query) {
        this.logInfo('Request report', { reportId, variantId });
        return this.restClient
            .get({
            path: `/async/reports/${reportId}/${variantId}`,
            token,
            query,
        })
            .then((response) => response);
    }
    cancelAsyncRequest(token, reportId, variantId, executionId, dataProductDefinitionsPath) {
        this.logInfo('Cancel Request', { reportId, variantId, executionId });
        return this.restClient
            .delete({
            path: `/reports/${reportId}/${variantId}/statements/${executionId}`,
            token,
            query: {
                dataProductDefinitionsPath,
            },
        })
            .then((response) => response);
    }
    downloadAsyncReport(token, reportId, variantId, tableId, query, res) {
        this.logInfo('Streaming download data', { reportId, variantId, tableId });
        return this.restClient.getStream({
            path: `/reports/${reportId}/${variantId}/tables/${tableId}/download`,
            query,
            token,
        }, res);
    }
    downloadSyncReport(token, resourceName, query, res) {
        this.logInfo('Streaming download data', { resourceName });
        return this.restClient.getStream({
            path: `/${resourceName}/download`,
            query,
            token,
        }, res);
    }
    getAsyncReport(token, reportId, variantId, tableId, query) {
        this.logInfo('Get Data', { reportId, variantId, tableId });
        return this.restClient
            .get({
            path: `/reports/${reportId}/${variantId}/tables/${tableId}/result`,
            token,
            query,
        })
            .then((response) => response);
    }
    getAsyncSummaryReport(token, reportId, variantId, tableId, summaryId, query) {
        this.logInfo('Get summary data', { reportId, variantId, tableId, summaryId });
        return this.restClient
            .get({
            path: `/reports/${reportId}/${variantId}/tables/${tableId}/result/summary/${summaryId}`,
            token,
            query,
        })
            .then((response) => response);
    }
    getAsyncReportStatus(token, reportId, variantId, executionId, dataProductDefinitionsPath, tableId) {
        this.logInfo('Get status', { reportId, variantId, tableId, executionId });
        return this.restClient
            .get({
            path: `/reports/${reportId}/${variantId}/statements/${executionId}/status`,
            token,
            query: {
                dataProductDefinitionsPath,
                tableId,
            },
        })
            .then((response) => response);
    }
    getAsyncCount(token, tableId, dataProductDefinitionsPath) {
        this.logInfo('Get count', { tableId });
        return this.restClient
            .get({
            path: `/report/tables/${tableId}/count`,
            token,
            query: {
                dataProductDefinitionsPath,
            },
        })
            .then((response) => response.count);
    }
    getAsyncInteractiveCount(token, tableId, reportId, id, filters) {
        this.logInfo('Get interactive count', { tableId, reportId, id });
        return this.restClient
            .get({
            path: `/reports/${reportId}/${id}/tables/${tableId}/count`,
            token,
            query: filters.toRecordWithFilterPrefix(true),
        })
            .then((response) => response.count);
    }
    /**
     * Gets the expiry state for tables
     *
     * @param {string} token
     * @param {string[]} tableIds
     * @return {*}
     * @memberof ReportingClient
     */
    getTableExpiryState(token, tableIds) {
        return this.restClient
            .post({
            path: `/reports/tableExpiryState`,
            data: { tableIds },
        }, token)
            .then((response) => response);
    }
    logInfo(title, args) {
        logger.default.info(`Reporting Client: ${title}:`);
        if (args && Object.keys(args).length)
            logger.default.info(JSON.stringify(args, null, 2));
    }
}

exports.ReportingClient = ReportingClient;
exports.default = ReportingClient;
//# sourceMappingURL=reportingClient.js.map
