"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const cdk_1 = require("@aws-cdk/cdk");
const cloudwatch_generated_1 = require("./cloudwatch.generated");
const layout_1 = require("./layout");
/**
 * A CloudWatch dashboard
 */
class Dashboard extends cdk_1.Construct {
    constructor(parent, name, props) {
        super(parent, name);
        this.rows = [];
        // WORKAROUND -- Dashboard cannot be updated if the DashboardName is missing.
        // This is a bug in CloudFormation, but we don't want CDK users to have a bad
        // experience. We'll generate a name here if you did not supply one.
        // See: https://github.com/awslabs/aws-cdk/issues/213
        const dashboardName = (props && props.dashboardName) || new cdk_1.Token(() => this.generateDashboardName());
        this.dashboard = new cloudwatch_generated_1.cloudformation.DashboardResource(this, 'Resource', {
            dashboardName,
            dashboardBody: new cdk_1.Token(() => {
                const column = new layout_1.Column(...this.rows);
                column.position(0, 0);
                return cdk_1.CloudFormationJSON.stringify({ widgets: column.toJson() });
            })
        });
    }
    /**
     * Add a widget to the dashboard.
     *
     * Widgets given in multiple calls to add() will be laid out stacked on
     * top of each other.
     *
     * Multiple widgets added in the same call to add() will be laid out next
     * to each other.
     */
    add(...widgets) {
        if (widgets.length === 0) {
            return;
        }
        const w = widgets.length > 1 ? new layout_1.Row(...widgets) : widgets[0];
        this.rows.push(w);
    }
    /**
     * Generate a unique dashboard name in case the user didn't supply one
     */
    generateDashboardName() {
        // Combination of stack name and LogicalID, which are guaranteed to be unique.
        const stack = cdk_1.Stack.find(this);
        return stack.name + '-' + this.dashboard.logicalId;
    }
}
exports.Dashboard = Dashboard;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGFzaGJvYXJkLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiZGFzaGJvYXJkLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQUEsc0NBQTJFO0FBQzNFLGlFQUF3RDtBQUN4RCxxQ0FBdUM7QUFZdkM7O0dBRUc7QUFDSCxNQUFhLFNBQVUsU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