"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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.NativeAxisRenderer = void 0;
var TextPosition_1 = require("../../../types/TextPosition");
var parseColor_1 = require("../../../utils/parseColor");
var NativeObject_1 = require("../Helpers/NativeObject");
var AxisRenderer_1 = require("./AxisRenderer");
/**
 * AxisRenderer subclass that uses native WASM font rendering (DrawStringAdvanced).
 * Used when {@link LabelProviderBase2D.useNativeText} is true.
 */
var NativeAxisRenderer = /** @class */ (function (_super) {
    __extends(NativeAxisRenderer, _super);
    function NativeAxisRenderer() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    /** @inheritDoc */
    NativeAxisRenderer.prototype.onBeginDrawLabels = function (renderContext, labelProvider, labelStyle) {
        this.nativeFont = renderContext.getFont(labelStyle, labelProvider.rotation !== 0);
        this.textBounds = (0, NativeObject_1.getTextBounds)(this.webAssemblyContext);
        this.mlaNative = (0, TextPosition_1.convertMultiLineAlignment)(labelStyle.multilineAlignment, this.webAssemblyContext);
        this.nativeLineSpacing = labelProvider.lineSpacing;
        return this.calculateLineHightForNativeFont(this.nativeFont, this.textBounds);
    };
    /** @inheritDoc */
    NativeAxisRenderer.prototype.measureLabels = function (tickLabels, labelProvider, ctx, labelStyle, labelInfos) {
        if (!labelProvider.useCache) {
            var labelHeights = [];
            var labelWidths = [];
            for (var _i = 0, tickLabels_1 = tickLabels; _i < tickLabels_1.length; _i++) {
                var label = tickLabels_1[_i];
                this.nativeFont.CalculateStringBounds(label !== null && label !== void 0 ? label : "", this.textBounds, this.nativeLineSpacing);
                labelHeights.push(this.textBounds.m_fHeight + labelStyle.padding.top + labelStyle.padding.bottom);
                labelWidths.push(this.textBounds.m_fWidth + labelStyle.padding.left + labelStyle.padding.right);
            }
            return { labelHeights: labelHeights, labelWidths: labelWidths };
        }
        return _super.prototype.measureLabels.call(this, tickLabels, labelProvider, ctx, labelStyle, labelInfos);
    };
    /** @inheritDoc */
    NativeAxisRenderer.prototype.drawSingleLabel = function (renderContext, labelProvider, labelText, labelInfo, xCoord, yCoord, tickCoord, labelWidth, labelHeight, index, labelStyle, clipRect, isHorizontal) {
        var padding = labelStyle.padding;
        var viewRect = this.currentViewRect;
        var lineHeight = this.currentLineHeight;
        var rotationRad = this.currentRotationRad;
        var tx = xCoord + viewRect.left + padding.left;
        var ty = yCoord + viewRect.top + lineHeight + padding.top;
        var rxc = tx;
        var ryc = ty - lineHeight;
        if (isHorizontal && rotationRad !== 0) {
            var nativeLabelInfo = labelInfo !== null && labelInfo !== void 0 ? labelInfo : labelProvider.getNativeLabelInfo(labelText);
            var textWidth = nativeLabelInfo.textWidth, textHeight = nativeLabelInfo.textHeight;
            tx =
                tickCoord +
                    viewRect.left -
                    (textHeight * Math.sin(rotationRad)) / 2;
            rxc = tx;
            if (rotationRad > 0) {
                // rotating up
                tx -= textWidth;
            }
        }
        if (!isHorizontal) {
            // Vertical: rotation center = position
            rxc = tx;
            ryc = ty;
        }
        var _a = labelProvider.adjustLabel(index, labelText, labelWidth, labelHeight, tx, ty, rxc, ryc, rotationRad), text = _a.text, x = _a.x, y = _a.y, rx = _a.rx, ry = _a.ry, rotationRadians = _a.rotationRadians, color = _a.color;
        this.nativeFont.DrawStringAdvanced(text !== null && text !== void 0 ? text : "", color !== null && color !== void 0 ? color : this.currentTextColor, Math.round(x), Math.round(y), (0, NativeObject_1.getVector4)(this.webAssemblyContext, rx, ry, rotationRadians, 0), this.mlaNative, this.nativeLineSpacing);
    };
    /** @inheritDoc */
    NativeAxisRenderer.prototype.measureModifierAxisLabel = function (renderContext, displayValue, textStyle, _fill, effectivePadding, _cornerRadius) {
        var _a, _b;
        this.modifierNativeFont = renderContext.getFont(textStyle, false, true);
        this.modifierTextBounds = (0, NativeObject_1.getTextBounds)(this.webAssemblyContext);
        this.modifierLineSpacing = (_b = (_a = this.parentAxis.labelProvider) === null || _a === void 0 ? void 0 : _a.lineSpacing) !== null && _b !== void 0 ? _b : 0;
        this.modifierNativeFont.CalculateStringBounds(displayValue, this.modifierTextBounds, this.modifierLineSpacing);
        return {
            textureWidth: this.modifierTextBounds.m_fWidth + effectivePadding.left + effectivePadding.right,
            textureHeight: this.modifierTextBounds.m_fHeight + effectivePadding.top + effectivePadding.bottom
        };
    };
    /** @inheritDoc */
    NativeAxisRenderer.prototype.drawModifierAxisLabelSpecific = function (renderContext, displayValue, textStyle, fill, effectivePadding, cornerRadius, xPosition, yPosition, textureWidth, textureHeight, _clipRect) {
        if (fill) {
            var roundedRectTexture = this.textureManager.createFilledRectTexture(textureWidth, textureHeight, fill, cornerRadius);
            renderContext.drawTexture(roundedRectTexture.bitmapTexture, Math.round(xPosition), Math.round(yPosition), textureWidth, textureHeight);
            roundedRectTexture.bitmapTexture.delete();
        }
        var textColor = (0, parseColor_1.parseColorToUIntArgb)(textStyle.color);
        var lineHeight = this.calculateLineHightForNativeFont(this.modifierNativeFont, this.modifierTextBounds);
        var textX = xPosition + effectivePadding.left;
        var textY = yPosition + lineHeight + effectivePadding.top;
        this.modifierNativeFont.DrawStringAdvanced(displayValue, textColor, Math.round(textX), Math.round(textY), (0, NativeObject_1.getVector4)(this.webAssemblyContext, 0, 0, 0, 0), this.webAssemblyContext.eTSRTextAlignMode.Left, this.modifierLineSpacing);
        this.modifierNativeFont.End();
    };
    /** @inheritDoc */
    NativeAxisRenderer.prototype.onEndDrawLabels = function () {
        // nativeFont is managed by the renderContext font cache
    };
    return NativeAxisRenderer;
}(AxisRenderer_1.AxisRenderer));
exports.NativeAxisRenderer = NativeAxisRenderer;
