/**
 * 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-upload.css';
import NileElement from '../internal/nile-element';
import { DragHandler } from './utils/drag-drop.util';
import { FileUploadDefaults } from './types/file-upload.enums';
import { html } from 'lit';
import { customElement, property, query, state } from 'lit/decorators.js';
import { FileUploadState, FileUploadVariant, FileUploadEvent } from './types/file-upload.enums';
import { setUpDragHandler, addGlobalListeners, addInternalListeners, uploadFiles } from './utils/file-validation.util';
import { getHorizontalDefaultState, getHorizontalDragState, getVerticalDefaultState, getVerticalDisabledState, getVerticalDragState, getHorizontalErrorState, getVerticalErrorState, getHorizontalDisabledState } from './nile-file-upload.template';
/**
 * Nile file-upload component.
 *
 * @tag nile-file-upload
 *
 */
let NileFileUpload = class NileFileUpload extends NileElement {
    constructor() {
        super(...arguments);
        this.size = "10MB";
        this.error = false;
        this.errorMessage = "";
        this.allowMultiple = false;
        this.allowDuplicates = false;
        this.allowedTypes = [];
        this.uploadedFiles = [];
        this.title = FileUploadDefaults.TITLE;
        this.subtitle = FileUploadDefaults.SUBTITLE;
        this.state = FileUploadState.DEFAULT;
        this.variant = FileUploadVariant.HORIZONTAL;
        this.fileUploadUrl = "";
        this.autoUpload = true;
        this.doNotUpload = [];
        this.uploadRequests = new Map();
        this.fileSizeExceededFilesNumber = 0;
        this.isStringTruncated = false;
        this.setState = (newState) => {
            this.state = newState;
        };
    }
    /**
     * The styles for nile-file-upload
     * @remarks If you are extending this class you can extend the base styles with super. Eg `return [super(), myCustomStyles]`
     */
    static get styles() {
        return [styles];
    }
    connectedCallback() {
        super.connectedCallback();
        this.emit(FileUploadEvent.NILE_INIT);
    }
    firstUpdated(changedProps) {
        super.firstUpdated(changedProps);
        this.dragHandler = new DragHandler();
        setUpDragHandler(this, this.dragHandler);
        addInternalListeners(this, this.dragHandler, this.uploadRequests);
        addGlobalListeners(this.dragHandler);
    }
    updated(changedProps) {
        super.updated(changedProps);
        if (changedProps.has('uploadedFiles')) {
            if (this.fileUploadUrl && this.autoUpload) {
                uploadFiles(this);
            }
            this.emit(FileUploadEvent.NILE_CHANGE, { files: this.uploadedFiles });
        }
        else if (changedProps.has('state')) {
            this.dragHandler.setValuesInDragHandler(this.state);
        }
        if (changedProps.has('autoUpload') && this.autoUpload) {
            if (this.fileUploadUrl && this.autoUpload) {
                uploadFiles(this);
            }
            this.emit(FileUploadEvent.NILE_CHANGE, { files: this.uploadedFiles });
        }
    }
    browseFiles() {
        this.emit(FileUploadEvent.NILE_BROWSE);
        this.errorMessage = "";
        this.input.click();
    }
    render() {
        return html `
      ${this.getState()}
    `;
    }
    clearAllUploadedFiles() {
        this.uploadedFiles = [];
        this.state = FileUploadState.DEFAULT;
        this.errorMessage = "";
    }
    getState() {
        return this.variant === FileUploadVariant.HORIZONTAL
            ? this.getHorizontalState()
            : this.getVerticalState();
    }
    getHorizontalState() {
        switch (this.state) {
            case FileUploadState.DEFAULT:
                return getHorizontalDefaultState(this.browseFiles, this.title, this.subtitle, this.dragHandler, this.state, this.errorMessage, this.allowedTypes, this);
            case FileUploadState.DISABLED:
                return getHorizontalDisabledState(this.browseFiles, this.title, this.subtitle, this.dragHandler, this.state, this.errorMessage, this.allowedTypes, this);
            case FileUploadState.DRAG:
                return getHorizontalDragState();
            case FileUploadState.ERROR:
                return getHorizontalErrorState(this.browseFiles, this.title, this.subtitle, this.dragHandler, this.state, this.errorMessage, this.allowedTypes, this);
            default:
                return getHorizontalDefaultState(this.browseFiles, this.title, this.subtitle, this.dragHandler, this.state, this.errorMessage, this.allowedTypes, this);
        }
    }
    getVerticalState() {
        switch (this.state) {
            case FileUploadState.DEFAULT:
                return getVerticalDefaultState(this.browseFiles, this.title, this.subtitle, this.dragHandler, this.errorMessage, this.allowedTypes, this);
            case FileUploadState.DISABLED:
                return getVerticalDisabledState(this.browseFiles, this.title, this.subtitle, this.dragHandler, this.errorMessage, this.allowedTypes, this);
            case FileUploadState.DRAG:
                return getVerticalDragState();
            case FileUploadState.ERROR:
                return getVerticalErrorState(this.browseFiles, this.title, this.subtitle, this.dragHandler, this.errorMessage, this.allowedTypes, this);
            default:
                return getVerticalDefaultState(this.browseFiles, this.title, this.subtitle, this.dragHandler, this.errorMessage, this.allowedTypes, this);
        }
    }
    disconnectedCallback() {
        this.emit(FileUploadEvent.NILE_DESTROY);
    }
};
__decorate([
    property({ type: String })
], NileFileUpload.prototype, "size", void 0);
__decorate([
    property({ type: Boolean })
], NileFileUpload.prototype, "error", void 0);
__decorate([
    property({ type: String })
], NileFileUpload.prototype, "errorMessage", void 0);
__decorate([
    property({ type: Boolean })
], NileFileUpload.prototype, "allowMultiple", void 0);
__decorate([
    property({ type: Boolean })
], NileFileUpload.prototype, "allowDuplicates", void 0);
__decorate([
    property({ type: Array })
], NileFileUpload.prototype, "allowedTypes", void 0);
__decorate([
    property({ type: Array })
], NileFileUpload.prototype, "uploadedFiles", void 0);
__decorate([
    property({ type: String })
], NileFileUpload.prototype, "title", void 0);
__decorate([
    property({ type: String })
], NileFileUpload.prototype, "subtitle", void 0);
__decorate([
    property({ type: String })
], NileFileUpload.prototype, "state", void 0);
__decorate([
    property({ type: String })
], NileFileUpload.prototype, "variant", void 0);
__decorate([
    property({ type: String })
], NileFileUpload.prototype, "fileUploadUrl", void 0);
__decorate([
    property({ type: Boolean })
], NileFileUpload.prototype, "autoUpload", void 0);
__decorate([
    state()
], NileFileUpload.prototype, "doNotUpload", void 0);
__decorate([
    query('input')
], NileFileUpload.prototype, "input", void 0);
__decorate([
    query('.horizontal-div')
], NileFileUpload.prototype, "horizontalDiv", void 0);
NileFileUpload = __decorate([
    customElement('nile-file-upload')
], NileFileUpload);
export { NileFileUpload };
//# sourceMappingURL=nile-file-upload.js.map