/** @odoo-module **/

import { patch } from "@web/core/utils/patch";
import { AccountReportController } from "@odex30_account_reports/components/account_report/controller";

patch(AccountReportController.prototype, {

    async unfoldLine(lineIndex) {
        const targetLine = this.lines[lineIndex];


        if (this.options.show_full_hierarchy && targetLine.expand_function === '_report_expand_unfoldable_line_with_groupby') {

            const isCurrentlyUnfolded = targetLine.unfolded;

            if (isCurrentlyUnfolded) {
                this.options.unfolded_lines = this.options.unfolded_lines.filter(
                    id => id !== targetLine.id
                );
            } else {
                if (!this.options.unfolded_lines.includes(targetLine.id)) {
                    this.options.unfolded_lines.push(targetLine.id);
                }
            }


            this.saveSessionOptions(this.options);

            this.incrementCallNumber();

            await this.reload(null, this.options);

            return;
        }

        return super.unfoldLine(...arguments);
    },


    foldLine(lineIndex) {
        const targetLine = this.lines[lineIndex];


        if (this.options.show_full_hierarchy && targetLine.expand_function === '_report_expand_unfoldable_line_with_groupby') {

            this.options.unfolded_lines = this.options.unfolded_lines.filter(
                id => id !== targetLine.id
            );


            this.saveSessionOptions(this.options);

            this.incrementCallNumber();

            this.reload(null, this.options);

            return; // إيقاف السلوك الافتراضي
        }

        return super.foldLine(...arguments);
    }
});
