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;
};
import { h } from "@stencil/core";
import { PlatformServiceName, SelectCurrentLimeObject, SelectCurrentLimeType, } from "@limetech/lime-web-components";
import { negate } from "lodash-es";
import { LIVE_DOCS_TYPES } from "./constants";
import { getPreviewType } from "./extension-mapping";
/**
 * Component for displaying the contents of an attached file on a limeobject
 *
 * Example use case is to use it as a tab on the document card
 *
 * @exampleComponent limebb-example-limeobject-file-viewer
 * @exampleComponent limebb-example-limeobject-file-viewer-not-found
 * @exampleComponent limebb-example-limeobject-file-viewer-live-docs
 * @beta
 */
export class FileViewer {
    constructor() {
        /**
         * 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(PlatformServiceName.Translate);
    }
    static get is() { return "limebb-limeobject-file-viewer"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["file-viewer.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["file-viewer.css"]
        };
    }
    static get properties() {
        return {
            "platform": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "LimeWebComponentPlatform",
                    "resolved": "LimeWebComponentPlatform",
                    "references": {
                        "LimeWebComponentPlatform": {
                            "location": "import",
                            "path": "@limetech/lime-web-components",
                            "id": "node_modules::LimeWebComponentPlatform",
                            "referenceLocation": "LimeWebComponentPlatform"
                        }
                    }
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Reference to the platform service container.\n\nUse this to access all platform services like repositories, HTTP client,\ncommand bus, navigator, and more. The platform instance is shared across\nall components in the same application context."
                },
                "getter": false,
                "setter": false
            },
            "context": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "LimeWebComponentContext",
                    "resolved": "LimeWebComponentContext",
                    "references": {
                        "LimeWebComponentContext": {
                            "location": "import",
                            "path": "@limetech/lime-web-components",
                            "id": "node_modules::LimeWebComponentContext",
                            "referenceLocation": "LimeWebComponentContext"
                        }
                    }
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The context describing where this component is running.\n\nThe context provides information about the current limetype and record ID\n(if viewing/editing a specific record). Components can use this to load\nrelevant data and understand their operating environment."
                },
                "getter": false,
                "setter": false
            },
            "property": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Name of the file property to use\n\nIf not specified, will use the first file property that is found"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "property"
            },
            "fileTypes": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "LimeFileUrlType[]",
                    "resolved": "LimeFileUrlType[]",
                    "references": {
                        "LimeFileUrlType": {
                            "location": "import",
                            "path": "@limetech/lime-web-components",
                            "id": "node_modules::LimeFileUrlType",
                            "referenceLocation": "LimeFileUrlType"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "What file types to use when viewing the file.\n\nThe first available one will be used"
                },
                "getter": false,
                "setter": false,
                "defaultValue": "['view', 'contents', 'download']"
            }
        };
    }
    static get states() {
        return {
            "limeobject": {},
            "limetype": {}
        };
    }
}
__decorate([
    SelectCurrentLimeObject()
], FileViewer.prototype, "limeobject", void 0);
__decorate([
    SelectCurrentLimeType()
], FileViewer.prototype, "limetype", void 0);
