/*
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 { TextMetricsProviderBase } from "./TextMetricsProviderBase";
import { List$1 } from "igniteui-angular-core";
import { WorksheetCellFormatData } from "./WorksheetCellFormatData";
import { CellFormatCache } from "./CellFormatCache";
import { MathUtilities } from "./MathUtilities";
import { Size } from "igniteui-angular-core";
import { toNullable, markType } from "igniteui-angular-core";
import { intDivide } from "igniteui-angular-core";
import { nullableEquals } from "igniteui-angular-core";
import { stringReplace } from "igniteui-angular-core";
/**
 * @hidden
 */
export class DomTextMetricsProvider extends TextMetricsProviderBase {
    constructor(a, b) {
        super(a);
        this._i = null;
        this._h = new List$1(WorksheetCellFormatData.$, 0);
        this._k = null;
        this._j = null;
        this._i = b;
    }
    dispose() {
        super.dispose();
        if (this._k != null) {
            CellFormatCache.h(this._k);
            this._k = null;
        }
        if (this._j != null) {
            CellFormatCache.h(this._j);
            this._j = null;
        }
        if (this._h != null) {
            for (let a = this._h.count - 1; a >= 0; a--) {
                this._h._inner[a]._as.g();
            }
            this._h.clear();
        }
    }
    _n() {
        if (this._k == null) {
            let a = this._i.createElement("div");
            a.setStyleProperty("position", "absolute");
            a.setStyleProperty("display", "block");
            a.setStyleProperty("visibility", "hidden");
            a.setStyleProperty("overflow", "hidden");
            this._k = a;
            this._i.appendToBody(this._k);
        }
        return this._k;
    }
    _d(a) {
        if (a._as != null && a._as.c >= 0) {
            return a._as.c;
        }
        let b = this._j == null;
        if (b) {
            this._j = this._m();
        }
        this._j.setStyleProperty("font-family", a._df);
        this._j.setStyleProperty("font-size", ((intDivide(a._cq, 20)) + "pt"));
        if (b) {
            this._j.setText("0");
            this._n().append(this._j);
        }
        let c = this._j.height();
        ;
        if (a._as == null) {
            new CellFormatCache(a);
            this._h.add(a);
        }
        c = Math.round(c * 1.085);
        a._as.c = c;
        return c;
    }
    _e(a) {
        let b = this._d(a);
        return MathUtilities.f(Math.max(1, 0.05 * b)) * 3;
    }
    _g(a, b, c = toNullable(Size.$, null), d = true, e = false) {
        if (e) {
            b = stringReplace(stringReplace(stringReplace(b, "\r\n", ""), "\r", ""), "\n", "");
        }
        else if (b != null && b.length > 0 && b.charCodeAt(b.length - 1) == 10) {
            b += " ";
        }
        if (a._as == null) {
            new CellFormatCache(a);
            this._h.add(a);
        }
        if (Size.l_op_Equality_Lifted(c, toNullable(Size.$, null)) && b.indexOf('\n') < 0) {
            let f = a._as.e;
            if (f == null) {
                let g = this._i.createElement("canvas").getNativeElement();
                f = g.getContext("2d");
                a._as.d = g;
                a._as.e = f;
                let h = "";
                if (nullableEquals(a._c3, true)) {
                    h += "italic ";
                }
                if (nullableEquals(a._c2, true)) {
                    h += "bold ";
                }
                h += (intDivide(a._cq, 20)) + "pt \"" + a._df + "\"";
                f.font = h;
            }
            if (!a._as.b) {
                let i = Math.round(a._as.e.measureText(b).width);
                let j = this._d(a);
                return new Size(1, i, j);
            }
        }
        let k = false;
        let l = a._as.f;
        if (l == null) {
            a._as.f = l = this._l(a);
            k = true;
        }
        l.setText(b);
        l.setStyleProperty("max-width", Size.l_op_Inequality_Lifted(c, toNullable(Size.$, null)) ? c.value.width + "px" : "none");
        if (k) {
            this._n().append(l);
        }
        let m = l.width();
        let n = l.height();
        return new Size(1, m, n);
    }
    _m() {
        let a = this._i.createElement("span");
        a.setStyleProperty("border", "0px none");
        a.setStyleProperty("margin", "0px");
        a.setStyleProperty("padding", "0px");
        a.setStyleProperty("line-height", "normal");
        a.setStyleProperty("position", "relative");
        a.setStyleProperty("display", "block");
        a.setStyleProperty("visibility", "hidden");
        a.setStyleProperty("white-space", "pre");
        a.setStyleProperty("text-decoration", "none");
        a.setStyleProperty("font-weight", "normal");
        a.setStyleProperty("font-style", "normal");
        a.setStyleProperty("max-width", "none");
        return a;
    }
    _l(a) {
        let b = this._i.createElement("span");
        let c = a._b8;
        b.setStyleProperty("border", "0px none");
        b.setStyleProperty("margin", "0px");
        b.setStyleProperty("padding", "0px");
        b.setStyleProperty("line-height", "normal");
        b.setStyleProperty("position", "relative");
        b.setStyleProperty("display", "block");
        b.setStyleProperty("visibility", "hidden");
        b.setStyleProperty("font-family", a._df);
        b.setStyleProperty("font-size", ((intDivide(a._cq, 20)) + "pt"));
        b.setStyleProperty("white-space", (c ? "pre-wrap" : "pre"));
        b.setStyleProperty("overflow-wrap", (c ? "break-word" : "normal"));
        b.setStyleProperty("word-wrap", (c ? "break-word" : "normal"));
        b.setStyleProperty("text-decoration", (a._a1 != 0 ? "underline" : "none"));
        b.setStyleProperty("font-weight", (nullableEquals(a._c2, true) ? "bold" : "normal"));
        b.setStyleProperty("font-style", (nullableEquals(a._c3, true) ? "italic" : "normal"));
        return b;
    }
}
DomTextMetricsProvider.$t = markType(DomTextMetricsProvider, 'DomTextMetricsProvider', TextMetricsProviderBase.$);
//# sourceMappingURL=DomTextMetricsProvider.js.map