/** @odoo-module **/
// Fusion Authorizer Portal - Message Authorizer Chatter Button
// Copyright 2026 Nexa Systems Inc.
// License OPL-1
//
// Patches the Chatter component to add a "Message Authorizer" button
// that opens the mail composer targeted at the assigned authorizer.

import { Chatter } from "@mail/chatter/web_portal/chatter";
import { patch } from "@web/core/utils/patch";
import { useService } from "@web/core/utils/hooks";

patch(Chatter.prototype, {
    setup() {
        super.setup(...arguments);
        this._fapActionService = useService("action");
        this._fapOrm = useService("orm");
    },

    async onClickMessageAuthorizer() {
        const thread = this.state.thread;
        if (!thread || thread.model !== "sale.order") return;

        try {
            const result = await this._fapOrm.call(
                "sale.order",
                "action_message_authorizer",
                [thread.id],
            );
            if (result && result.type === "ir.actions.act_window") {
                if (!result.views && result.view_mode) {
                    result.views = result.view_mode.split(",").map(v => [false, v.trim()]);
                }
                this._fapActionService.doAction(result);
            }
        } catch (e) {
            console.warn("Message Authorizer action failed:", e);
        }
    },
});
