// #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 { RendererServiceLocator } from '../../../../Dom/Renderer';
import { ScrollerServiceLocator } from '../../../../Dom/Scroller';
import { Emit } from '../../../../Events/Decorators/EmitDecorator';
import { HTMLElementEventEmitter } from '@breadstone/mosaik-elements';
import { IntlController } from '../../../../Intl/IntlController';
import { CssLength } from '@breadstone/mosaik-themes';
import { Appearanceable } from '../../../Behaviors/Appearanceable';
import { Dimensionable } from '../../../Behaviors/Dimensionable';
import { Variantable } from '../../../Behaviors/Variantable';
import { Attribute } from '../../../Decorators/AttributeDecorator';
import { Component } from '../../../Decorators/ComponentDecorator';
import { Property } from '../../../Decorators/PropertyDecorator';
import { Watch } from '../../../Decorators/WatchDecorator';
import { Appearance } from '../../../Types/Appearance';
import { ChatBegin } from '../../../Types/ChatBegin';
import { Variant } from '../../../Types/Variant';
import { CustomElement } from '../../Abstracts/CustomElement';
import { BackTopElement } from '../../Misc/BackTop/BackTopElement';
import { chatElementTemplate } from './ChatElementTemplate';
import { ChatHeaderElement } from './ChatHeaderElement';
import { ChatInputElement } from './ChatInputElement';
import { ChatMessageDividerElement } from './ChatMessageDividerElement';
import { ChatMessageElement } from './ChatMessageElement';
import { ChatElementIntl } from './Intl/ChatElementIntl';
import { chatElementJoyStyle } from './Themes/ChatElement.Joy';
import { chatElementMemphisStyle } from './Themes/ChatElement.Memphis';
import { chatElementCosmopolitanStyle } from './Themes/ChatElement.Cosmopolitan';
import { Themeable } from '../../../Behaviors/Themeable';
import { Slottable } from '../../../Behaviors/Slottable';
// #endregion
/**
 * Chat - A user interface component for real-time messaging and communication.
 *
 * @description
 * The Chat component provides a platform for users to engage in real-time text-based conversations.
 * It typically includes features such as sending and receiving messages, user avatars, timestamps, and more.
 * Chat components are commonly used in messaging applications, social media platforms, and customer support systems.
 *
 * @element mosaik-chat
 * @category Media
 *
 * @fires chatSubmitted - Called when a new message is submitted.
 *
 * @slot header - Slot for chat header content.
 * @slot message - Slot for chat message elements.
 * @slot empty - Slot for empty state content.
 * @slot input - Slot for chat input element.
 *
 * @example
 * ```html
 * <mosaik-chat>
 *   <!-- Chat messages goes here -->
 * </mosaik-chat>
 * ```
 *
 * @public
 */
