"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
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 });
var core_1 = require("@ag-grid-community/core");
var fontPanel_1 = require("../fontPanel");
var TitlePanel = /** @class */ (function (_super) {
    __extends(TitlePanel, _super);
    function TitlePanel(chartController) {
        var _this = _super.call(this, TitlePanel.TEMPLATE) || this;
        _this.activePanels = [];
        _this.chartController = chartController;
        return _this;
    }
    TitlePanel.prototype.init = function () {
        this.initFontPanel();
    };
    TitlePanel.prototype.hasTitle = function () {
        var chartProxy = this.chartController.getChartProxy();
        var title = chartProxy.getChartOption('title');
        return title && title.enabled && title.text && title.text.length > 0;
    };
    TitlePanel.prototype.initFontPanel = function () {
        var _this = this;
        var chartProxy = this.chartController.getChartProxy();
        var hasTitle = this.hasTitle;
        var setFont = function (font) {
            var proxy = _this.chartController.getChartProxy();
            if (font.family) {
                proxy.setTitleOption('fontFamily', font.family);
            }
            if (font.weight) {
                proxy.setTitleOption('fontWeight', font.weight);
            }
            if (font.style) {
                proxy.setTitleOption('fontStyle', font.style);
            }
            if (font.size) {
                proxy.setTitleOption('fontSize', font.size);
            }
            if (font.color) {
                proxy.setTitleOption('color', font.color);
            }
        };
        var initialFont = {
            family: chartProxy.getChartOption('title.fontFamily'),
            style: chartProxy.getChartOption('title.fontStyle'),
            weight: chartProxy.getChartOption('title.fontWeight'),
            size: chartProxy.getChartOption('title.fontSize'),
            color: chartProxy.getChartOption('title.color')
        };
        if (!hasTitle) {
            setFont(initialFont);
        }
        var fontPanelParams = {
            name: this.chartTranslator.translate('title'),
            enabled: this.hasTitle(),
            suppressEnabledCheckbox: false,
            initialFont: initialFont,
            setFont: setFont,
            setEnabled: function (enabled) {
                var proxy = _this.chartController.getChartProxy();
                if (enabled) {
                    var newTitle = _this.disabledTitle || _this.chartTranslator.translate('titlePlaceholder');
                    proxy.setTitleOption('text', newTitle);
                    _this.disabledTitle = '';
                }
                else {
                    _this.disabledTitle = proxy.getTitleOption('text');
                    proxy.setTitleOption('text', '');
                }
            }
        };
        var fontPanelComp = this.createBean(new fontPanel_1.FontPanel(fontPanelParams));
        this.getGui().appendChild(fontPanelComp.getGui());
        this.activePanels.push(fontPanelComp);
        // edits to the title can disable it, so keep the checkbox in sync:
        this.addManagedListener(this.eventService, 'chartTitleEdit', function () {
            fontPanelComp.setEnabled(_this.hasTitle());
        });
    };
    TitlePanel.prototype.destroyActivePanels = function () {
        var _this = this;
        this.activePanels.forEach(function (panel) {
            core_1._.removeFromParent(panel.getGui());
            _this.destroyBean(panel);
        });
    };
    TitlePanel.prototype.destroy = function () {
        this.destroyActivePanels();
        _super.prototype.destroy.call(this);
    };
    TitlePanel.TEMPLATE = "<div></div>";
    __decorate([
        core_1.Autowired('chartTranslator')
    ], TitlePanel.prototype, "chartTranslator", void 0);
    __decorate([
        core_1.PostConstruct
    ], TitlePanel.prototype, "init", null);
    return TitlePanel;
}(core_1.Component));
exports.default = TitlePanel;
//# sourceMappingURL=titlePanel.js.map