"use strict";

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.Title = void 0;

const vrender_1 = require("./../../vrender"), padding_1 = require("../../scenegraph/utils/padding"), vutils_1 = require("@visactor/vutils");

class Title {
    constructor(titleOption, table) {
        this.isReleased = !1, this.table = table, this._titleOption = (0, vutils_1.merge)({
            orient: "top"
        }, titleOption), !1 !== titleOption.visible && (this._titleComponent = this._createOrUpdateTitleComponent(this._getTitleAttrs()));
    }
    _createOrUpdateTitleComponent(attrs) {
        if (this._titleComponent) (0, vutils_1.isEqual)(attrs, this._cacheAttrs) || this._titleComponent.setAttributes(attrs); else {
            const title = new vrender_1.Title(attrs);
            title.name = "title", this.table.scenegraph.stage.defaultLayer.appendChild(title), 
            this._titleComponent = title;
        }
        return this._adjustTableSize(this._titleComponent.attribute), this._titleComponent;
    }
    resize() {
        var _a, _b, _c, _d, _e;
        if (!this._titleComponent) return;
        const padding = (0, padding_1.getQuadProps)(null !== (_a = this._titleOption.padding) && void 0 !== _a ? _a : 10), realWidth = null !== (_b = this._titleOption.width) && void 0 !== _b ? _b : ("adaptive" === this.table.widthMode ? this.table.tableNoFrameWidth : Math.min(this.table.tableNoFrameWidth, this.table.getDrawRange().width)) - padding[1] - padding[3], realHeight = null !== (_c = this._titleOption.height) && void 0 !== _c ? _c : ("adaptive" === this.table.heightMode ? this.table.tableNoFrameHeight : Math.min(this.table.tableNoFrameHeight, this.table.getDrawRange().height)) - padding[0] - padding[2];
        this._titleComponent.setAttributes({
            x: (null !== (_d = this._titleOption.x) && void 0 !== _d ? _d : "right" === this._titleOption.orient) ? this.table.tableX + Math.min(this.table.tableNoFrameWidth, this.table.getDrawRange().width) - this.table._tableBorderWidth_left : this.table.tableX - this.table._tableBorderWidth_left,
            y: (null !== (_e = this._titleOption.y) && void 0 !== _e ? _e : "bottom" === this._titleOption.orient) ? this.table.tableY + this.table.tableNoFrameHeight - this.table._tableBorderWidth_top : this.table.tableY - this.table._tableBorderWidth_top,
            width: "top" === this._titleOption.orient || "bottom" === this._titleOption.orient ? realWidth : this._titleOption.width,
            height: "left" === this._titleOption.orient || "right" === this._titleOption.orient ? realHeight : this._titleOption.height,
            textStyle: Object.assign({
                width: realWidth
            }, this._titleOption.textStyle),
            subtextStyle: Object.assign({
                width: realWidth
            }, this._titleOption.subtextStyle)
        }), this._adjustTableSize(this._titleComponent.attribute);
    }
    _adjustTableSize(attrs) {
        var _a, _b, _c, _d;
        const width = isFinite(null === (_a = this._titleComponent) || void 0 === _a ? void 0 : _a.AABBBounds.width()) ? this._titleComponent.AABBBounds.width() : 0, height = isFinite(null === (_b = this._titleComponent) || void 0 === _b ? void 0 : _b.AABBBounds.height()) ? this._titleComponent.AABBBounds.height() : 0;
        "left" === this._titleOption.orient ? (this.table.tableNoFrameWidth = this.table.tableNoFrameWidth - Math.ceil(width), 
        this.table.tableX += Math.ceil(width)) : "top" === this._titleOption.orient ? (this.table.tableNoFrameHeight = this.table.tableNoFrameHeight - Math.ceil(height), 
        this.table.tableY += Math.ceil(height)) : "right" === this._titleOption.orient ? this.table.tableNoFrameWidth = this.table.tableNoFrameWidth - Math.ceil(width) : "bottom" === this._titleOption.orient && (this.table.tableNoFrameHeight = this.table.tableNoFrameHeight - Math.ceil(height)), 
        this._cacheAttrs = attrs, "right" !== this._titleOption.orient && "bottom" !== this._titleOption.orient || this._titleComponent.setAttributes({
            x: (null !== (_c = this._titleOption.x) && void 0 !== _c ? _c : "right" === this._titleOption.orient) ? this.table.tableX + this.table.tableNoFrameWidth : this.table.tableX,
            y: (null !== (_d = this._titleOption.y) && void 0 !== _d ? _d : "bottom" === this._titleOption.orient) ? this.table.tableY + this.table.tableNoFrameHeight : this.table.tableY
        });
    }
    release() {
        this._titleComponent && this.table.scenegraph.stage.defaultLayer.removeChild(this._titleComponent), 
        this._titleComponent = null, this.isReleased = !0;
    }
    _getTitleAttrs() {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
        const defaultPadding = this._titleOption.text || this._titleOption.subtext ? 10 : 0, padding = (0, 
        padding_1.getQuadProps)(null !== (_a = this._titleOption.padding) && void 0 !== _a ? _a : defaultPadding), realWidth = null !== (_b = this._titleOption.width) && void 0 !== _b ? _b : Math.min(this.table.tableNoFrameWidth, this.table.getDrawRange().width) - padding[1] - padding[3], realHeight = null !== (_c = this._titleOption.height) && void 0 !== _c ? _c : Math.min(this.table.tableNoFrameHeight, this.table.getDrawRange().height) - padding[0] - padding[2];
        return {
            text: null !== (_d = this._titleOption.text) && void 0 !== _d ? _d : "",
            subtext: null !== (_e = this._titleOption.subtext) && void 0 !== _e ? _e : "",
            x: (null !== (_f = this._titleOption.x) && void 0 !== _f ? _f : "right" === this._titleOption.orient) ? this.table.tableX + Math.min(this.table.tableNoFrameWidth, this.table.getDrawRange().width) - this.table._tableBorderWidth_left : this.table.tableX - this.table._tableBorderWidth_left,
            y: (null !== (_g = this._titleOption.y) && void 0 !== _g ? _g : "bottom" === this._titleOption.orient) ? this.table.tableY + this.table.tableNoFrameHeight - this.table._tableBorderWidth_top : this.table.tableY - this.table._tableBorderWidth_top,
            width: "top" === this._titleOption.orient || "bottom" === this._titleOption.orient ? realWidth : this._titleOption.width,
            height: "left" === this._titleOption.orient || "right" === this._titleOption.orient ? realHeight : this._titleOption.height,
            minWidth: this._titleOption.minWidth,
            maxWidth: this._titleOption.maxWidth,
            minHeight: this._titleOption.minHeight,
            maxHeight: this._titleOption.maxHeight,
            padding: this._titleOption.padding,
            align: null !== (_h = this._titleOption.align) && void 0 !== _h ? _h : "left",
            verticalAlign: null !== (_j = this._titleOption.verticalAlign) && void 0 !== _j ? _j : "top",
            textStyle: Object.assign({
                width: realWidth
            }, this._titleOption.textStyle),
            subtextStyle: Object.assign({
                width: realWidth
            }, this._titleOption.subtextStyle),
            dx: null !== (_k = this._titleOption.dx) && void 0 !== _k ? _k : 0,
            dy: null !== (_l = this._titleOption.dy) && void 0 !== _l ? _l : 0
        };
    }
    getComponentGraphic() {
        return this._titleComponent;
    }
}

exports.Title = Title;
//# sourceMappingURL=title.js.map
