var GWJS;
(function (GWJS) {
    var QRLogin;
    (function (QRLogin) {
        defaultModule.directive('sbLoad', ['$parse', function ($parse) {
                return {
                    restrict: 'A',
                    link: function (scope, elem, attrs) {
                        var fn = $parse(attrs.sbLoad);
                        elem.on('load', function (event) {
                            scope.$apply(function () {
                                fn(scope, { $event: event });
                            });
                        });
                    }
                };
            }]);
        var ViewModel = /** @class */ (function () {
            function ViewModel(resources, lang) {
                this.Seconds = 0;
                this.ModalName = "qrLogin";
                this.Resources = resources;
                if (location.host == "teolokullari.k12net.com") {
                    this.PlayStoreLink = 'https://play.google.com/store/apps/details?id=com.idea.teolk12&hl=tr_TR';
                    this.AppleStoreLink = 'https://apps.apple.com/tr/app/teol/id1488101253?l=tr';
                }
                else if (location.host == "bogazicikoleji.k12net.com") {
                    this.PlayStoreLink = 'https://play.google.com/store/apps/details?id=com.bogazicikoleji.bogazicikolejimobil&gl=TR';
                    this.AppleStoreLink = 'https://apps.apple.com/tr/app/bo%C4%9Fazi%C3%A7i-koleji/id1599138638?l=tr#?platform=iphone';
                }
                else {
                    this.PlayStoreLink = 'https://play.google.com/store/apps/details?id=com.k12nt.k12netframe&hl=' + lang;
                    this.AppleStoreLink = 'https://itunes.apple.com/tr/app/k12net-mobil/id1155767502?l=' + lang + '&mt=8';
                }
            }
            ViewModel.prototype.Show = function () {
                var _this = this;
                $("#" + this.ModalName).on('hide.bs.modal', function () {
                    _this.StopTimer();
                    $("#" + _this.ModalName).off('hide.bs.modal');
                });
                $("#" + this.ModalName).modal("show");
            };
            ViewModel.prototype.Hide = function () {
                $("#" + this.ModalName).modal("hide");
            };
            ViewModel.prototype.Load = function () {
                this.StopTimer();
                this.IsBusy = true;
                this.QRImagePath = this.Controller.DomainContext.ApiAddress + "/api/Login/QRCode/Generate?r=" + Math.random();
            };
            ViewModel.prototype.QRImageLoaded = function () {
                var _this = this;
                this.IsBusy = false;
                this.Seconds = 120;
                this.CheckCode();
                this.Countdown = this.$interval(function () { return _this.Seconds--; }, 1000);
            };
            ViewModel.prototype.CheckCode = function () {
                var _this = this;
                if (this.Seconds <= 2)
                    return;
                this.Controller.Login().then(function (result) {
                    if (result.SingleResult == "OK") {
                        _this.StopTimer();
                        window.location.href = '/Default.aspx';
                    }
                    else if (result.SingleResult == "LOGGED_IN") {
                        _this.StopTimer();
                        window.location.href = '/Default.aspx';
                    }
                    else if (result.SingleResult == "QR_EXPIRED") {
                        _this.StopTimer();
                        _this.ErrorMessage == _this.Resources.QRErrorExpired;
                    }
                    else if (result.SingleResult == "NOT_SCANNED" && _this.Countdown) {
                        setTimeout(function () {
                            _this.CheckCode();
                        }, 1000);
                    }
                }).catch(function (error) {
                    _this.StopTimer();
                    _this.ErrorMessage = error;
                });
            };
            Object.defineProperty(ViewModel.prototype, "RemainingTime", {
                get: function () {
                    if (this.Seconds <= 0)
                        return "0";
                    var mins = Math.floor(this.Seconds / 60);
                    var secs = this.Seconds - (mins * 60);
                    return mins.toString().padStart(2, '0') + ":" + secs.toString().padStart(2, '0');
                },
                enumerable: false,
                configurable: true
            });
            ViewModel.prototype.StopTimer = function () {
                this.$interval.cancel(this.Countdown);
                this.Countdown = null;
            };
            return ViewModel;
        }());
        QRLogin.ViewModel = ViewModel;
    })(QRLogin = GWJS.QRLogin || (GWJS.QRLogin = {}));
})(GWJS || (GWJS = {}));
//# sourceMappingURL=qr-login.js.map