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 { getMimeTypeFromExtension } from './utils.js';
import { css, customElement, html, nothing, property, state } from '../../../../../external/lit/index.js';
import { stringOrStringArrayContains } from '../../../../core/utils/index.js';
import { UmbLitElement } from '../../../../core/lit-element/index.js';
import { UmbExtensionsManifestInitializer } from '../../../../../libs/extension-api/index.js';
import { umbExtensionsRegistry } from '../../../../core/extension-registry/index.js';
let UmbFileUploadPreviewElement = class UmbFileUploadPreviewElement extends UmbLitElement {
    set path(value) {
        this.#path = value;
        this.#setPreviewAlias();
    }
    get path() {
        return this.#path;
    }
    #path = '';
    #manifests = [];
    async #setPreviewAlias() {
        if (!this.path)
            return;
        const manifests = await this.#getManifests();
        const fallbackAlias = manifests.find((manifest) => stringOrStringArrayContains(manifest.forMimeTypes, '*/*'))?.alias;
        let mimeType = null;
        if (this.file) {
            mimeType = this.file.type;
        }
        else {
            mimeType = this.#getMimeTypeFromPath(this.path);
        }
        if (!mimeType) {
            this._previewAlias = fallbackAlias;
            return;
        }
        // Check for an exact match
        const exactMatch = manifests.find((manifest) => {
            return stringOrStringArrayContains(manifest.forMimeTypes, mimeType);
        });
        if (exactMatch) {
            this._previewAlias = exactMatch.alias;
            return;
        }
        // Check for wildcard match (e.g. image/*)
        const wildcardMatch = manifests.find((manifest) => {
            const forMimeTypes = Array.isArray(manifest.forMimeTypes) ? manifest.forMimeTypes : [manifest.forMimeTypes];
            return forMimeTypes.find((type) => {
                const snippet = type.replace(/\*/g, '');
                if (mimeType.startsWith(snippet))
                    return manifest.alias;
                if (mimeType.endsWith(snippet))
                    return manifest.alias;
                return undefined;
            });
        });
        if (wildcardMatch) {
            this._previewAlias = wildcardMatch.alias;
            return;
        }
        // Use fallbackAlias.
        this._previewAlias = fallbackAlias;
    }
    async #getManifests() {
        if (this.#manifests.length)
            return this.#manifests;
        await new UmbExtensionsManifestInitializer(this, umbExtensionsRegistry, 'fileUploadPreview', null, (exts) => {
            this.#manifests = exts.map((exts) => exts.manifest);
        }).asPromise();
        return this.#manifests;
    }
    #getMimeTypeFromPath(path) {
        // Extract the the MIME type from the data url
        if (path.startsWith('data:')) {
            const mimeType = path.substring(5, path.indexOf(';'));
            return mimeType;
        }
        // Extract the file extension from the path
        const extension = path.split('.').pop()?.toLowerCase();
        if (!extension)
            return null;
        return getMimeTypeFromExtension('.' + extension);
    }
    render() {
        if (!this.path)
            return nothing;
        return html `
			<umb-extension-slot
				single
				type="fileUploadPreview"
				.props=${{ path: this.path, file: this.file }}
				.filter=${(manifest) => manifest.alias === this._previewAlias}>
			</umb-extension-slot>
		`;
    }
    static { this.styles = [
        css `
			:host {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 100%;
				height: 100%;
			}
		`,
    ]; }
};
__decorate([
    state()
], UmbFileUploadPreviewElement.prototype, "_previewAlias", void 0);
__decorate([
    property({ type: Object })
], UmbFileUploadPreviewElement.prototype, "file", void 0);
__decorate([
    property({ type: String })
], UmbFileUploadPreviewElement.prototype, "path", null);
UmbFileUploadPreviewElement = __decorate([
    customElement('umb-file-upload-preview')
], UmbFileUploadPreviewElement);
export { UmbFileUploadPreviewElement };
export { UmbFileUploadPreviewElement as element };
