import { r as registerInstance, h } from './index-BIwHMk6j.js';
import { G as Ge, R as Re, b as n } from './index.esm-CMeGYws2.js';
import { g as getPreviewType } from './extension-mapping-Bcm94u_z.js';
import { n as negate } from './negate-ooVD-6_a.js';

const LIVE_DOCS_TYPES = ['pdf', 'edit', 'view'];

const fileViewerCss = () => `.not-found{display:flex;flex-direction:column;height:100%;align-items:center;justify-content:center;line-height:1rem;color:rgb(var(--contrast-1100))}.not-found limel-icon{color:rgb(var(--color-red-default))}iframe{height:100%;width:100%}`;

var __decorate = (undefined && undefined.__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;
};
const FileViewer = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        /**
         * What file types to use when viewing the file.
         *
         * The first available one will be used
         */
        this.fileTypes = ['view', 'contents', 'download'];
        this.renderLiveDocs = (file) => {
            if (!this.isLiveDocsAvailable(file)) {
                return h("limebb-live-docs-info", null);
            }
            if (!this.platform.isFeatureEnabled('useOfficeFilePreview')) {
                return (h("limebb-empty-state", { value: 'Preview of this file type will be available soon', icon: {
                        name: 'attention',
                        color: 'rgb(var(--color-blue-light))',
                    } }));
            }
            const url = this.getLiveDocsUrl(file);
            return (h("iframe", { src: url, frameBorder: 0, marginHeight: 0, marginWidth: 0 }));
        };
        this.renderFileViewer = (file) => {
            const filename = file.filename;
            const url = this.getFileUrl(file);
            if (!url) {
                return this.renderNotFound();
            }
            return (h("limel-file-viewer", { filename: filename, url: url, allowFullscreen: true, allowOpenInNewTab: true }));
        };
        this.renderUnsupported = (file) => {
            return (h("limel-file-viewer", { filename: file.filename, url: undefined, allowFullscreen: true, allowOpenInNewTab: true }));
        };
        this.renderNotFound = () => {
            return [
                h("section", { class: "not-found" }, h("limel-icon", { name: "error", size: "large" }), h("h2", null, this.translator.get('webclient.not-found')), h("p", null, this.translator.get('webclient.no-files-attached', {
                    object: this.getDescriptive(this.limeobject),
                }))),
            ];
        };
    }
    render() {
        const file = this.getFile();
        if (!file) {
            return this.renderNotFound();
        }
        const fileType = getPreviewType(file.extension);
        const fileViewerFunctions = {
            fileViewer: this.renderFileViewer,
            browser: this.renderFileViewer,
            sharepoint: this.renderLiveDocs,
            unsupported: this.renderUnsupported,
        };
        const fileViewerFunction = fileViewerFunctions[fileType];
        return fileViewerFunction(file);
    }
    getLiveDocsUrl(file) {
        let fileTypes = [...this.fileTypes];
        if (this.platform.isFeatureEnabled('useOfficeFilePreview')) {
            fileTypes = ['pdf', ...fileTypes];
        }
        return fileTypes
            .filter(this.isLiveDocsType)
            .map((type) => file.getUrl(type))
            .find(Boolean);
    }
    getFileUrl(file) {
        return this.fileTypes
            .filter(negate(this.isLiveDocsType))
            .map((type) => file.getUrl(type))
            .find(Boolean);
    }
    getFile() {
        const property = this.fileProperty;
        if (!property) {
            return;
        }
        return this.limeobject.getFile(property.name);
    }
    isLiveDocsAvailable(file) {
        return LIVE_DOCS_TYPES.some((type) => file.getUrl(type));
    }
    isLiveDocsType(type) {
        return LIVE_DOCS_TYPES.includes(type);
    }
    getDescriptive(limeobject) {
        var _a;
        return ((limeobject === null || limeobject === void 0 ? void 0 : limeobject.descriptive) ||
            ((_a = this.limetype) === null || _a === void 0 ? void 0 : _a.localname.singular) ||
            this.translator.get('object'));
    }
    get fileProperty() {
        if (!this.limeobject) {
            return;
        }
        if (this.property) {
            const property = this.limeobject
                .getLimetype()
                .getProperty(this.property);
            if (property.type === 'file') {
                return property;
            }
        }
        return Object.values(this.limeobject.getLimetype().properties).find((prop) => prop.type === 'file');
    }
    get translator() {
        return this.platform.get(n.Translate);
    }
};
__decorate([
    Ge()
], FileViewer.prototype, "limeobject", void 0);
__decorate([
    Re()
], FileViewer.prototype, "limetype", void 0);
FileViewer.style = fileViewerCss();

export { FileViewer as limebb_limeobject_file_viewer };
