/**
 * Copyright Aquera Inc 2025
 *
 * This source code is licensed under the BSD-3-Clause license found in the
 * LICENSE file in the root directory of this source tree.
 */
import { __decorate } from "tslib";
import { styles } from './nile-file-preview.css';
import NileElement from '../internal/nile-element';
import { customElement, property } from 'lit/decorators.js';
import { generatePreviewUrl, isRealImage } from './utils';
import { FilePreviewState, FilePreviewVariant, FilePreviewErrorMessages, FilePreviewEvent } from './types';
import { html } from 'lit';
import { getHorizontalErrorState, getHorizontalNoPreviewState, getHorizontalPreviewState, getHorizontalUploadingState, getVerticalErrorState, getVerticalNoPreviewState, getVerticalPreviewState, getVerticalUploadingState } from './nile-file-preview.template';
/**
 * Nile preview component.
 *
 * @tag nile-preview
 *
 */
let NileFilePreview = class NileFilePreview extends NileElement {
    constructor() {
        super(...arguments);
        this.errorMessage = "";
        this.fileUrl = "";
        this.uploadStatus = 0;
        this.state = FilePreviewState.DEFAULT;
        this.variant = FilePreviewVariant.HORIZONTAL;
        this.inputFileName = "";
        this.showFileType = false;
        this.inputFileHtml = null;
        this.originalUrl = "";
        this.isStringTruncated = false;
        /* #endregion */
    }
    /**
     * The styles for nile-preview
     * @remarks If you are extending this class you can extend the base styles with super. Eg `return [super(), myCustomStyles]`
     */
    static get styles() {
        return [styles];
    }
    /**
     * Render method
     * @slot This is a slot test
     */
    connectedCallback() {
        super.connectedCallback();
        this.emit(FilePreviewEvent.NILE_INIT);
    }
    firstUpdated(_changedProperties) {
        super.firstUpdated(_changedProperties);
    }
    updated(changedProperties) {
        if (changedProperties.has('uploadStatus') && this.uploadStatus) {
            if (this.uploadStatus < 100) {
                this.createUploadState(this.inputFile, this.uploadStatus);
            }
            else {
                this.createState(this.inputFile);
            }
        }
        else if (changedProperties.has('inputFile') && this.inputFile) {
            this.createState(this.inputFile);
            this.emit(FilePreviewEvent.NILE_RECEIVE);
        }
        else if (changedProperties.has('fileUrl') && this.fileUrl) {
            this.handleFileUrl(this.fileUrl);
            this.emit(FilePreviewEvent.NILE_RECEIVE);
        }
        else if (changedProperties.has('errorMessage')) {
            this.createState(this.inputFile);
        }
    }
    createUploadState(file, uploadStatus) {
        const isHorizontal = this.variant === FilePreviewVariant.HORIZONTAL;
        this.inputFileHtml = isHorizontal
            ? getHorizontalUploadingState(file, uploadStatus, this)
            : getVerticalUploadingState(file, uploadStatus, this);
        this.requestUpdate();
    }
    async handleFileUrl(url) {
        this.originalUrl = url;
        try {
            const response = await fetch(url);
            const blob = await response.blob();
            const fileName = this.inputFileName || url.split('/').pop() || FilePreviewErrorMessages.UNKNOWN_FILE_NAME;
            const file = new File([blob], fileName, { type: blob.type });
            this.createState(file);
        }
        catch (error) {
            const dummyFile = new File([], this.inputFileName || FilePreviewErrorMessages.UNKNOWN_FILE_NAME, { type: 'application/octet-stream' });
            this.errorMessage = FilePreviewErrorMessages.FAILED_TO_LOAD;
            console.error(this.errorMessage);
            this.inputFileHtml = this.variant === FilePreviewVariant.HORIZONTAL
                ? getHorizontalErrorState(dummyFile, this.errorMessage, this.originalUrl, this)
                : getVerticalErrorState(dummyFile, this.errorMessage, this.originalUrl, this);
            this.requestUpdate();
        }
    }
    async createState(file) {
        const isHorizontal = this.variant === FilePreviewVariant.HORIZONTAL;
        if (!this.errorMessage) {
            let isImage = false;
            try {
                isImage = file && await isRealImage(file);
            }
            catch (error) {
                isImage = false;
            }
            if (isImage) {
                try {
                    const url = await generatePreviewUrl(file);
                    this.inputFileHtml = isHorizontal
                        ? getHorizontalPreviewState(url, file, this.originalUrl, this.showFileType)
                        : getVerticalPreviewState(url, file, this.originalUrl, this.showFileType);
                }
                catch (error) {
                    this.inputFileHtml = isHorizontal
                        ? getHorizontalNoPreviewState(file, this.originalUrl, this.showFileType)
                        : getVerticalNoPreviewState(file, this.originalUrl, this.showFileType);
                }
            }
            else {
                this.inputFileHtml = isHorizontal
                    ? getHorizontalNoPreviewState(file, this.originalUrl, this.showFileType)
                    : getVerticalNoPreviewState(file, this.originalUrl, this.showFileType);
            }
        }
        else {
            this.inputFileHtml = isHorizontal
                ? getHorizontalErrorState(file, this.errorMessage, this.originalUrl, this)
                : getVerticalErrorState(file, this.errorMessage, this.originalUrl, this);
        }
        this.requestUpdate();
    }
    render() {
        return html `${this.inputFileHtml ?? html ``}`;
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        this.emit(FilePreviewEvent.NILE_DESTROY);
    }
};
__decorate([
    property({ type: String })
], NileFilePreview.prototype, "errorMessage", void 0);
__decorate([
    property({ type: Object })
], NileFilePreview.prototype, "inputFile", void 0);
__decorate([
    property({ type: String })
], NileFilePreview.prototype, "fileUrl", void 0);
__decorate([
    property({ type: Number })
], NileFilePreview.prototype, "uploadStatus", void 0);
__decorate([
    property({ type: String })
], NileFilePreview.prototype, "state", void 0);
__decorate([
    property({ type: String })
], NileFilePreview.prototype, "variant", void 0);
__decorate([
    property({ type: String })
], NileFilePreview.prototype, "inputFileName", void 0);
__decorate([
    property({ type: Boolean, reflect: true, attribute: true })
], NileFilePreview.prototype, "showFileType", void 0);
NileFilePreview = __decorate([
    customElement('nile-file-preview')
], NileFilePreview);
export { NileFilePreview };
export default NileFilePreview;
//# sourceMappingURL=nile-file-preview.js.map