// #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 { TextBoxElement } from '../../Inputs/TextBox/TextBoxElement';
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';
import { Appearanceable } from '../../../Behaviors/Appearanceable';
import { Disableable } from '../../../Behaviors/Disableable';
import { FocusRingElement } from '../../Primitives/FocusRing/FocusRingElement';
import { ChipElement } from '../Chip/ChipElement';
import { TextElement } from '../../Primitives/Text/TextElement';
import { DockPanelElement } from '../../Layouts/Dock/DockPanelElement';
import { Slottable } from '../../../Behaviors/Slottable';
import { ChatElement } from './ChatElement';
import { DomServiceLocator } from '../../../../Dom/Dom';
// #endregion
/**
 * The `{@link ChatInputElement}` element.
 *
 * @fires submitted - Called when a new message is submitted.
 *
 * @public
 */
let ChatInputElement = class ChatInputElement extends Themeable(Variantable(Appearanceable(Disableable(Slottable(CustomElement))))) {
    // #region Fields
    _submitted;
    _transformer;
    _value;
    _placeholder;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._value = {
            message: '',
            files: []
        };
        this._placeholder = '';
        this._transformer = (message) => ({
            message: {
                text: message.text.trim(),
                files: message.files
            },
            isValid: () => !!message.text.length
        });
        this._submitted = new HTMLElementEventEmitter(this, 'submitted', { 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 `placeholder` property.
     *
     * @public
     */
    get placeholder() {
        return this._placeholder;
    }
    set placeholder(value) {
        if (this._placeholder !== value) {
            this._placeholder = value;
            this.requestUpdate('placeholder');
        }
    }
    /**
     * Called when a new message is submitting.
     * Provides reference to `{@link IChatSubmitEventDetail}` as event detail.
     *
     * @public
     * @readonly
     * @eventProperty
     */
    get submitted() {
        return this._submitted;
    }
    // #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.onSubmitted({
                        message: {
                            text: msg.message.text,
                            files: msg.message.files
                        }
                    });
                    this.clear();
                }
            }
        }
        else {
            // log invalid message
            console.warn('[ChatInputElement]: The chat message is invalid.');
        }
    }
    /**
     * @public
     */
    clear() {
        this.value = {
            message: '',
            files: []
        };
        this.requestUpdate('value');
    }
    /**
     * @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);
        }
    }
    onSlotChanges(slotName) {
        if (slotName === 'tools') {
            const chatElement = DomServiceLocator.current.findAncestor(this, ChatElement.is, { strict: true });
            this.getSlotAssignments('tools').forEach((tool) => {
                tool.attach(chatElement);
            });
        }
    }
    /**
     * @protected
     */
    onApplyTemplate() {
        super.onApplyTemplate();
        const text = this.getTemplatePart('textBox');
    }
    /**
     * Emits the {@link submitted} event.
     *
     * @protected
     */
    onSubmitted(args) {
        this._submitted.emit(args);
    }
};
__decorate([
    Property({ type: Object }),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [Object])
], ChatInputElement.prototype, "value", null);
__decorate([
    Attribute({ type: String }),
    __metadata("design:type", String),
    __metadata("design:paramtypes", [String])
], ChatInputElement.prototype, "placeholder", null);
__decorate([
    Emit({
        eventName: 'submitted',
        bubbles: false
    }),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [])
], ChatInputElement.prototype, "submitted", null);
ChatInputElement = __decorate([
    Component({
        selector: 'mosaik-chat-input',
        template: chatInputElementTemplate,
        themes: {
            joy: chatInputElementJoyStyle,
            retro: chatInputElementRetroStyle,
            cosmopolitan: chatInputElementCosmopolitanStyle
        },
        host: {
            slot: 'input'
        },
        imports: [
            ButtonElement,
            TextBoxElement,
            FocusRingElement,
            ChipElement,
            TextElement,
            DockPanelElement
        ]
    }),
    __metadata("design:paramtypes", [])
], ChatInputElement);
export { ChatInputElement };
//# sourceMappingURL=ChatInputElement.js.map