"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 __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PolarColumnSeriesDataLabelProvider = void 0;
var Point_1 = require("../../../../../Core/Point");
var ColumnMode_1 = require("../../../../../types/ColumnMode");
var DataLabelProviderType_1 = require("../../../../../types/DataLabelProviderType");
var DataLabelSkipMode_1 = require("../../../../../types/DataLabelSkipMode");
var TextPosition_1 = require("../../../../../types/TextPosition");
var PolarLabelMode_1 = require("../../../Axis/Polar/types/PolarLabelMode");
var labelHelper_1 = require("../../../Helpers/labelHelper");
var ColumnSeriesDataLabelProvider_1 = require("../../DataLabels/ColumnSeriesDataLabelProvider");
var PolarColumnDataLabelState_1 = require("./PolarColumnDataLabelState");
var PolarDataLabelProvider_1 = require("./PolarDataLabelProvider");
var PolarColumnSeriesDataLabelProvider = /** @class */ (function (_super) {
    __extends(PolarColumnSeriesDataLabelProvider, _super);
    function PolarColumnSeriesDataLabelProvider(options) {
        var _this = this;
        var _a, _b;
        _this = _super.call(this, __assign({ horizontalTextPosition: TextPosition_1.EHorizontalTextPosition.Center, verticalTextPosition: TextPosition_1.EVerticalTextPosition.Center, skipMode: DataLabelSkipMode_1.EDataLabelSkipMode.ShowAll }, options)) || this;
        _this.type = DataLabelProviderType_1.EDataLabelProviderType.PolarColumn;
        _this.polarLabelModeProperty = PolarLabelMode_1.EPolarLabelMode.Horizontal;
        _this.yPositionMultiplierProperty = 0.5;
        _this.labelYPositionModeProperty = ColumnSeriesDataLabelProvider_1.EColumnDataLabelPosition.Outside;
        _this.polarLabelModeProperty = (_a = options === null || options === void 0 ? void 0 : options.polarLabelMode) !== null && _a !== void 0 ? _a : _this.polarLabelModeProperty;
        _this.yPositionMultiplierProperty = options === null || options === void 0 ? void 0 : options.labelYPositionMultiplier;
        _this.labelYPositionModeProperty = (_b = options === null || options === void 0 ? void 0 : options.labelYPositionMode) !== null && _b !== void 0 ? _b : _this.labelYPositionModeProperty;
        return _this;
    }
    /** @inheritDoc */
    PolarColumnSeriesDataLabelProvider.prototype.shouldSkipLabel = function (state, label) {
        return false;
    };
    Object.defineProperty(PolarColumnSeriesDataLabelProvider.prototype, "polarLabelMode", {
        /**
         * Gets or sets polar label mode to use for calculating data label rotation. Default Horizontal
         * For a list of value, see {@link EPolarLabelMode}
         */
        get: function () {
            return this.polarLabelModeProperty;
        },
        set: function (value) {
            this.polarLabelModeProperty = value;
            this.invalidateParent();
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(PolarColumnSeriesDataLabelProvider.prototype, "yPositionMultiplier", {
        /**
         * Gets or sets y text position, where 0 is bottom and 1 is top of the column. Default 0.5
         * Use a value greater than 1 to place the label outside of the column
         */
        get: function () {
            return this.yPositionMultiplierProperty;
        },
        set: function (value) {
            this.yPositionMultiplierProperty = value;
            this.invalidateParent();
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(PolarColumnSeriesDataLabelProvider.prototype, "labelYPositionMode", {
        /**
         * For for both normal (x is angular) and vertical polar charts (y is angular), sets whether text should be positioned outside or inside the end of the column using {@link EColumnDataLabelPosition}.
         * Position Mode will position the label according to labelYPositionMultiplier where 0 corresponds to bottom y value and 1 to top
         * Default Outside
         */
        get: function () {
            return this.labelYPositionModeProperty;
        },
        set: function (value) {
            this.labelYPositionModeProperty = value;
            this.invalidateParent();
        },
        enumerable: false,
        configurable: true
    });
    PolarColumnSeriesDataLabelProvider.prototype.getDataLabelState = function (renderContext, renderPassData, style, color, yValues, parentSeries) {
        return new PolarColumnDataLabelState_1.PolarColumnDataLabelState(renderContext, renderPassData, style, color, yValues, parentSeries);
    };
    PolarColumnSeriesDataLabelProvider.prototype.getPosition = function (state, textBounds) {
        var _a;
        var rs = this.parentSeries;
        var columnMode = rs.columnXMode, defaultY1 = rs.defaultY1;
        var defaultY1Coord = state.getYCoord(defaultY1);
        if (!state.isVerticalChart) {
            var startAngle = this.parentSeries.xAxis.startAngle;
            var yPositionMultiplier = (_a = this.yPositionMultiplier) !== null && _a !== void 0 ? _a : 0.5;
            var angle = void 0;
            if (columnMode === ColumnMode_1.EColumnMode.Start) {
                var direction = state.isFlipped ? -1 : 1;
                angle = state.angleCoord() + (direction * state.columnWidth) / 2;
            }
            else if (columnMode === ColumnMode_1.EColumnMode.StartEnd) {
                var endAngle = state.x1Coord();
                angle = (state.angleCoord() + endAngle) / 2;
            }
            else if (columnMode === ColumnMode_1.EColumnMode.StartWidth) {
                var angleWidth = state.x1Coord() - state.zeroXCoord;
                angle = state.angleCoord() + angleWidth / 2;
                // } else if (columnMode === EColumnMode.Width) {
                //     angle = state.accumulatedWidthCoord() + (state.angleCoord() - state.zeroXCoord) / 2;
            }
            else {
                angle = state.angleCoord();
            }
            var visibleAngle = angle + startAngle;
            var textRadialSize = 0;
            if (this.polarLabelMode === PolarLabelMode_1.EPolarLabelMode.Parallel) {
                textRadialSize = textBounds.m_fHeight;
            }
            else if (this.polarLabelMode === PolarLabelMode_1.EPolarLabelMode.Perpendicular) {
                textRadialSize = textBounds.m_fWidth;
            }
            else if (this.polarLabelMode === PolarLabelMode_1.EPolarLabelMode.Horizontal) {
                textRadialSize =
                    Math.abs(Math.sin(visibleAngle)) * textBounds.m_fHeight +
                        Math.abs(Math.cos(visibleAngle)) * textBounds.m_fWidth;
            }
            else {
                throw new Error("PolarColumnSeriesDataLabelProvider EPolarLabelMode ".concat(this.polarLabelMode, " is not supported for vertical chart"));
            }
            var radius = void 0;
            if (this.labelYPositionMode === ColumnSeriesDataLabelProvider_1.EColumnDataLabelPosition.Outside) {
                // angle = flippedCoordinates ? angle - textAngularSize : angle + textAngularSize;
                radius = state.radiusCoord() + textRadialSize;
            }
            else if (this.labelYPositionMode === ColumnSeriesDataLabelProvider_1.EColumnDataLabelPosition.Inside) {
                radius = state.radiusCoord() - textRadialSize;
            }
            else if (this.labelYPositionMode === ColumnSeriesDataLabelProvider_1.EColumnDataLabelPosition.Position) {
                radius = defaultY1Coord + (state.radiusCoord() - defaultY1Coord) * yPositionMultiplier;
            }
            var _b = state.toCartesian(angle, radius), x = _b.x, y = _b.y;
            var rotationCenter = new Point_1.Point(x, y);
            var rotationAngle = labelHelper_1.labelHelper.getPolarLabelRotation(this.polarLabelMode, visibleAngle);
            x -= textBounds.m_fWidth / 2;
            var lineBounds = textBounds.GetLineBounds(0);
            var yOffset = textBounds.m_fHeight - lineBounds.m_fHeight;
            lineBounds.delete();
            // Handle multiline
            y -= yOffset;
            y += textBounds.m_fHeight / 2;
            return { position: new Point_1.Point(x, y), rotationCenter: rotationCenter, rotationAngle: rotationAngle };
        }
        else {
            var angularAxis = this.parentSeries.yAxis;
            var startAngle = angularAxis.startAngle, flippedCoordinates = angularAxis.flippedCoordinates;
            var angle = state.angleCoord();
            var visibleAngle = angle + startAngle;
            var zeroLineAngle = state.getYCoord(defaultY1);
            var radius = void 0;
            if (columnMode === ColumnMode_1.EColumnMode.Start) {
                var direction = state.isFlipped ? -1 : 1;
                radius = state.radiusCoord() + (direction * state.columnWidth) / 2;
            }
            else if (columnMode === ColumnMode_1.EColumnMode.StartEnd) {
                var endRadius = state.x1Coord();
                radius = (state.radiusCoord() + endRadius) / 2;
            }
            else if (columnMode === ColumnMode_1.EColumnMode.StartWidth) {
                var radialWidth = state.x1Coord() - state.zeroXCoord;
                radius = state.radiusCoord() + radialWidth / 2;
                // } else if (columnMode === EColumnMode.Width) {
                //     radius = state.accumulatedWidthCoord() + (state.radiusCoord() - state.zeroXCoord) / 2;
            }
            else if (columnMode === ColumnMode_1.EColumnMode.Mid || columnMode === ColumnMode_1.EColumnMode.MidWidth) {
                radius = state.radiusCoord();
            }
            else {
                throw new Error("PolarColumnSeriesDataLabelProvider EColumnMode ".concat(columnMode, " is not supported for vertical chart"));
            }
            var textAngularSize = 0;
            if (this.polarLabelMode === PolarLabelMode_1.EPolarLabelMode.Parallel) {
                textAngularSize = Math.atan2(textBounds.m_fWidth, radius);
            }
            else if (this.polarLabelMode === PolarLabelMode_1.EPolarLabelMode.Perpendicular) {
                textAngularSize = Math.atan2(textBounds.m_fHeight, radius);
            }
            else if (this.polarLabelMode === PolarLabelMode_1.EPolarLabelMode.Horizontal) {
                var textSize = Math.abs(Math.sin(visibleAngle)) * textBounds.m_fWidth +
                    Math.abs(Math.cos(visibleAngle)) * textBounds.m_fHeight;
                textAngularSize = Math.atan2(textSize, radius);
            }
            else {
                throw new Error("PolarColumnSeriesDataLabelProvider EPolarLabelMode ".concat(this.polarLabelMode, " is not supported for vertical chart"));
            }
            if (this.labelYPositionMode === ColumnSeriesDataLabelProvider_1.EColumnDataLabelPosition.Outside) {
                angle = flippedCoordinates ? angle - textAngularSize : angle + textAngularSize;
            }
            else if (this.labelYPositionMode === ColumnSeriesDataLabelProvider_1.EColumnDataLabelPosition.Inside) {
                angle = flippedCoordinates ? angle + textAngularSize : angle - textAngularSize;
            }
            else if (this.labelYPositionMode === ColumnSeriesDataLabelProvider_1.EColumnDataLabelPosition.Position) {
                angle = zeroLineAngle + (angle - zeroLineAngle) * this.yPositionMultiplier;
            }
            var _c = state.toCartesian(angle, radius), x = _c.x, y = _c.y;
            var rotationCenter = new Point_1.Point(x, y);
            var rotationAngle = labelHelper_1.labelHelper.getPolarLabelRotation(this.polarLabelMode, angle + startAngle);
            x -= textBounds.m_fWidth / 2;
            var lineBounds = textBounds.GetLineBounds(0);
            var yOffset = textBounds.m_fHeight - textBounds.GetLineBounds(0).m_fHeight;
            lineBounds.delete();
            // Handle multiline
            y -= yOffset;
            y += textBounds.m_fHeight / 2;
            return { position: new Point_1.Point(x, y), rotationCenter: rotationCenter, rotationAngle: rotationAngle };
        }
    };
    PolarColumnSeriesDataLabelProvider.prototype.toJSON = function () {
        var json = _super.prototype.toJSON.call(this);
        var options = {
            polarLabelMode: this.polarLabelMode,
            labelYPositionMultiplier: this.yPositionMultiplier,
            labelYPositionMode: this.labelYPositionMode
        };
        Object.assign(json.options, options);
        return json;
    };
    Object.defineProperty(PolarColumnSeriesDataLabelProvider.prototype, "useRotation", {
        get: function () {
            return this.polarLabelMode !== PolarLabelMode_1.EPolarLabelMode.Horizontal;
        },
        enumerable: false,
        configurable: true
    });
    return PolarColumnSeriesDataLabelProvider;
}(PolarDataLabelProvider_1.PolarDataLabelProvider));
exports.PolarColumnSeriesDataLabelProvider = PolarColumnSeriesDataLabelProvider;
