"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.SvgAnnotationBase = void 0;
var AnchorPoint_1 = require("../../../types/AnchorPoint");
var constants_1 = require("./constants");
var DomAnnotationBase_1 = require("./DomAnnotationBase");
var EAnnotationLayer_1 = require("./types/EAnnotationLayer");
/**
 * The Base class for an {@link AnnotationBase | Annotation} which draws using an HTML5 SVG canvas
 */
var SvgAnnotationBase = /** @class */ (function (_super) {
    __extends(SvgAnnotationBase, _super);
    /**
     * Creates an instance of an SvgAnnotationbase
     * @param options Optional parameters of type {@link ISvgAnnotationBaseOptions} used to configure the annotation on construction
     */
    function SvgAnnotationBase(options) {
        var _this = _super.call(this, options) || this;
        /** @inheritDoc */
        _this.isSvgAnnotation = true;
        return _this;
    }
    /** @inheritDoc */
    SvgAnnotationBase.prototype.onDetach = function () {
        _super.prototype.onDetach.call(this);
        this.delete();
    };
    /**
     * Updates the annotation position, with the {@link CoordinateCalculatorBase | Coordinate Calculators} passed in
     * @param xCalc The XAxis {@link CoordinateCalculatorBase | CoordinateCalculator} applied to this annotation
     * @param yCalc The YAxis {@link CoordinateCalculatorBase | CoordinateCalculator} applied to this annotation
     * @param xCoordSvgTrans X-coordinate translation which is needed to use SVG canvas having the whole chart size
     * @param yCoordSvgTrans Y-coordinate translation which is needed to use SVG canvas having the whole chart size
     */
    SvgAnnotationBase.prototype.update = function (xCalc, yCalc, xCoordSvgTrans, yCoordSvgTrans) {
        var _a, _b;
        this.xCoordSvgTransProperty = xCoordSvgTrans;
        this.yCoordSvgTransProperty = yCoordSvgTrans;
        this.create(xCalc, yCalc, xCoordSvgTrans, yCoordSvgTrans);
        var shiftX = (_a = this.xCoordShift) !== null && _a !== void 0 ? _a : 0;
        var shiftY = (_b = this.yCoordShift) !== null && _b !== void 0 ? _b : 0;
        var _c = this.getSvgDomRect(), width = _c.width, height = _c.height;
        if (this.horizontalAnchorPointProperty === AnchorPoint_1.EHorizontalAnchorPoint.Center) {
            shiftX -= width / 2;
        }
        else if (this.horizontalAnchorPointProperty === AnchorPoint_1.EHorizontalAnchorPoint.Right) {
            shiftX -= width;
        }
        if (this.verticalAnchorPointProperty === AnchorPoint_1.EVerticalAnchorPoint.Center) {
            shiftY -= height / 2;
        }
        else if (this.verticalAnchorPointProperty === AnchorPoint_1.EVerticalAnchorPoint.Bottom) {
            shiftY -= height;
        }
        this.svg.style.visibility = this.isHidden ? "hidden" : "visible";
        this.svg.style.opacity = this.opacity.toString();
        var _d = this.convertPolarToCartesian(this.getX1Coordinate(xCalc, yCalc), this.getY1Coordinate(xCalc, yCalc), true), x = _d.x, y = _d.y;
        var x1Coord = shiftX + x + xCoordSvgTrans;
        var y1Coord = shiftY + y + yCoordSvgTrans;
        if (isNaN(x1Coord) || isNaN(y1Coord) || !isFinite(x1Coord) || !isFinite(y1Coord)) {
            this.svg.style.display = "none";
        }
        else {
            this.setSvgAttribute("x", x1Coord);
            this.setSvgAttribute("y", y1Coord);
        }
    };
    /** @inheritDoc */
    SvgAnnotationBase.prototype.delete = function () {
        this.clear();
        _super.prototype.delete.call(this);
    };
    Object.defineProperty(SvgAnnotationBase.prototype, "svg", {
        get: function () {
            return this.svgProperty;
        },
        enumerable: false,
        configurable: true
    });
    SvgAnnotationBase.prototype.getSvgDomRect = function () {
        var _a, _b, _c;
        if (!this.svgDOMRect) {
            // getBBox has issue measuring the inner content of SVG on Firefox SCJS-1936,
            // thus here we try to measure the contents, e.g. background element if it exists
            // @ts-ignore property doesn't exist warning
            this.svgDOMRect = (_c = (_b = (_a = this.svg.firstChild) === null || _a === void 0 ? void 0 : _a.getBBox) === null || _b === void 0 ? void 0 : _b.call(_a)) !== null && _c !== void 0 ? _c : this.svg.getBBox();
        }
        return this.svgDOMRect;
    };
    SvgAnnotationBase.prototype.clear = function () {
        if (!this.parentSurface || this.parentSurface.isDeleted || !this.svg)
            return;
        this.nextSibling = this.svg.nextElementSibling;
        this.svg.parentNode.removeChild(this.svg);
        this.svgDOMRect = undefined;
        this.setSvg(undefined);
    };
    /**
     * Called to create the SVG Dom Element right before it is added to the parent chart
     * @param xCalc The XAxis {@link CoordinateCalculatorBase | CoordinateCalculator} applied to this annotation
     * @param yCalc The YAxis {@link CoordinateCalculatorBase | CoordinateCalculator} applied to this annotation
     * @param xCoordSvgTrans X-coordinate translation which is needed to use SVG canvas having the whole chart size
     * @param yCoordSvgTrans Y-coordinate translation which is needed to use SVG canvas having the whole chart size
     */
    SvgAnnotationBase.prototype.create = function (xCalc, yCalc, xCoordSvgTrans, yCoordSvgTrans) {
        this.xCoordSvgTransProperty = xCoordSvgTrans;
        this.yCoordSvgTransProperty = yCoordSvgTrans;
    };
    Object.defineProperty(SvgAnnotationBase.prototype, "svgRoot", {
        /**
         * Gets the {@link SVGSVGElement | SVG Element} at the root of this annotation
         */
        get: function () {
            return this.svgRootProperty;
        },
        enumerable: false,
        configurable: true
    });
    SvgAnnotationBase.prototype.selectLayerRoot = function () {
        if (this.annotationLayer === EAnnotationLayer_1.EAnnotationLayer.AboveChart) {
            this.svgRootProperty = this.parentSurface.domSvgContainer;
        }
        else if (this.annotationLayer === EAnnotationLayer_1.EAnnotationLayer.BelowChart) {
            // default to foreground for back compatability
            this.svgRootProperty = this.parentSurface.domSvgContainer;
        }
        else if (this.annotationLayer === EAnnotationLayer_1.EAnnotationLayer.Background) {
            this.svgRootProperty = this.parentSurface.domBackgroundSvgContainer;
        }
        else {
            throw new Error("Unexpected annotationLayer value: \"".concat(this.annotationLayer, "!\""));
        }
    };
    SvgAnnotationBase.prototype.setSvgAttribute = function (attributeName, value) {
        var strValue = value.toString(10);
        this.svg.firstElementChild.setAttribute(attributeName, strValue);
    };
    SvgAnnotationBase.prototype.setSvg = function (svg) {
        this.svgProperty = svg;
    };
    SvgAnnotationBase.prototype.applyClipping = function (svgString, clipping) {
        return this.applySvgClipping(svgString, clipping);
    };
    SvgAnnotationBase.prototype.notifyPropertyChanged = function (propertyName) {
        var _a;
        if (propertyName === constants_1.PROPERTY.ANNOTATION_CANVAS && !((_a = this.svgRootProperty) === null || _a === void 0 ? void 0 : _a.isConnected)) {
            this.clear();
            this.nextSibling = undefined;
        }
        _super.prototype.notifyPropertyChanged.call(this, propertyName);
    };
    SvgAnnotationBase.prototype.getSize = function () {
        if (!this.svg)
            return undefined;
        var _a = this.getSvgDomRect(), width = _a.width, height = _a.height;
        return { width: width, height: height };
    };
    return SvgAnnotationBase;
}(DomAnnotationBase_1.DomAnnotationBase));
exports.SvgAnnotationBase = SvgAnnotationBase;
