// #region Imports
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;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Property } from '../../../../Decorators/PropertyDecorator';
import { Attribute } from '../../../../Decorators/AttributeDecorator';
import { Component } from '../../../../Decorators/ComponentDecorator';
import { ButtonElement } from '../../../Buttons/Button/ButtonElement';
import { FilePickerElement } from '../../../Inputs/FileUpload/FilePickerElement';
import { attachmentChatToolElementTemplate } from './AttachmentChatToolElementTemplate';
import { ChatToolBaseElement } from './Abstracts/ChatToolBaseElement';
import { Themeable } from '../../../../Behaviors/Themeable';
import { Emit } from '../../../../../Events/Decorators/EmitDecorator';
import { HTMLElementEventEmitter } from '@breadstone/mosaik-elements';
// #endregion
/**
 * The `{@link AttachmentChatToolElement}` element.
 *
 * @fires filePicked - Called when a file is picked.
 * @fires chatAttachmentRejected - Called when an attachment is rejected.
 *
 * @public
 */
let AttachmentChatToolElement = class AttachmentChatToolElement extends Themeable(ChatToolBaseElement) {
    // #region Fields
    _attachmentRejected;
    _multiple;
    _accept;
    _directory;
    _maxFiles;
    _maxFileSize;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._multiple = false;
        this._accept = [];
        this._directory = false;
        this._maxFiles = 10;
        this._maxFileSize = 10 * 1024 * 1024; // 10 MB
        this._attachmentRejected = new HTMLElementEventEmitter(this, 'attachmentRejected');
    }
    // #endregion
    // #region Properties
    /**
     * Returns the `{@link is}` property.
     * The `{@link is}` property represents natural name of this element.
     *
     * @public
     * @static
     * @readonly
     */
    static get is() {
        return 'mosaik-attachment-chat-tool';
    }
    /**
     * Gets or sets the `multiple` property.
     *
     * @public
     * @attr
     */
    get multiple() {
        return this._multiple;
    }
    set multiple(value) {
        if (this._multiple !== value) {
            this._multiple = value;
            this.requestUpdate('multiple');
        }
    }
    /**
     * Gets or sets the `accept` property.
     *
     * @public
     */
    get accept() {
        return this._accept;
    }
    set accept(value) {
        if (this._accept !== value) {
            this._accept = value;
            this.requestUpdate('accept');
        }
    }
    /**
     * Gets or sets the `directory` property.
     *
     * @public
     * @attr
     */
    get directory() {
        return this._directory;
    }
    set directory(value) {
        if (this._directory !== value) {
            this._directory = value;
            this.requestUpdate('directory');
        }
    }
    /**
     * Gets or sets the `maxFiles` property.
     *
     * @public
     * @attr
     */
    get maxFiles() {
        return this._maxFiles;
    }
    set maxFiles(value) {
        if (this._maxFiles !== value) {
            this._maxFiles = value;
            this.requestUpdate('maxFiles');
        }
    }
    /**
     * Gets or sets the `maxFileSize` property.
     *
     * @public
     * @attr
     */
    get maxFileSize() {
        return this._maxFileSize;
    }
    set maxFileSize(value) {
        if (this._maxFileSize !== value) {
            this._maxFileSize = value;
            this.requestUpdate('maxFileSize');
        }
    }
    /**
     * Called when an attachment is rejected.
     * Provides reference to `{@link IChatAttachmentRejectEventDetail}` as event detail.
     *
     * @public
     * @readonly
     * @eventProperty
     */
    get attachmentRejected() {
        return this._attachmentRejected;
    }
    // #endregion
    // #region Methods
    /**
     * @public
     * @override
     */
    execute(args) {
        // check if fileList or array of files
        if (!(args instanceof FileList || Array.isArray(args))) {
            return false;
        }
        if (!this.chatElement?.inputElement) {
            return false;
        }
        if (!Array.from(args).every((file) => this.validate(file))) {
            return false;
        }
        this.chatElement.inputElement.value.files.push(...Array.from(args));
        this.chatElement.inputElement.requestUpdate('value');
        this.chatElement.inputElement.attachmentsElement.requestUpdate('files');
        return true;
    }
    /**
     * Emits the {@link attachmentRejected} event.
     *
     * @protected
     */
    onAttachmentRejected(args) {
        this._attachmentRejected.emit(args);
    }
    /**
     * @private
     */
    validate(file) {
        if (this.accept.length > 0 && !this.accept.includes(file.type)) {
            this.onAttachmentRejected({
                file: file,
                reason: 'type',
                cancel: true
            });
            return false;
        }
        if (file.size > this.maxFileSize) {
            this.onAttachmentRejected({
                file: file,
                reason: 'size',
                cancel: true
            });
            return false;
        }
        if (this.chatElement?.inputElement && this.chatElement.inputElement.value.files.length >= this.maxFiles) {
            this.onAttachmentRejected({
                file: file,
                reason: 'count',
                cancel: true
            });
            return false;
        }
        return true;
    }
};
__decorate([
    Emit({ eventName: 'attachmentRejected' }),
    __metadata("design:type", Object)
], AttachmentChatToolElement.prototype, "_attachmentRejected", void 0);
__decorate([
    Attribute({
        type: Boolean,
        useDefault: true
    }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], AttachmentChatToolElement.prototype, "multiple", null);
__decorate([
    Property({ type: (Array) }),
    __metadata("design:type", Array),
    __metadata("design:paramtypes", [Array])
], AttachmentChatToolElement.prototype, "accept", null);
__decorate([
    Attribute({
        type: Boolean,
        useDefault: true
    }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], AttachmentChatToolElement.prototype, "directory", null);
__decorate([
    Attribute({
        type: Number,
        useDefault: true
    }),
    __metadata("design:type", Number),
    __metadata("design:paramtypes", [Number])
], AttachmentChatToolElement.prototype, "maxFiles", null);
__decorate([
    Attribute({
        type: Number,
        useDefault: true
    }),
    __metadata("design:type", Number),
    __metadata("design:paramtypes", [Number])
], AttachmentChatToolElement.prototype, "maxFileSize", null);
AttachmentChatToolElement = __decorate([
    Component({
        selector: 'mosaik-attachment-chat-tool',
        template: attachmentChatToolElementTemplate,
        // styles: attachmentChatToolElementStyle,
        host: {
            slot: 'tools'
        },
        options: {
            mode: 'open',
            delegatesFocus: true
        },
        imports: [
            FilePickerElement,
            ButtonElement
        ]
    }),
    __metadata("design:paramtypes", [])
], AttachmentChatToolElement);
export { AttachmentChatToolElement };
//# sourceMappingURL=AttachmentChatToolElement.js.map