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 { UmbMediaUrlRepository } from '../../media/url/index.js';
import { css, customElement, html, nothing, property, state, when } from '../../../../external/lit/index.js';
import { UmbLitElement } from '../../../core/lit-element/index.js';
import { UmbTextStyles } from '../../../core/style/index.js';
let UmbMediaImageElement = class UmbMediaImageElement extends UmbLitElement {
    constructor() {
        super(...arguments);
        /**
         * The fallback icon for the thumbnail.
         */
        this.icon = 'icon-picture';
        /**
         * The `loading` state of the thumbnail.
         * @enum {'lazy' | 'eager'}
         * @default 'lazy'
         */
        this.loading = 'lazy';
        this._isLoading = true;
        this._imageUrl = '';
        this.#mediaRepository = new UmbMediaUrlRepository(this);
    }
    #mediaRepository;
    #intersectionObserver;
    connectedCallback() {
        super.connectedCallback();
        if (this.loading === 'lazy') {
            this.#intersectionObserver = new IntersectionObserver((entries) => {
                if (entries[0].isIntersecting) {
                    this.#generateThumbnailUrl();
                    this.#intersectionObserver?.disconnect();
                }
            });
            this.#intersectionObserver.observe(this);
        }
        else {
            this.#generateThumbnailUrl();
        }
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        this.#intersectionObserver?.disconnect();
    }
    async #generateThumbnailUrl() {
        if (!this.unique)
            throw new Error('Unique is missing');
        const { data } = await this.#mediaRepository.requestItems([this.unique]);
        this._imageUrl = data?.[0]?.url ?? '';
        this._isLoading = false;
    }
    render() {
        return html ` ${this.#renderThumbnail()} ${when(this._isLoading, () => this.#renderLoading())} `;
    }
    #renderLoading() {
        return html `<div id="loader"><uui-loader></uui-loader></div>`;
    }
    #renderThumbnail() {
        if (this._isLoading)
            return nothing;
        return when(this._imageUrl, () => html `<img
					part="img"
					src="${this._imageUrl}"
					alt="${this.alt ?? ''}"
					loading="${this.loading}"
					draggable="false" />`, () => html `<umb-icon id="icon" name="${this.icon}"></umb-icon>`);
    }
    static { this.styles = [
        UmbTextStyles,
        css `
			:host {
				display: contents;
			}

			#loader {
				display: flex;
				justify-content: center;
				align-items: center;
				height: 100%;
				width: 100%;
			}

			#icon {
				width: 100%;
				height: 100%;
				font-size: var(--uui-size-8);
			}
		`,
    ]; }
};
__decorate([
    property()
], UmbMediaImageElement.prototype, "unique", void 0);
__decorate([
    property()
], UmbMediaImageElement.prototype, "alt", void 0);
__decorate([
    property()
], UmbMediaImageElement.prototype, "icon", void 0);
__decorate([
    property()
], UmbMediaImageElement.prototype, "loading", void 0);
__decorate([
    state()
], UmbMediaImageElement.prototype, "_isLoading", void 0);
__decorate([
    state()
], UmbMediaImageElement.prototype, "_imageUrl", void 0);
UmbMediaImageElement = __decorate([
    customElement('umb-media-image')
], UmbMediaImageElement);
export { UmbMediaImageElement };
