"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DashboardVariableSQLDynamicProvider = void 0;
const common_1 = require("@nestjs/common");
const typeorm_1 = require("typeorm");
const dashboard_selection_provider_decorator_1 = require("../../decorators/dashboard-selection-provider.decorator");
let DashboardVariableSQLDynamicProvider = class DashboardVariableSQLDynamicProvider {
    constructor(entityManager) {
        this.entityManager = entityManager;
    }
    help() {
        return "# Get the dashboard variable after executing the SQL query configured for the dashboard variable.\n";
    }
    name() {
        return 'DashboardVariableSQLDynamicProvider';
    }
    async value(optionValue, ctxt) {
        throw new Error("DashboardVariableSQLDynamicProvider does not support value method. Use values method instead.");
    }
    async values(query, ctxt) {
        const { sql, limit, offset } = ctxt;
        if (!sql) {
            throw new Error("DashboardVariableSQLDynamicProvider requires a SQL query to be provided in the context.");
        }
        const results = await this.entityManager.query(this.appendLimitOffset(sql), [limit, offset]);
        return results.map((result) => {
            const transformedResult = {
                value: result.value,
                label: result.label,
            };
            return transformedResult;
        });
    }
    appendLimitOffset(sql) {
        const trimmedSql = sql.trim().replace(/;$/, '');
        const finalSql = `${trimmedSql} LIMIT $1 OFFSET $2`;
        return finalSql;
    }
};
exports.DashboardVariableSQLDynamicProvider = DashboardVariableSQLDynamicProvider;
exports.DashboardVariableSQLDynamicProvider = DashboardVariableSQLDynamicProvider = __decorate([
    (0, dashboard_selection_provider_decorator_1.DashboardVariableSelectionProvider)(),
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [typeorm_1.EntityManager])
], DashboardVariableSQLDynamicProvider);
//# sourceMappingURL=dashboard-variable-sql-dynamic-provider.service.js.map