let ChatElement = class ChatElement extends Themeable(Dimensionable(Appearanceable(Variantable(Slottable(CustomElement))))) {
    // #region Fields
    _intl;
    _submitted;
    _scrollToEnd;
    _begin;
    _inputElement;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._intl = new IntlController(this, {
            factory: () => new ChatElementIntl()
        });
        this._scrollToEnd = false;
        this._begin = ChatBegin.Start;
        this._submitted = new HTMLElementEventEmitter(this, 'chatSubmitted');
    }
    // #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';
    }
    /**
     * Gets or sets the `scrollToEnd` property.
     * If `true`, in order to `begin` property, the chat will scroll to top or bottom of the chat messages.
     *
     * @public
     * @attr
     */
    get scrollToEnd() {
        return this._scrollToEnd;
    }
    set scrollToEnd(value) {
        if (this._scrollToEnd !== value) {
            this._scrollToEnd = value;
            this.requestUpdate('scrollToEnd');
        }
    }
    /**
     * Gets or sets the `begin` property.
     *
     * @public
     */
    get begin() {
        return this._begin;
    }
    set begin(value) {
        if (this._begin !== value) {
            this._begin = value;
            this.requestUpdate('begin');
        }
    }
    /**
     * Returns the `intl` property.
     *
     * @public
     * @readonly
     */
    get intl() {
        return this._intl.intl;
    }
    /**
     * Returns the `input` property.
     *
     * @private
     * @readonly
     */
    get inputElement() {
        return this._inputElement;
    }
    /**
     * Called when a new message is submitted.
     * Provides reference to `{@link IChatSubmitEventDetail}` as event detail.
     *
     * @public
     * @readonly
     * @eventProperty
     */
    get submitted() {
        return this._submitted;
    }
    // #endregion
    // #region Methods
    onSlotChanges(slotName) {
        super.onSlotChanges(slotName);
        if (slotName === 'input') {
            // eslint-disable-next-line @typescript-eslint/non-nullable-type-assertion-style
            this._inputElement = this.getSlotAssignmentsOf('input', ChatInputElement).at(0);
            this._inputElement.dir = this.dir;
            this._inputElement.variant = this.variant;
            this._inputElement.placeholder = this.intl.inputPlaceholder;
            this._inputElement.on('submitted', (e) => {
                this.onSubmitted(e);
            });
        }
        if (slotName === 'message') {
            const items = this.getSlotAssignmentsOf('message', ChatMessageElement);
            // set inherited properties
            items.forEach((x) => {
                x.variant = this.variant;
                x.appearance = this.appearance;
            });
            if (this.begin === ChatBegin.End) {
                if (this._scrollToEnd) {
                    ScrollerServiceLocator.current.scrollToBottom(this.getTemplatePart('messages'), {
                        behavior: 'smooth'
                    });
                }
            }
        }
    }
    /**
     * @protected
     * @template
     */
    onSlotChange(_event) {
    }
    /**
     * @protected
     * @template
     */
    onSubmitted(event) {
        this._submitted.emit(event.detail);
        // const messageSlot = getSlot(this.shadowRoot, 'message');
        // const message = new ChatMessageElement();
        // message.text = e.detail.message as string;
        // messageSlot?.assign(message);
    }
    /**
     * @protected
     */
    onWidthPropertyChanged(_prev, next) {
        RendererServiceLocator.current.setStyle(this, 'min-width', next);
        RendererServiceLocator.current.setStyle(this, 'width', next);
    }
    /**
     * @protected
     */
    onHeightPropertyChanged(_prev, next) {
        RendererServiceLocator.current.setStyle(this, 'min-height', next);
        RendererServiceLocator.current.setStyle(this, 'height', next);
    }
    /**
     * @protected
     */
    onVariantPropertyChanged(_prev, next) {
        const items = this.getSlotAssignmentsOf('message', ChatMessageElement);
        if (next) {
            items.forEach((x) => {
                x.variant = next;
            });
        }
    }
    /**
     * @protected
     */
    onAppearancePropertyChanged(_prev, next) {
        const items = this.getSlotAssignmentsOf('message', ChatMessageElement);
        if (next) {
            items.forEach((x) => {
                x.appearance = next;
            });
        }
    }
};
__decorate([
    Attribute({
        type: Boolean,
        useDefault: true
    }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], ChatElement.prototype, "scrollToEnd", null);
__decorate([
    Property({ type: String }),
    __metadata("design:type", String),
    __metadata("design:paramtypes", [String])
], ChatElement.prototype, "begin", null);
__decorate([
    Emit({ eventName: 'chatSubmitted' }),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [])
], ChatElement.prototype, "submitted", null);
__decorate([
    Watch('width'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object]),
    __metadata("design:returntype", void 0)
], ChatElement.prototype, "onWidthPropertyChanged", null);
__decorate([
    Watch('height'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object]),
    __metadata("design:returntype", void 0)
], ChatElement.prototype, "onHeightPropertyChanged", null);
__decorate([
    Watch('variant'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", void 0)
], ChatElement.prototype, "onVariantPropertyChanged", null);
__decorate([
    Watch('appearance'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", void 0)
], ChatElement.prototype, "onAppearancePropertyChanged", null);
ChatElement = __decorate([
    Component({
        selector: 'mosaik-chat',
        template: chatElementTemplate,
        themes: {
            joy: chatElementJoyStyle,
            memphis: chatElementMemphisStyle,
            cosmopolitan: chatElementCosmopolitanStyle
        },
        host: {
            role: 'list'
        },
        imports: [
            ChatHeaderElement,
            ChatMessageElement,
            ChatMessageDividerElement,
            ChatInputElement,
            BackTopElement
        ]
    }),
    __metadata("design:paramtypes", [])
], ChatElement);
export { ChatElement };
//# sourceMappingURL=ChatElement.js.map