/*
THIS INFRAGISTICS ULTIMATE SOFTWARE LICENSE  AGREEMENT ("AGREEMENT") LOCATED HERE:
https://www.infragistics.com/legal/license/igultimate-la
https://www.infragistics.com/legal/license/igultimate-eula
GOVERNS THE LICENSING, INSTALLATION AND USE OF INFRAGISTICS SOFTWARE. BY DOWNLOADING AND/OR INSTALLING AND USING INFRAGISTICS SOFTWARE:  you are indicating that you have read and understand this Agreement, and agree to be legally bound by it on behalf of the yourself and your company.
*/
import { __extends } from "tslib";
import { Base, typeCast, Point_$type, markType, PointUtil } from "igniteui-angular-core";
import { Extensions } from "igniteui-angular-core";
import { Brush } from "igniteui-angular-core";
import { Color } from "igniteui-angular-core";
import { BrushUtil } from "igniteui-angular-core";
import { Defaults } from "./Defaults";
import { MathUtil } from "igniteui-angular-core";
import { Size } from "igniteui-angular-core";
import { RotateTransform } from "igniteui-angular-core";
import { TransformGroup } from "igniteui-angular-core";
import { BrushUtilCore } from "igniteui-angular-core";
import { CoreGeometryUtil } from "igniteui-angular-core";
import { FontInfo } from "igniteui-angular-core";
import { Rect } from "igniteui-angular-core";
import { RectUtil } from "igniteui-angular-core";
import { stringIsNullOrEmpty } from "igniteui-angular-core";
import { isNaN_ } from "igniteui-angular-core";
/**
 * @hidden
 */
