"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OldFilePreviewer = void 0;
var aurelia_router_1 = require("aurelia-router");
var aurelia_framework_1 = require("aurelia-framework");
var aurelia_dialog_1 = require("aurelia-dialog");
var constants_1 = require("../../../constants/constants");
var config_1 = require("../../../config");
var OldFilePreviewer = exports.OldFilePreviewer = (function () {
    function OldFilePreviewer(dialogController, router, config) {
        this.dialogController = dialogController;
        this.router = router;
        this.config = config;
        this.loadTimeout = 1000;
        this.isLoading = true;
    }
    OldFilePreviewer.prototype.activate = function (model) {
        this.model = model;
        this.originalUrl = model.fileUrl;
    };
    OldFilePreviewer.prototype.attached = function () {
        var _this = this;
        if (this.iframeRef != null) {
            this.iframeRef.onload = function () {
                _this.markIframeLoaded();
            };
            this.loadIframe();
        }
        this.fileName = this.model.fileUrl.split('/').last();
    };
    OldFilePreviewer.prototype.detached = function () {
        clearTimeout(this.timer);
    };
    OldFilePreviewer.prototype.loadIframe = function () {
        var _this = this;
        this.isLoading = true;
        this.timer = setTimeout(function () {
            _this.loadIframe();
        }, this.loadTimeout);
        try {
            this.iframeRef.src = this.getUrlFrame();
        }
        catch (e) {
            this.markIframeLoaded();
        }
    };
    OldFilePreviewer.prototype.markIframeLoaded = function () {
        this.isLoading = false;
        clearTimeout(this.timer);
    };
    Object.defineProperty(OldFilePreviewer.prototype, "isPictureOrGif", {
        get: function () {
            return constants_1.Extensions.picture.includes(this.model.fileExtension) || this.model.fileExtension == 'gif';
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(OldFilePreviewer.prototype, "isOther", {
        get: function () {
            return constants_1.Extensions.other.includes(this.model.fileExtension);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(OldFilePreviewer.prototype, "isOffice", {
        get: function () {
            return constants_1.Extensions.office.includes(this.model.fileExtension);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(OldFilePreviewer.prototype, "isNotSupported", {
        get: function () {
            if (this.isFromLocalStorage) {
                return false == (this.isPictureOrGif || this.isOther);
            }
            return false == (this.isOffice || this.isPictureOrGif || this.isOther);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(OldFilePreviewer.prototype, "isFromLocalStorage", {
        get: function () {
            return this.originalUrl.startsWith(this.config.globalConfig.remoteServiceName);
        },
        enumerable: false,
        configurable: true
    });
    OldFilePreviewer.prototype.getUrlFrame = function () {
        if (this.isNotSupported) {
            return '';
        }
        if (this.isFromLocalStorage) {
            return this.originalUrl;
        }
        if (this.isOffice || this.isOther) {
            var fileUrl = new URL(this.model.fileUrl);
            fileUrl.searchParams.append("dt", new Date().toISOString());
            if (this.isOffice) {
                return "https://view.officeapps.live.com/op/embed.aspx?src=".concat(fileUrl.href);
            }
            else {
                return "https://docs.google.com/viewerng/viewer?url=".concat(fileUrl.href, "&embedded=true");
            }
        }
        return '';
    };
    OldFilePreviewer.prototype.download = function () {
        if (this.isFromLocalStorage) {
            var link = document.createElement('a');
            link.href = this.originalUrl;
            link.download = this.model.fileName || this.originalUrl.substring(this.originalUrl.lastIndexOf('/') + 1);
            link.click();
        }
        else {
            this.router.navigate(this.originalUrl, { replace: false });
        }
    };
    __decorate([
        aurelia_framework_1.bindable,
        __metadata("design:type", Object)
    ], OldFilePreviewer.prototype, "loadTimeout", void 0);
    __decorate([
        (0, aurelia_framework_1.computedFrom)("model.fileExtension"),
        __metadata("design:type", Object),
        __metadata("design:paramtypes", [])
    ], OldFilePreviewer.prototype, "isPictureOrGif", null);
    __decorate([
        (0, aurelia_framework_1.computedFrom)("model.fileExtension"),
        __metadata("design:type", Object),
        __metadata("design:paramtypes", [])
    ], OldFilePreviewer.prototype, "isOther", null);
    __decorate([
        (0, aurelia_framework_1.computedFrom)("model.fileExtension"),
        __metadata("design:type", Object),
        __metadata("design:paramtypes", [])
    ], OldFilePreviewer.prototype, "isOffice", null);
    __decorate([
        (0, aurelia_framework_1.computedFrom)('isOffice', 'isPictureOrGif', 'isOther', 'isFromLocalStorage'),
        __metadata("design:type", Object),
        __metadata("design:paramtypes", [])
    ], OldFilePreviewer.prototype, "isNotSupported", null);
    __decorate([
        (0, aurelia_framework_1.computedFrom)('originalUrl'),
        __metadata("design:type", Object),
        __metadata("design:paramtypes", [])
    ], OldFilePreviewer.prototype, "isFromLocalStorage", null);
    OldFilePreviewer = __decorate([
        aurelia_framework_1.autoinject,
        __metadata("design:paramtypes", [aurelia_dialog_1.DialogController, aurelia_router_1.Router, config_1.Config])
    ], OldFilePreviewer);
    return OldFilePreviewer;
}());

//# sourceMappingURL=old-file-previewer.js.map
