// #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 { HTMLElementEventEmitter } from '@breadstone/mosaik-elements';
import { DomServiceLocator } from '../../../../Dom/Dom';
import { Emit } from '../../../../events/Decorators/EmitDecorator';
import { Appearanceable } from '../../../Behaviors/Appearanceable';
import { Disableable } from '../../../Behaviors/Disableable';
import { Slottable } from '../../../Behaviors/Slottable';
import { Themeable } from '../../../Behaviors/Themeable';
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 { DockPanelElement } from '../../Layouts/Dock/DockPanelElement';
import { FocusRingElement } from '../../Primitives/FocusRing/FocusRingElement';
import { TextElement } from '../../Primitives/Text/TextElement';
import { ChipElement } from '../Chip/ChipElement';
import { ChatElement } from './ChatElement';
import { ChatInputAttachmentListElement } from './ChatInputAttachmentListElement';
import { chatInputElementTemplate } from './ChatInputElementTemplate';
import { chatInputElementCosmopolitanStyle } from './Themes/ChatInputElement.Cosmopolitan';
import { chatInputElementJoyStyle } from './Themes/ChatInputElement.Joy';
import { chatInputElementMemphisStyle } from './Themes/ChatInputElement.Memphis';
// #endregion
/**
 * Chat Input - Represents the input area for sending messages in the chat.
 *
 * @slot tools - The tools slot.
 *
 * @csspart attachments - The attachments part.
 * @csspart begin - The begin part.
 * @csspart end - The end part.
 * @csspart focusRing - The focusRing part.
 * @csspart textBox - The textBox part.
 * @csspart tools - The tools part.
 *
 * @cssprop {String} --chat-input-background-color - The input background color CSS custom property.
 * @cssprop {String} --chat-input-border-color - The input border color CSS custom property.
 * @cssprop {String} --chat-input-border-radius - The input border radius CSS custom property.
 * @cssprop {String} --chat-input-border-style - The input border style CSS custom property.
 * @cssprop {String} --chat-input-border-width - The input border width CSS custom property.
 * @cssprop {String} --chat-input-font-family - The input font family CSS custom property.
 * @cssprop {String} --chat-input-font-letter-spacing - The input font letter spacing CSS custom property.
 * @cssprop {String} --chat-input-font-line-height - The input font line height CSS custom property.
 * @cssprop {String} --chat-input-font-size - The input font size CSS custom property.
 * @cssprop {String} --chat-input-font-text-decoration - The input font text decoration CSS custom property.
 * @cssprop {String} --chat-input-font-text-transform - The input font text transform CSS custom property.
 * @cssprop {String} --chat-input-font-weight - The input font weight CSS custom property.
 * @cssprop {String} --chat-input-foreground-color - The input foreground color CSS custom property.
 * @cssprop {String} --chat-input-gap - The input gap CSS custom property.
 * @cssprop {String} --chat-input-padding-bottom - The input padding bottom CSS custom property.
 * @cssprop {String} --chat-input-padding-left - The input padding left CSS custom property.
 * @cssprop {String} --chat-input-padding-right - The input padding right CSS custom property.
 * @cssprop {String} --chat-input-padding-top - The input padding top CSS custom property.
 * @cssprop {String} --chat-input-shadow - The input shadow CSS custom property.
 * @cssprop {String} --chat-input-shadow-blur - The input shadow blur CSS custom property.
 * @cssprop {String} --chat-input-shadow-color - The input shadow color CSS custom property.
 * @cssprop {String} --chat-input-shadow-offset-x - The input shadow offset x CSS custom property.
 * @cssprop {String} --chat-input-shadow-offset-y - The input shadow offset y CSS custom property.
 * @cssprop {String} --chat-input-shadow-spread - The input shadow spread CSS custom property.
 * @cssprop {String} --chat-input-transition-duration - The input transition duration CSS custom property.
 * @cssprop {String} --chat-input-transition-mode - The input transition mode CSS custom property.
 * @cssprop {String} --chat-input-transition-property - The input transition property CSS custom property.
 * @cssprop {String} --chat-input-translate - The input translate CSS custom property.
 *
 * @fires chatSubmit {ChatSubmitEvent} - Called when a new message is submitted.
 *
 * @dependency mosaik-button - The Button element.
 * @dependency mosaik-chat-input-attachment-list - The Chat Input Attachment List element.
 * @dependency mosaik-focus-ring - The Focus Ring element.
 * @dependency mosaik-textbox - The Textbox element.
 *
 * @example
 * Basic chat input:
 * ```html
 * <mosaik-chat-input placeholder="Type a message..."></mosaik-chat-input>
 * ```
 *
 * @example
 * Disabled chat input:
 * ```html
 * <mosaik-chat-input placeholder="Unavailable" disabled></mosaik-chat-input>
 * ```
 *
 * @public
 */
let ChatInputElement = class ChatInputElement extends Themeable(Variantable(Appearanceable(Disableable(Slottable(CustomElement))))) {
    // #region Fields
    _attachmentsElement;
    _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, 'chatSubmit', { bubbles: false });
    }
    // #endregion
    // #region Properties
    /**
     * Returns the `is` property.
     * The `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 `IChatSubmitEventDetail` as event detail.
     *
     * @public
     * @readonly
     * @eventProperty
     */
    get submitted() {
        return this._submitted;
    }
    /**
     * @private
     */
    get attachmentsElement() {
        return this._attachmentsElement;
    }
    // #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);
        }
    }
    /**
     * @private
     * @template
     */
    onAttachmentAdded(detail) {
        this.value.files.push(detail.file);
        this.requestUpdate('value');
        this._attachmentsElement.requestUpdate('files');
    }
    /**
     * @private
     * @template
     */
    onAttachmentRemoved(detail) {
        this.value.files = this.value.files.filter((file) => file !== detail.file);
        this.requestUpdate('value');
        this._attachmentsElement.requestUpdate('files');
    }
    onApplyTemplate() {
        super.onApplyTemplate();
        this._attachmentsElement = this.getTemplatePart('attachments');
    }
    /**
     * @public
     * @override
     */
    onSlotChanges(slotName) {
        if (slotName === 'tools') {
            const chatElement = DomServiceLocator.current.findAncestor(this, ChatElement.is, { strict: true });
            this.getSlotAssignments('tools').forEach((tool) => {
                tool.attach(chatElement);
            });
        }
    }
    /**
     * Emits the `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: 'chatSubmitted',
        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,
            memphis: chatInputElementMemphisStyle,
            cosmopolitan: chatInputElementCosmopolitanStyle
        },
        host: {
            slot: 'input'
        },
        imports: [
            ChatInputAttachmentListElement,
            ButtonElement,
            TextBoxElement,
            FocusRingElement,
            ChipElement,
            TextElement,
            DockPanelElement
        ]
    }),
    __metadata("design:paramtypes", [])
], ChatInputElement);
export { ChatInputElement };
//# sourceMappingURL=ChatInputElement.js.map