// #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 { Emit } from '../../../../Events/Decorators/EmitDecorator';
import { HTMLElementEventEmitter } from '@breadstone/mosaik-elements';
import { Variantable } from '../../../Behaviors/Variantable';
import { Attribute } from '../../../Decorators/AttributeDecorator';
import { Component } from '../../../Decorators/ComponentDecorator';
import { Property } from '../../../Decorators/PropertyDecorator';
import { CustomElement } from '../../Abstracts/CustomElement';
import { ButtonElement } from '../../Buttons/Button/ButtonElement';
import { ToggleButtonElement } from '../../Buttons/ToggleButton/ToggleButtonElement';
import { chatInputElementTemplate } from './ChatInputElementTemplate';
import { chatInputElementJoyStyle } from './Themes/ChatInputElement.Joy';
import { chatInputElementRetroStyle } from './Themes/ChatInputElement.Retro';
import { chatInputElementCosmopolitanStyle } from './Themes/ChatInputElement.Cosmopolitan';
import { Themeable } from '../../../Behaviors/Themeable';
// #endregion
/**
 * The `{@link ChatInputElement}` element.
 *
 * @fires submited - Called when a new message is submitted.
 *
 * @public
 */
let ChatInputElement = class ChatInputElement extends Themeable(Variantable(CustomElement)) {
    // #region Fields
    _submited;
    _transformer;
    _reactions;
    _value;
    _placeholder;
    _allowAttachments;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._value = {
            message: '',
            files: []
        };
        this._reactions = new Array();
        this._placeholder = '';
        this._transformer = (message) => ({
            message: {
                text: message.text.trim(),
                files: message.files
            },
            isValid: () => !!message.text.length
        });
        this._allowAttachments = false;
        this._submited = new HTMLElementEventEmitter(this, 'submited', { bubbles: false });
    }
    // #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-chat-input';
    }
    /**
     * Gets or sets the `value` property.
     *
     * @public
     */
    get value() {
        return this._value;
    }
    set value(value) {
        if (this._value !== value) {
            this._value = value;
            this.requestUpdate('value');
        }
    }
    /**
     * Gets or sets the `reactions` property.
     *
     * @public
     */
    get reactions() {
        return this._reactions;
    }
    set reactions(value) {
        if (this._reactions !== value) {
            this._reactions = value;
            this.requestUpdate('reactions');
        }
    }
    /**
     * Gets or sets the `placeholder` property.
     *
     * @public
     */
    get placeholder() {
        return this._placeholder;
    }
    set placeholder(value) {
        if (this._placeholder !== value) {
            this._placeholder = value;
            this.requestUpdate('placeholder');
        }
    }
    /**
     * Gets or sets the `allowAttachments` property.
     *
     * @public
     * @attr
     */
    get allowAttachments() {
        return this._allowAttachments;
    }
    set allowAttachments(value) {
        if (this._allowAttachments !== value) {
            this._allowAttachments = value;
            this.requestUpdate('allowAttachments');
        }
    }
    /**
     * Called when a new message is submitting.
     * Provides reference to `{@link IChatSubmitEventDetail}` as event detail.
     *
     * @public
     * @readonly
     * @eventProperty
     */
    get submited() {
        return this._submited;
    }
    // #endregion
    // #region Methods
    /**
     * @public
     */
    send(message, files) {
        const msg = this._transformer({
            text: message,
            files: Array.from(files ?? [])
        });
        const args = {
            message: msg.message,
            cancel: false
        };
        if (msg.isValid()) {
            if (!args.cancel) {
                if (msg.isValid()) {
                    // this._container.messages.push(msg.message);
                    this.onSubmited({
                        message: {
                            text: msg.message.text,
                            files: msg.message.files
                        }
                    });
                    this.clear();
                }
            }
        }
        else {
            // log invalid message
            console.warn('[ChatInputElement]: The chat message is invalid.');
        }
    }
    attach(files) {
        this._value.files.push(...Array.from(files));
        this.requestUpdate('value');
    }
    /**
     * @public
     */
    clear() {
        this.value = {
            message: '',
            files: []
        };
        this.requestUpdate('value');
    }
    /**
     * @private
     * @template
     */
    onReaction(reaction) {
        this.send(reaction);
    }
    /**
     * @private
     * @template
     */
    onInput(e) {
        this.value.message = e.target.value;
        this.requestUpdate('value');
    }
    /**
     * @private
     * @template
     */
    onKeyup(e) {
        if (e.key === 'Enter' && !e.shiftKey) {
            this.send(this.value.message, this.value.files);
        }
    }
    /**
     * @protected
     */
    onApplyTemplate() {
        super.onApplyTemplate();
        const text = this.getTemplatePart('textBox');
    }
    /**
     * Emits the {@link submited} event.
     *
     * @protected
     */
    onSubmited(args) {
        this._submited.emit(args);
    }
};
__decorate([
    Property({ type: Object }),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [Object])
], ChatInputElement.prototype, "value", null);
__decorate([
    Property({ type: (Array) }),
    __metadata("design:type", Array),
    __metadata("design:paramtypes", [Array])
], ChatInputElement.prototype, "reactions", null);
__decorate([
    Attribute({ type: String }),
    __metadata("design:type", String),
    __metadata("design:paramtypes", [String])
], ChatInputElement.prototype, "placeholder", null);
__decorate([
    Attribute({
        type: Boolean,
        useDefault: true
    }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], ChatInputElement.prototype, "allowAttachments", null);
__decorate([
    Emit({
        eventName: 'submited',
        bubbles: false
    }),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [])
], ChatInputElement.prototype, "submited", null);
ChatInputElement = __decorate([
    Component({
        selector: 'mosaik-chat-input',
        template: chatInputElementTemplate,
        themes: {
            joy: chatInputElementJoyStyle,
            retro: chatInputElementRetroStyle,
            cosmopolitan: chatInputElementCosmopolitanStyle
        },
        imports: [
            ButtonElement,
            ToggleButtonElement
        ]
    }),
    __metadata("design:paramtypes", [])
], ChatInputElement);
export { ChatInputElement };
//# sourceMappingURL=ChatInputElement.js.map