// Fusion Accounting - Bank Reconciliation Finish Buttons
// Copyright (C) 2026 Nexa Systems Inc.

import { Component, useState } from "@odoo/owl";

/**
 * BankRecFinishButtons - Action buttons displayed upon completing
 * bank reconciliation, providing navigation to journal dashboard
 * or next reconciliation batch.
 */
export class BankRecFinishButtons extends Component {
    static template = "fusion_accounting.BankRecFinishButtons";
    static props = {};

    setup() {
        this.breadcrumbs = useState(this.env.config.breadcrumbs);
    }

    getJournalFilter() {
        // retrieves the searchModel's searchItem for the journal
        return Object.values(this.searchModel.searchItems).filter(i => i.type == "field" && i.fieldName == "journal_id")[0];
    }

    get searchModel() {
        return this.env.searchModel;
    }

    get otherFiltersActive() {
        const facets = this.searchModel.facets;
        const journalFilterItem = this.getJournalFilter();
        for (const facet of facets) {
            if (facet.groupId !== journalFilterItem.groupId) {
                return true;
            }
        }
        return false;
    }

    clearFilters() {
        const facets = this.searchModel.facets;
        const journalFilterItem = this.getJournalFilter();
        for (const facet of facets) {
            if (facet.groupId !== journalFilterItem.groupId) {
                this.searchModel.deactivateGroup(facet.groupId);
            }
        }
    }

    breadcrumbBackOrDashboard() {
        if (this.breadcrumbs.length > 1) {
            this.env.services.action.restore();
        } else {
            this.env.services.action.doAction("account.open_account_journal_dashboard_kanban", {clearBreadcrumbs: true});
        }
    }
}
