// Fusion Accounting - Move Line List Reconcile
// Copyright (C) 2026 Nexa Systems Inc.

import { registry } from "@web/core/registry";
import { useSubEnv } from "@odoo/owl";
import { AccountMoveLineListController, AccountMoveLineListRenderer, AccountMoveLineListView } from "../move_line_list/move_line_list";

/**
 * AccountMoveLineReconcileListController - Extended list controller for
 * journal item reconciliation, providing selection-based reconcile actions.
 */
export class AccountMoveLineReconcileListController extends AccountMoveLineListController {

    setup() {
        super.setup();
        useSubEnv({
            callAutoReconcileAction: this.openAutoReconcileWizard.bind(this),
        });
    }

    openAutoReconcileWizard(group=null) {
        if (group) {
            return this.actionService.doAction("fusion_accounting.action_open_auto_reconcile_wizard", {
                additionalContext: {
                    domain: group.list.domain,
                }
            });
        } else {
            return this.actionService.doAction("fusion_accounting.action_open_auto_reconcile_wizard");
        }
    }
}

export class AccountMoveLineReconcileListRenderer extends AccountMoveLineListRenderer {

    static groupRowTemplate = "fusion_accounting.AccountMoveLineReconcileGroupRow";

    setup() {
        super.setup();
        this.props.list.groups?.map(group => this.toggleGroup(group));  // unfold the first groups (account_id)
    }

}

export const AccountMoveLineReconcileLineListView = {
    ...AccountMoveLineListView,
    Controller: AccountMoveLineReconcileListController,
    Renderer: AccountMoveLineReconcileListRenderer,
    buttonTemplate: "fusion_accounting.ListViewReconcile.Buttons",
}

registry.category("views").add("account_move_line_reconcile_list", AccountMoveLineReconcileLineListView);
