/*
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 { Base, runOn, delegateCombine, markType, INotifyPropertyChanged_$type, PropertyChangedEventArgs, Point_$type } from "./type";
import { CanvasGestureDOMEventProxy } from "./CanvasGestureDOMEventProxy";
import { Brush } from "./Brush";
import { Rect } from "./Rect";
import { truncate } from "./number";
import { ScrollerScrollingEventArgs } from "./ScrollerScrollingEventArgs";
export class ScrollerView extends Base {
    constructor(a) {
        super();
        this.b = null;
        this.w = null;
        this.aa = null;
        this.v = null;
        this.a = null;
        this.z = null;
        this.x = null;
        this.y = null;
        this.ab = null;
        this.d = false;
        this.c = null;
        this.h = false;
        this.g = false;
        this.r = 0;
        this.o = 0;
        this.s = -1;
        this.p = -1;
        this.f = false;
        this.e = false;
        this.q = 0;
        this.n = 0;
        this.b = a;
    }
    aw() {
        return ((() => {
            let $ret = new Brush();
            $ret.fill = "#666666";
            return $ret;
        })());
    }
    ap(a) {
        if (a == null) {
            if (this.w != null) {
                this.w = null;
            }
            if (this.a != null) {
                this.a.ai = null;
                this.a.an();
            }
            return;
        }
        this.w = a;
        let b = Math.round(this.w.rootWrapper.width());
        let c = Math.round(this.w.rootWrapper.height());
        this.z = this.w.rootWrapper;
        this.z.setStyleProperty("position", "relative");
        this.aa = this.w.createElement("div");
        this.aa.setStyleProperty("position", "relative");
        this.aa.setStyleProperty("width", "100%");
        this.aa.setStyleProperty("height", "100%");
        this.aa.setStyleProperty("overflow", "hidden");
        this.z.append(this.aa);
        this.y = this.w.createElement("div");
        this.y.setStyleProperty("position", "absolute");
        this.y.setStyleProperty("height", this.b.scrollbarSpan + "px");
        this.y.setStyleProperty("width", this.b.minScrollbarSize + "px");
        this.y.setStyleProperty("background-color", this.b.scrollbarBrush._fill);
        this.y.setStyleProperty("display", "none");
        this.y.setStyleProperty("border-radius", "5px");
        this.y.setStyleProperty("z-index", "10000");
        this.y.setStyleProperty("bottom", c + "px");
        this.z.append(this.y);
        this.ab = this.w.createElement("div");
        this.ab.setStyleProperty("position", "absolute");
        this.ab.setStyleProperty("width", this.b.scrollbarSpan + "px");
        this.ab.setStyleProperty("height", this.b.minScrollbarSize + "px");
        this.ab.setStyleProperty("background-color", this.b.scrollbarBrush._fill);
        this.ab.setStyleProperty("display", "none");
        this.ab.setStyleProperty("border-radius", "5px");
        this.ab.setStyleProperty("z-index", "10000");
        this.ab.setStyleProperty("right", "0px");
        this.z.append(this.ab);
        this.v = this.aa.getNativeElement();
        this.a = new CanvasGestureDOMEventProxy(this.w.rootWrapper, this.w, true);
        let d = this.a;
        d.onMouseWheel = delegateCombine(d.onMouseWheel, runOn(this, this.j));
        let e = this.a;
        e.onDragStarted = delegateCombine(e.onDragStarted, runOn(this, this.ai));
        let f = this.a;
        f.onDragDelta = delegateCombine(f.onDragDelta, runOn(this, this.ah));
        let g = this.a;
        g.onDragCompleted = delegateCombine(g.onDragCompleted, runOn(this, this.ag));
        let h = this.a;
        h.onFlingStarted = delegateCombine(h.onFlingStarted, runOn(this, this.i));
        let i = this.a;
        i.onContactStarted = delegateCombine(i.onContactStarted, runOn(this, this.af));
        let j = this.a;
        j.onContactMoved = delegateCombine(j.onContactMoved, runOn(this, this.ae));
        let k = this.a;
        k.onContactCompleted = delegateCombine(k.onContactCompleted, runOn(this, this.ad));
        this.a.z = true;
        this.a.ak = runOn(this, this.m);
        this.b.aw(b, c);
    }
    m(a, b, c) {
        if (this.e || this.f) {
            return true;
        }
        if (c || b) {
            return true;
        }
        if (this.k(a, b)) {
            return true;
        }
        if (this.l(a, b)) {
            return true;
        }
        return false;
    }
    ad(a, b) {
        this.e = false;
        this.f = false;
    }
    ae(a, b) {
        if (this.f) {
            let c = a.y - this.q;
            this.q = a.y;
            this.b.av(c);
        }
        else if (this.e) {
            let d = a.x - this.n;
            this.n = a.x;
            this.b.at(d);
        }
    }
    af(a, b) {
        this.d = false;
        this.ac();
        let c = this.l(a, b);
        let d = this.k(a, b);
        if (c) {
            this.f = true;
            this.q = a.y;
        }
        else if (d) {
            this.e = true;
            this.n = a.x;
        }
    }
    k(a, b) {
        if (!this.g) {
            return false;
        }
        if (a.y >= this.b.viewportHeight - this.b.scrollbarSpan && a.y <= this.b.viewportHeight && a.x >= this.p - this.o / 2 && a.x <= this.p + this.o / 2) {
            return true;
        }
        return false;
    }
    l(a, b) {
        if (!this.h) {
            return false;
        }
        if (a.x >= this.b.viewportWidth - this.b.scrollbarSpan && a.x <= this.b.viewportWidth && a.y >= this.s - this.r / 2 && a.y <= this.s + this.r / 2) {
            return true;
        }
        return false;
    }
    i(a, b, c) {
        this.d = true;
        return true;
    }
    an(a) {
        this.a.bc = new Rect(0, 0, 0, this.b.viewportWidth, this.b.viewportHeight);
    }
    ao(a) {
        this.a.bc = new Rect(0, 0, 0, this.b.viewportWidth, this.b.viewportHeight);
    }
    al(a) {
        if (this.x != null) {
            this.x.setStyleProperty("height", a + "px");
        }
    }
    am(a) {
        if (this.x != null) {
            this.x.setStyleProperty("width", a + "px");
        }
    }
    ac() {
    }
    ag(a) {
        if (this.e || this.f) {
            return;
        }
        this.d = false;
        this.b.aq(a);
    }
    ah(a) {
        if (this.e || this.f) {
            return;
        }
        this.b.ar(a);
    }
    au(a) {
        this.v.scrollTop = truncate(a);
    }
    ai(a) {
        if (this.e || this.f) {
            return;
        }
        this.d = false;
        this.b.as(a);
    }
    t() {
        return this.v.scrollLeft;
    }
    u() {
        return this.v.scrollTop;
    }
    j(a, b) {
        this.d = false;
        this.ac();
        return this.b.l(a, b);
    }
    at(a) {
        this.v.scrollLeft = truncate(a);
    }
    aq(a) {
        this.x = a;
        this.aa.append(this.x);
    }
    ar(a) {
        if (this.w == null) {
            return;
        }
        if (this.c == null) {
            this.c = this.w.getRequestAnimationFrame();
        }
        if (this.d) {
            a();
        }
        else {
            this.c(() => a());
        }
    }
    ak() {
        if (this.h) {
            this.h = false;
            this.ab.setStyleProperty("display", "none");
        }
    }
    aj() {
        if (this.g) {
            this.g = false;
            this.y.setStyleProperty("display", "none");
        }
    }
    av(a, b) {
        let c = false;
        if (!this.h) {
            c = true;
            this.h = true;
            this.ab.setStyleProperty("display", "block");
        }
        if (a != this.s || c) {
            this.s = a;
            if (this.h) {
                this.ab.setRawYPosition(a - b / 2);
            }
        }
        if (b != this.r || c) {
            this.r = b;
            if (this.h) {
                this.ab.setRawSize(this.b.scrollbarSpan, b);
            }
        }
    }
    as(a, b) {
        let c = false;
        if (!this.g) {
            c = true;
            this.g = true;
            this.y.setStyleProperty("display", "block");
        }
        if (a != this.p || c) {
            this.p = a;
            if (this.g) {
                this.y.setRawXPosition(a - b / 2);
            }
        }
        if (b != this.o || c) {
            this.o = b;
            if (this.g) {
                this.y.setRawSize(b, this.b.scrollbarSpan);
            }
        }
    }
}
ScrollerView.$t = markType(ScrollerView, 'ScrollerView');
export class Scroller extends Base {
    constructor() {
        super();
        this.n = 0;
        this.o = 20;
        this.r = 8;
        this.u = 0;
        this.a6 = null;
        this.m = 0;
        this.w = 0;
        this.v = 0;
        this.t = 0;
        this.s = 0;
        this.b = null;
        this.h = true;
        this.j = false;
        this.g = false;
        this.e = false;
        this.f = false;
        this.a8 = null;
        this.d = false;
        this.propertyChanged = null;
        this.onScrolling = null;
        this.p = 0;
        this.q = 0;
        this.i = false;
        this.a6 = this.view.aw();
    }
    get contentWidth() {
        return this.n;
    }
    set contentWidth(a) {
        let b = this.n;
        this.n = a;
        if (b != this.n) {
            this.au("ContentWidth", b, this.n);
        }
    }
    get minScrollbarSize() {
        return this.o;
    }
    set minScrollbarSize(a) {
        let b = this.o;
        this.o = a;
        if (b != this.o) {
            this.au("MinScrollbarSize", b, this.o);
        }
    }
    get scrollbarSpan() {
        return this.r;
    }
    set scrollbarSpan(a) {
        let b = this.r;
        this.r = a;
        if (b != this.r) {
            this.au("ScrollbarSpan", b, this.r);
        }
    }
    get verticalTrackStartInset() {
        return this.u;
    }
    set verticalTrackStartInset(a) {
        let b = this.u;
        this.u = a;
        if (b != this.u) {
            this.au("VerticalTrackStartInset", b, this.u);
        }
    }
    get scrollbarBrush() {
        return this.a6;
    }
    set scrollbarBrush(a) {
        let b = this.a6;
        this.a6 = a;
        if (b != this.a6) {
            this.au("ScrollbarBrush", b, this.a6);
        }
    }
    au(a, b, c) {
        if (this.propertyChanged != null) {
            this.propertyChanged(this, new PropertyChangedEventArgs(a));
        }
        this.ax(a, b, c);
    }
    ax(a, b, c) {
        switch (a) {
            case "ContentWidth":
                this.view.am(this.contentWidth);
                this.a3();
                break;
            case "ContentHeight":
                this.view.al(this.contentHeight);
                this.a5();
                break;
            case "ScrollTop":
                this.view.au(this.scrollTop);
                break;
            case "ScrollLeft":
                this.view.at(this.scrollLeft);
                break;
            case "ViewportWidth":
                this.view.ao(this.viewportWidth);
                break;
            case "ViewportHeight":
                this.view.an(this.viewportHeight);
                break;
            case "ScrollbarBrush":
            case "ScrollbarSpan":
            case "MinScrollbarSize":
            case "VerticalTrackStartInset":
                this.a5();
                this.a3();
                break;
        }
    }
    get contentHeight() {
        return this.m;
    }
    set contentHeight(a) {
        let b = this.m;
        this.m = a;
        if (b != this.m) {
            this.au("ContentHeight", b, this.m);
        }
    }
    av(a) {
        let b = this.verticalTrackStartInset + (this.minScrollbarSize / 2);
        let c = this.viewportHeight - (this.minScrollbarSize / 2);
        let d = a / (c - b);
        let e = d * (this.contentHeight - this.viewportHeight);
        this.a1(0, e);
    }
    at(a) {
        let b = 0 + (this.minScrollbarSize / 2);
        let c = this.viewportWidth - (this.minScrollbarSize / 2);
        let d = a / (c - b);
        let e = d * (this.contentWidth - this.viewportWidth);
        this.a1(e, 0);
    }
    aw(a, b) {
        this.w = a;
        this.v = b;
    }
    get viewportWidth() {
        return this.w;
    }
    set viewportWidth(a) {
        let b = this.w;
        this.w = a;
        if (b != this.w) {
            this.au("ViewportWidth", b, this.w);
        }
    }
    get viewportHeight() {
        return this.v;
    }
    set viewportHeight(a) {
        let b = this.v;
        this.v = a;
        if (b != this.v) {
            this.au("ViewportHeight", b, this.v);
        }
    }
    get scrollTop() {
        return this.t;
    }
    set scrollTop(a) {
        let b = this.t;
        this.t = a;
        if (b != this.t) {
            this.au("ScrollTop", b, this.t);
        }
    }
    get scrollLeft() {
        return this.s;
    }
    set scrollLeft(a) {
        let b = this.s;
        this.s = a;
        if (b != this.s) {
            this.au("ScrollLeft", b, this.s);
        }
    }
    get view() {
        if (this.b == null) {
            this.b = new ScrollerView(this);
        }
        return this.b;
    }
    provideContainer(a) {
        this.view.ap(a);
        this.t = this.view.u();
        this.s = this.view.t();
        this.a3();
        this.a5();
    }
    provideContent(a) {
        this.view.aq(a);
    }
    l(a, b) {
        this.f = false;
        this.e = false;
        this.g = false;
        this.view.ac();
        this.a1(0, b);
        return true;
    }
    get lockScrollDirection() {
        return this.h;
    }
    set lockScrollDirection(a) {
        this.h = true;
    }
    as(a) {
        this.g = false;
        this.e = false;
        this.f = false;
        this.d = true;
        this.a8 = { $type: Point_$type, x: a.x, y: a.y };
    }
    ar(a) {
        if (this.d) {
            let b = a.y - this.a8.y;
            let c = a.x - this.a8.x;
            if (this.lockScrollDirection && !this.f) {
                this.f = true;
                if (b > 0 || c > 0) {
                    if (b > c) {
                        this.g = true;
                    }
                    else {
                        this.g = true;
                    }
                }
            }
            this.a8 = { $type: Point_$type, x: a.x, y: a.y };
            this.a1(-c, -b);
        }
    }
    a1(a, b) {
        this.p += a;
        this.q += b;
        this.a0();
    }
    a0() {
        if (this.i) {
            return;
        }
        this.i = true;
        this.view.ar(runOn(this, this.a2));
    }
    a2() {
        this.i = false;
        let a = this.p;
        let b = this.q;
        this.p = 0;
        this.q = 0;
        if (this.f) {
            if (this.g) {
                a = 0;
            }
            if (this.e) {
                b = 0;
            }
        }
        let c = Math.round(this.scrollTop + b);
        let d = Math.round(this.scrollLeft + a);
        if (c < 0) {
            c = 0;
            this.view.ac();
        }
        if (d < 0) {
            d = 0;
            this.view.ac();
        }
        if (c + this.viewportHeight > this.contentHeight) {
            c = this.contentHeight - this.viewportHeight;
            if (c < 0) {
                c = 0;
            }
            this.view.ac();
        }
        if (d + this.viewportWidth > this.contentWidth) {
            d = this.contentWidth - this.viewportWidth;
            if (d < 0) {
                d = 0;
            }
            this.view.ac();
        }
        b = c - this.scrollTop;
        a = d - this.scrollLeft;
        if (b != 0 || a != 0) {
            this.j = true;
            this.scrollTop = c;
            this.scrollLeft = d;
            this.a5();
            this.a3();
            this.j = false;
            if (this.onScrolling != null) {
                this.onScrolling(this, ((() => {
                    let $ret = new ScrollerScrollingEventArgs();
                    $ret.deltaX = a;
                    $ret.deltaY = b;
                    return $ret;
                })()));
            }
        }
    }
    a5() {
        this.a4(this.contentHeight, this.viewportHeight, this.scrollTop, true);
    }
    a3() {
        this.a4(this.contentWidth, this.viewportWidth, this.scrollLeft, false);
    }
    a4(a, b, c, d) {
        let e = Math.round((b / a) * b);
        if (e < this.minScrollbarSize) {
            e = this.minScrollbarSize;
        }
        if (e >= b) {
            if (d) {
                this.view.ak();
            }
            else {
                this.view.aj();
            }
            return;
        }
        let f = d ? this.verticalTrackStartInset : 0;
        let g = f + (e / 2);
        let h = b - (e / 2);
        let i = c / (a - b);
        let j = Math.round(g + (h - g) * i);
        if (d) {
            this.view.av(j, e);
        }
        else {
            this.view.as(j, e);
        }
    }
    aq(a) {
        this.f = false;
        this.e = false;
        this.g = false;
        this.d = false;
    }
}
Scroller.$t = markType(Scroller, 'Scroller', Base.$, [INotifyPropertyChanged_$type]);
//# sourceMappingURL=ScrollerView_combined.js.map