var OverlayTextUtil = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(OverlayTextUtil, _super);
    function OverlayTextUtil() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    OverlayTextUtil.ac = function (a) {
        var b = stringIsNullOrEmpty(a.textContent);
        var c = OverlayTextUtil.o(a.f, a.textVisible);
        var d = a.textLocation;
        if (d == 18) {
            c = false;
        }
        if (!c || b) {
            return;
        }
        if (!OverlayTextUtil.n(a.ad)) {
            return;
        }
        a.aa = OverlayTextUtil.ak(a.textLocation, a.ad, a.textAngle, a.horizontalMargin, a.verticalMargin, a.horizontalPadding, a.verticalPadding, a.ab, !a.g);
        if (!Extensions.a(a.aa)) {
            return;
        }
        OverlayTextUtil.y(a.ac, a.background, a.borderStroke, a.borderThickness, a.borderRadius);
        OverlayTextUtil.z(a.u, a.textColor);
        OverlayTextUtil.x(a);
        OverlayTextUtil.w(a);
    };
    OverlayTextUtil.ae = function (a) {
        var b = typeCast(Brush.$, a);
        if (b != null) {
            var c = b.color.l < 100;
            if (c) {
                return ((function () {
                    var $ret = new Brush();
                    $ret.color = OverlayTextUtil.aj;
                    return $ret;
                })());
            }
        }
        return BrushUtil.k(a, OverlayTextUtil.ai, OverlayTextUtil.aj);
    };
    OverlayTextUtil.af = function (a, b, c, d) {
        if (b == null) {
            return OverlayTextUtil.ae(a);
        }
        else {
            return OverlayTextUtil.ah(b, c, d);
        }
    };
    OverlayTextUtil.ad = function (a) {
        if (isNaN_(a.verticalPadding)) {
            a.verticalPadding = Defaults.dataAnnotationOverlayHorizontalPadding;
        }
        if (isNaN_(a.horizontalPadding)) {
            a.horizontalPadding = Defaults.dataAnnotationOverlayVerticalPadding;
        }
        if (a.borderRadius < 0) {
            a.borderRadius = 0;
        }
        if (a.verticalPadding < 0) {
            a.verticalPadding = 0;
        }
        if (a.horizontalPadding < 0) {
            a.horizontalPadding = 0;
        }
        a.background = OverlayTextUtil.ag(a.background, Defaults.dataAnnotationOverlayBackground);
        a.background = OverlayTextUtil.ah(a.background, a.backgroundShift, a.backgroundMode);
        a.borderStroke = OverlayTextUtil.ag(a.borderStroke, Defaults.dataAnnotationOverlayBorderStroke);
        a.borderStroke = OverlayTextUtil.ah(a.borderStroke, a.borderShift, a.borderMode);
        a.textColor = OverlayTextUtil.af(a.background, a.textColor, a.textColorShift, a.textColorMode);
        a.textEmpty = stringIsNullOrEmpty(a.textContent);
        a.textAngle = OverlayTextUtil.r(a.textAngle, !a.f);
        a.borderRadius = OverlayTextUtil.t(a.borderRadius, Defaults.dataAnnotationOverlayBorderCorner);
        a.borderThickness = OverlayTextUtil.t(a.borderThickness, Defaults.dataAnnotationOverlayBorderThickness);
    };
    OverlayTextUtil.ah = function (a, b, c) {
        if (a != null && !isNaN_(b) && b != 0) {
            if (c == 0 || c == 1) {
                b = MathUtil.d(b, -1, 1);
                if (b < 0) {
                    b *= -1;
                }
                return BrushUtil.o(a, b);
            }
            else if (c == 2) {
                return BrushUtil.q(a, b);
            }
            else if (c == 4 || c == 3) {
                b = MathUtil.d(b, -1, 1);
                if (b < 0) {
                    b *= -1;
                }
                return BrushUtil.s(a, b);
            }
        }
        return a;
    };
    OverlayTextUtil.w = function (a) {
        if (!OverlayTextUtil.n(a.ad)) {
            return;
        }
        if (!Extensions.a(a.aa)) {
            return;
        }
        var b = new Size(1, a.ad.width, a.ad.height);
        b.width += a.horizontalPadding * 2;
        b.height += a.verticalPadding * 2;
        b.width += a.borderThickness * 2;
        b.height += a.borderThickness * 2;
        var c = PointUtil.create();
        c.x = a.aa.x - a.horizontalPadding;
        c.y = a.aa.y - a.verticalPadding;
        c.x -= a.borderThickness;
        c.y -= a.borderThickness;
        a.ac.width = b.width;
        a.ac.height = b.height;
        OverlayTextUtil.aa(a.ac, c);
        OverlayTextUtil.ab(a.ac, a.textAngle, b);
    };
    OverlayTextUtil.x = function (a) {
        if (a == null) {
            return;
        }
        if (!OverlayTextUtil.n(a.ad)) {
            return;
        }
        if (!Extensions.a(a.aa)) {
            return;
        }
        OverlayTextUtil.aa(a.u, a.aa);
        OverlayTextUtil.ab(a.u, a.textAngle, a.ad);
        a.u.al = a.textContent;
    };
    OverlayTextUtil.aa = function (a, b) {
        if (Extensions.a(b)) {
            a.n = b.x;
            a.o = b.y;
        }
    };
    OverlayTextUtil.ab = function (a, b, c) {
        var d = Math.abs(b - 0) > 1E-06;
        if (isNaN_(b) || b == 0 || b == 360) {
            a.j = null;
        }
        else {
            var e = ((function () {
                var $ret = new RotateTransform();
                $ret.j = b;
                $ret.k = 0.5 * c.width;
                $ret.l = 0.5 * c.height;
                return $ret;
            })());
            var f = new TransformGroup();
            f.j.add(e);
            a.j = f;
        }
    };
    OverlayTextUtil.y = function (a, b, c, d, e) {
        var f = OverlayTextUtil.p(b);
        var g = OverlayTextUtil.p(c) && d > 0;
        if (g || f) {
            a._fill = b;
            a._stroke = c;
            a.ad = d;
            a.ap = e;
            a.aq = e;
            a._visibility = 0;
        }
        else {
            a._visibility = 1;
        }
    };
    OverlayTextUtil.z = function (a, b) {
        a.ao = b;
    };
    OverlayTextUtil.p = function (a) {
        if (a == null) {
            return false;
        }
        if (a != null) {
            return !BrushUtilCore.a(a);
        }
        return false;
    };
    OverlayTextUtil.ag = function (a, b) {
        return a != null ? a : b;
    };
    OverlayTextUtil.t = function (a, b) {
        return !isNaN_(a) ? a : b;
    };
    OverlayTextUtil.r = function (a, b) {
        var c = !isNaN_(a) ? a : b ? 90 : 0;
        c = CoreGeometryUtil.a(a);
        return c;
    };
    OverlayTextUtil.s = function (a) {
        return !isNaN_(a) ? a : 1;
    };
    OverlayTextUtil.k = function (a) {
        return isNaN_(a.f) && stringIsNullOrEmpty(a.q) && !stringIsNullOrEmpty(a.fontString) && a.fontString != "NaNpx/normal";
    };
    OverlayTextUtil.o = function (a, b) {
        if (!b) {
            return false;
        }
        if (a != null) {
            return a.f > 0;
        }
        return false;
    };
    OverlayTextUtil.n = function (a) {
        if (isNaN_(a.width) || a.width <= 0) {
            return false;
        }
        if (isNaN_(a.height) || a.height <= 0) {
            return false;
        }
        return true;
    };
    OverlayTextUtil.m = function (a) {
        if (isNaN_(a.x)) {
            return false;
        }
        if (isNaN_(a.y)) {
            return false;
        }
        return true;
    };
    OverlayTextUtil.q = function (a) {
        if (a == null) {
            return true;
        }
        if (a.q == null) {
            return isNaN_(a.f);
        }
        return false;
    };
    OverlayTextUtil.ak = function (a, b, c, d, e, f, g, h, i) {
        var j = { $type: Point_$type, x: NaN, y: NaN };
        if (!OverlayTextUtil.m(h)) {
            return j;
        }
        if (!OverlayTextUtil.n(b)) {
            return j;
        }
        a = OverlayTextUtil.a(a, h, i);
        if (a == 18) {
            return j;
        }
        var k = h.x + h.width;
        var l = h.y + h.height;
        var m = h.x;
        var n = h.y;
        var o = h.width;
        var p = h.height;
        if (isNaN_(e)) {
            e = OverlayTextUtil.g(a) ? 0 : Defaults.dataAnnotationOverlayVerticalMargin;
        }
        if (isNaN_(d)) {
            d = OverlayTextUtil.e(a) ? 0 : Defaults.dataAnnotationOverlayHorizontalMargin;
        }
        if (isNaN_(g)) {
            g = Defaults.dataAnnotationOverlayVerticalPadding;
        }
        if (isNaN_(f)) {
            f = Defaults.dataAnnotationOverlayHorizontalPadding;
        }
        var q = new Rect(0, 0, 0, b.width, b.height);
        q.width += (f * 2);
        q.height += (g * 2);
        q.x -= f;
        q.y -= g;
        if (a == 1) {
            j.y = n + (p / 2) - (b.height / 2);
            j.x = m + (o / 2) - (b.width / 2);
        }
        else if (a == 2) {
            j.y = n + (p / 2) - (b.height / 2);
            j.x = m;
        }
        else if (a == 3) {
            j.y = n + (p / 2) - (b.height / 2);
            j.x = k - b.width;
        }
        else if (a == 5) {
            j.y = n;
            j.x = m + (o / 2) - (b.width / 2);
        }
        else if (a == 4) {
            j.y = n;
            j.x = m;
        }
        else if (a == 6) {
            j.y = n;
            j.x = k - b.width;
        }
        else if (a == 8) {
            j.y = l - b.height;
            j.x = m + (o / 2) - (b.width / 2);
        }
        else if (a == 7) {
            j.y = l - b.height;
            j.x = m;
        }
        else if (a == 9) {
            j.y = l - b.height;
            j.x = k - b.width;
        }
        else if (a == 11) {
            j.y = n - b.height;
            j.x = m + (o / 2) - (b.width / 2);
        }
        else if (a == 10) {
            j.y = i ? n : n - b.height;
            j.x = i ? m - b.width : m;
        }
        else if (a == 12) {
            j.y = i ? n : n - b.height;
            j.x = i ? k : k - b.width;
        }
        else if (a == 16) {
            j.y = l;
            j.x = m + (o / 2) - (b.width / 2);
        }
        else if (a == 15) {
            j.y = i ? l - b.height : l;
            j.x = i ? m - b.width : m;
        }
        else if (a == 17) {
            j.y = i ? l - b.height : l;
            j.x = i ? k : k - b.width;
        }
        else if (a == 13) {
            j.y = n + (p / 2) - (b.height / 2);
            j.x = m - b.width;
        }
        else if (a == 14) {
            j.y = n + (p / 2) - (b.height / 2);
            j.x = k;
        }
        if (!isNaN_(c) && c != 0) {
            var r = RectUtil.w(q, c);
            if (a == 4 || a == 2 || a == 7) {
                j.x -= r.left;
            }
            else if (a == 6 || a == 3 || a == 9) {
                j.x += r.left;
            }
            if (a == 4 || a == 5 || a == 6) {
                j.y -= r.top;
            }
            else if (a == 7 || a == 8 || a == 9) {
                j.y += r.top;
            }
            if (i) {
                if (a == 12 || a == 14 || a == 17) {
                    j.x -= r.left;
                }
                else if (a == 10 || a == 13 || a == 15) {
                    j.x += r.left;
                }
                if (a == 16 || a == 10 || a == 12) {
                    j.y -= r.top;
                }
                else if (a == 11 || a == 15 || a == 17) {
                    j.y += r.top;
                }
            }
            else {
                if (a == 14 || a == 10 || a == 15) {
                    j.x -= r.left;
                }
                else if (a == 13 || a == 12 || a == 17) {
                    j.x += r.left;
                }
                if (a == 15 || a == 16 || a == 17) {
                    j.y -= r.top;
                }
                else if (a == 10 || a == 11 || a == 12) {
                    j.y += r.top;
                }
            }
        }
        if (OverlayTextUtil.j(a)) {
            if (OverlayTextUtil.d(a)) {
                j.y -= e;
            }
            else {
                j.y += e;
            }
            if (OverlayTextUtil.h(a)) {
                j.x -= d;
            }
            else {
                j.x += d;
            }
        }
        else {
            if (i) {
                if (OverlayTextUtil.h(a)) {
                    j.x -= d;
                }
                else {
                    j.x += d;
                }
                if (a == 15 || a == 17 || a == 11) {
                    j.y -= e;
                }
                else if (a == 10 || a == 12 || a == 13 || a == 14 || a == 16) {
                    j.y += e;
                }
            }
            else {
                if (OverlayTextUtil.h(a)) {
                    j.x += d;
                }
                else {
                    j.x -= d;
                }
                if (a == 10 || a == 12 || a == 11 || a == 13 || a == 14) {
                    j.y -= e;
                }
                else if (a == 15 || a == 17 || a == 16) {
                    j.y += e;
                }
            }
        }
        return j;
    };
    OverlayTextUtil.a = function (a, b, c) {
        if (a == 18) {
            return a;
        }
        if (b.width > 1 && b.height > 1) {
            if (a == 0) {
                return 1;
            }
            else {
                return a;
            }
        }
        else {
            if (c) {
                if (a == 0) {
                    return 14;
                }
                else if (a == 9) {
                    return 7;
                }
                else if (a == 7) {
                    return 9;
                }
                else if (a == 6) {
                    return 4;
                }
                else if (a == 4) {
                    return 6;
                }
                else if (a == 3) {
                    return 14;
                }
                else if (a == 2) {
                    return 13;
                }
                else if (a == 12) {
                    return 4;
                }
                else if (a == 10) {
                    return 6;
                }
                else if (a == 17) {
                    return 7;
                }
                else if (a == 15) {
                    return 9;
                }
                else {
                    return a;
                }
            }
            else {
                if (a == 0) {
                    return 16;
                }
                else if (a == 7) {
                    return 4;
                }
                else if (a == 4) {
                    return 7;
                }
                else if (a == 9) {
                    return 6;
                }
                else if (a == 6) {
                    return 9;
                }
                else if (a == 12) {
                    return 9;
                }
                else if (a == 10) {
                    return 7;
                }
                else if (a == 17) {
                    return 6;
                }
                else if (a == 15) {
                    return 4;
                }
                else if (a == 5) {
                    return 8;
                }
                else if (a == 8) {
                    return 5;
                }
                else {
                    return a;
                }
            }
        }
    };
    OverlayTextUtil.f = function (a) {
        return a == 4 || a == 2 || a == 7 || a == 12 || a == 14 || a == 17;
    };
    OverlayTextUtil.h = function (a) {
        return a == 10 || a == 13 || a == 15 || a == 6 || a == 3 || a == 9;
    };
    OverlayTextUtil.i = function (a) {
        return a == 4 || a == 5 || a == 6 || a == 15 || a == 16 || a == 17;
    };
    OverlayTextUtil.d = function (a) {
        return a == 10 || a == 11 || a == 12 || a == 7 || a == 8 || a == 9;
    };
    OverlayTextUtil.g = function (a) {
        return a == 13 || a == 14 || a == 2 || a == 1 || a == 3;
    };
    OverlayTextUtil.e = function (a) {
        return a == 11 || a == 16 || a == 5 || a == 1 || a == 8;
    };
    OverlayTextUtil.j = function (a) {
        return a == 4 || a == 5 || a == 6 || a == 2 || a == 1 || a == 3 || a == 7 || a == 8 || a == 9;
    };
    OverlayTextUtil.l = function (a) {
        return a == 10 || a == 11 || a == 12 || a == 13 || a == 14 || a == 15 || a == 16 || a == 17;
    };
    OverlayTextUtil.c = function () {
        if (OverlayTextUtil.b == null) {
            OverlayTextUtil.b = ((function () {
                var $ret = new FontInfo();
                $ret.q = Defaults.dataAnnotationOverlayFontFamily;
                $ret.f = Defaults.dataAnnotationOverlayFontSize;
                $ret.t = Defaults.dataAnnotationOverlayFontStyle;
                $ret.v = Defaults.dataAnnotationOverlayFontWeight;
                $ret.r = Defaults.dataAnnotationOverlayFontStretch;
                $ret.u = Defaults.dataAnnotationOverlayFontVariant;
                return $ret;
            })());
        }
        return OverlayTextUtil.b;
    };
    OverlayTextUtil.u = function (a) {
        for (var b = 0; b < a.count; b++) {
            a.item(b).al = null;
        }
        a.clear();
    };
    OverlayTextUtil.v = function (a) {
        for (var b = 0; b < a.count; b++) {
            a._inner[b].al = null;
        }
        a.clear();
    };
    OverlayTextUtil.$t = markType(OverlayTextUtil, 'OverlayTextUtil');
    OverlayTextUtil.aj = Color.u(255, 0, 0, 0);
    OverlayTextUtil.ai = Color.u(255, 255, 255, 255);
    OverlayTextUtil.b = null;
    return OverlayTextUtil;
}(Base));
export { OverlayTextUtil };
