"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DashboardMapper = void 0;
const common_1 = require("@nestjs/common");
let DashboardMapper = class DashboardMapper {
    toDto(dashboard) {
        return {
            name: dashboard.name,
            layoutJson: this.safeParseJSON(dashboard.layoutJson, {}),
            moduleUserKey: dashboard.module?.name ?? null,
            dashboardVariables: (dashboard.dashboardVariables || []).map(variable => ({
                variableName: variable.variableName,
                variableType: variable.variableType,
                selectionStaticValues: this.safeParseJSON(variable.selectionStaticValues, []),
                selectionDynamicSourceType: variable.selectionDynamicSourceType,
                selectionDynamicSQL: variable.selectionDynamicSQL ?? null,
                selectionDynamicProviderName: variable.selectionDynamicProviderName ?? null,
                defaultValue: this.safeParseJSON(variable.defaultValue, []),
                defaultOperator: variable.defaultOperator ?? null,
            })),
            questions: (dashboard.questions || []).map(question => ({
                name: question.name,
                sourceType: question.sourceType,
                visualisedAs: question.visualisedAs,
                providerName: question.providerName ?? null,
                chartOptions: question.chartOptions ?? null,
                labelSql: question.labelSql ?? null,
                kpiSql: question.kpiSql ?? null,
                externalId: question.externalId,
                questionSqlDatasetConfigs: (question.questionSqlDatasetConfigs || []).map(config => ({
                    sql: config.sql,
                    datasetName: config.datasetName,
                    datasetDisplayName: config.datasetDisplayName,
                    description: config.description,
                    labelColumnName: config.labelColumnName,
                    valueColumnName: config.valueColumnName,
                    options: this.safeParseJSON(config.options, {}),
                    externalId: config.externalId
                }))
            }))
        };
    }
    safeParseJSON(json, fallback) {
        try {
            return json ? JSON.parse(json) : fallback;
        }
        catch {
            return fallback;
        }
    }
};
exports.DashboardMapper = DashboardMapper;
exports.DashboardMapper = DashboardMapper = __decorate([
    (0, common_1.Injectable)()
], DashboardMapper);
//# sourceMappingURL=dashboard-mapper.js.map