"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileDescriptor = void 0;
exports.fileDescriptorFactory = FileDescriptorFactory;
class FileState {
}
FileState.DELETED = "deleted";
FileState.UPLOADING = "uploading";
FileState.UPLOADED = "uploaded";
FileState.NOT_UPLOADED = "not_uploaded";
FileState.DOWNLOADING = "downloading";
FileState.UNKNOWN = "unknown";
class ThumbnailPlaceholder {
    constructor(icon, style) {
        this.icon = icon;
        this.style = style;
    }
}
class Thumbnail {
    /**
     * @this FileDescriptor
     */
    constructor(data) {
        if (data) {
            this.availableThumbnail = data.availableThumbnail;
            this.md5sum = data.md5sum;
            this.size = data.size;
            this.wantThumbnailDate = data.wantThumbnailDate;
        }
        else {
            this.availableThumbnail = false;
        }
    }
    isThumbnailAvailable() {
        return this.availableThumbnail;
    }
    ;
}
/**
 * @public
 * @class
 * @name FileDescriptor
 * @description
 *      This class is used to represent a File Descriptor or a Short File Descriptor which describes a file shared in a conversation (one-to-one or bubble).
 */
class FileDescriptor {
    /**
     * @this FileDescriptor
     */
    constructor(id = null, url = null, ownerId = null, fileName = null, extension = null, typeMIME = null, size = null, registrationDate = null, uploadedDate = null, dateToSort = null, viewers = null, state = null, thumbnail = null, orientation, md5sum, applicationId) {
        /**
         * @public
         * @property {string} id The file descriptor ID (File Descriptor only)
         * @readonly
         */
        this.id = id;
        /**
         * @public
         * @property {string} url The file descriptor url (File Descriptor only)
         * @readonly
         */
        this.url = url;
        /**
         * @public
         * @property {string} ownerId The ID of the owner (File Descriptor only)
         * @readonly
         */
        this.ownerId = ownerId;
        /**
         * @public
         * @property {string} fileName The name of the file
         * @readonly
         */
        this.fileName = fileName;
        /**
         * @public
         * @property {string} extension The extension of the file
         * @readonly
         */
        this.extension = extension;
        /**
         * @public
         * @property {string} typeMIME The mime type of the file ('mime' in Short File Descriptor)
         * @readonly
         */
        this.typeMIME = typeMIME;
        /**
         * @public
         * @property {ThumbnailPlaceholder} thumbnailPlaceholder The thumbnail icon placeholder info
         * @readonly
         */
        this.thumbnailPlaceholder = this.getThumbnailPlaceholderFromMimetype(typeMIME);
        /**
         * @public
         * @property {string} size The size of the file (octets)
         * @readonly
         */
        this.size = size;
        /**
         * @public
         * @property {Object} registrationDate The creation date (File Descriptor only)
         * @readonly
         */
        this.registrationDate = registrationDate;
        /**
         * @public
         * @property {Object} uploadedDate The upload date (File Descriptor only)
         * @readonly
         */
        this.uploadedDate = uploadedDate;
        /**
         * @private
         * @property {Object} dateToSort The date to sort (?)
         * @readonly
         */
        this.dateToSort = dateToSort;
        /**
         * @public
         * @property {any[]} viewers The list of viewers (File Descriptor only)
         * @readonly
         */
        this.viewers = viewers;
        this.state = state;
        this.thumbnail = new Thumbnail(thumbnail);
        this.fileToSend = undefined;
        this.previewBlob = undefined;
        /**
         * @public
         * @property {number} orientation
         * @description
         *      There are four possible values for orientation and the image should be rotated acording to this value.
         *      1 -> rotate(0deg),
         *      3 -> rotate(180deg),
         *      6 -> rotate(90deg),
         *      8 -> rotate(270deg).
         */
        this.orientation = orientation ? orientation : undefined;
        /**
         * @public
         * @property {string} md5sum
         * @description
         *     md5 of the file get from the backend file storage (default: "", refreshed each time the file is uploaded).
         */
        this.md5sum = md5sum;
        /**
         * @public
         * @property {string} applicationId
         * @description
         *    applicationId the file belong to.
         */
        this.applicationId = applicationId;
    }
    ;
    isMicrosoftFile() {
        let mediaExtension = ["docx", "doc", "ppt", "pptx", "xls", "xlsx"];
        return (mediaExtension.some((ext) => ext === this.extension));
    }
    ;
    isThumbnailPossible() {
        return (this.isImage() || this.isPDF());
    }
    ;
    isPDF() {
        return (this.typeMIME === "application/pdf");
    }
    ;
    isImage() {
        let imgType = "image/";
        return this.typeMIME && this.typeMIME.length >= imgType.length && this.typeMIME.slice(0, imgType.length) === imgType;
    }
    ;
    isAudioVideo() {
        let mediaExtension = ["avi", "mpg", "wma", "mp3", "wmv", "mkv", "mov", "wav", "ogg", "mp4", "aac"];
        return (mediaExtension.some((ext) => ext === this.extension));
    }
    ;
    isUploaded() {
        let imgType = "image/";
        return this.state && this.state === FileState.UPLOADED;
    }
    ;
    isAlreadyFileViewer(viewerId) {
        if (this.ownerId && this.ownerId === viewerId) {
            // We are owner of this file
            return true;
        }
        if (this.viewers) {
            return this.viewers.some((viewer) => viewer.viewerId === viewerId);
        }
        return false;
    }
    getDisplayName() {
        return this.fileName.replace(/\.[^/.]+$/, "");
    }
    getDisplayNameTruncated() {
        var str = this.fileName.replace(/\.[^/.]+$/, "");
        return [str.substring(0, str.length - 4), str.slice(-4)];
    }
    getExtension() {
        if (this.fileName.toUpperCase() === this.extension.toUpperCase()) {
            return "";
        }
        else {
            return "." + this.extension;
        }
    }
    getThumbnailPlaceholderFromMimetype(mime) {
        if (!mime) {
            return new ThumbnailPlaceholder('icon_filestandard', 'otherStyle');
        }
        if (mime.indexOf("image") === 0) {
            return new ThumbnailPlaceholder('icon_image', 'imageStyle');
        }
        if (mime === "application/msword" ||
            (/^application\/vnd.openxmlformats-officedocument.wordprocessingml.*$/.test(mime)) ||
            mime === "application/vnd.oasis.opendocument.text") {
            return new ThumbnailPlaceholder('icon_doc', 'docStyle');
        }
        if (/^application\/vnd.ms-powerpoint.*$/.test(mime) ||
            (/^application\/vnd.openxmlformats-officedocument.presentationml.*$/.test(mime)) ||
            mime === "application/vnd.oasis.opendocument.presentation") {
            return new ThumbnailPlaceholder('icon_ppt', 'pptStyle');
        }
        if (mime.indexOf("application/vnd.ms-excel") === 0 ||
            (/^application\/vnd.openxmlformats-officedocument.spreadsheetml.*$/.test(mime))) {
            return new ThumbnailPlaceholder('icon_xls', 'xlsStyle');
        }
        if (mime === "application/pdf" || mime === "application/vnd.oasis.opendocument.spreadsheet") {
            return new ThumbnailPlaceholder('icon_pdf', 'pdfStyle');
        }
        if (mime.indexOf("video/") === 0 || mime.indexOf("audio/") === 0) {
            return new ThumbnailPlaceholder('icon_file-video', 'imageStyle');
        }
        return new ThumbnailPlaceholder('icon_filestandard', 'otherStyle');
    }
    ;
}
exports.FileDescriptor = FileDescriptor;
function FileDescriptorFactory() {
    return (id, url, ownerId, fileName, extension, typeMIME, size, registrationDate, uploadedDate, dateToSort, viewers, state, thumbnail, orientation, md5sum, applicationId) => {
        return new FileDescriptor(id, url, ownerId, fileName, extension, typeMIME, size, registrationDate, uploadedDate, dateToSort, viewers, state, thumbnail, orientation, md5sum, applicationId);
    };
}
module.exports.fileDescriptorFactory = FileDescriptorFactory;
//# sourceMappingURL=FileDescriptor.js.map