/** @odoo-module **/

import { Message } from "@mail/core/common/message_model";
import { patch } from "@web/core/utils/patch";

/**
 * Patch Message to display channel tags (Email, Matrix, etc.) next to the message.
 *
 * channel_tag_ids arrives from the backend as a plain JSON array:
 *   [{id: 1, name: "Email", code: "email"}, ...]
 * via Store.Attr in _to_store_defaults.
 *
 * We intercept it in update() and store it as _tierChannelTags.
 */
patch(Message.prototype, {
    /** @type {Array<{id: number, name: string, code: string}>|undefined} */
    _tierChannelTags: undefined,

    update(data) {
        super.update(data);
        if (data && Array.isArray(data.channel_tag_ids)) {
            this._tierChannelTags = data.channel_tag_ids.filter(
                (tag) => tag && typeof tag === 'object' && tag.name
            );
        }
    },

    /**
     * Returns display names of all channel tags on this message.
     * Used by message_channel_tag.xml to render badges.
     * @returns {string[]}
     */
    get tierChannelTagNames() {
        const tags = this._tierChannelTags;
        if (!tags || tags.length === 0) {
            return [];
        }
        return tags.map((tag) => tag.name).filter(Boolean);
    },
});
