// Fusion Accounting - Account Report Ellipsis
// Copyright (C) 2026 Nexa Systems Inc.

import { _t } from "@web/core/l10n/translation";
import { localization } from "@web/core/l10n/localization";
import { useService } from "@web/core/utils/hooks";
import { Component, useState } from "@odoo/owl";

import { AccountReportEllipsisPopover } from "@fusion_accounting/components/account_report/ellipsis/popover/ellipsis_popover";

/**
 * AccountReportEllipsis - Handles text overflow display with a popover
 * for viewing and copying the full content of truncated report values.
 */
export class AccountReportEllipsis extends Component {
    static template = "fusion_accounting.AccountReportEllipsis";
    static props = {
        name: { type: String, optional: true },
        no_format: { optional: true },
        type: { type: String, optional: true },
        maxCharacters: Number,
    };

    setup() {
        this.popover = useService("popover");
        this.notification = useService("notification");
        this.controller = useState(this.env.controller);
    }

    //------------------------------------------------------------------------------------------------------------------
    // Ellipsis
    //------------------------------------------------------------------------------------------------------------------
    get triggersEllipsis() {
        if (this.props.name)
            return this.props.name.length > this.props.maxCharacters;

        return false;
    }

    copyEllipsisText() {
        navigator.clipboard.writeText(this.props.name);
        this.notification.add(_t("Text copied"), { type: 'success' });
        this.popoverCloseFn();
        this.popoverCloseFn = null;
    }

    showEllipsisPopover(ev) {
        ev.preventDefault();
        ev.stopPropagation();

        if (this.popoverCloseFn) {
            this.popoverCloseFn();
            this.popoverCloseFn = null;
        }

        this.popoverCloseFn = this.popover.add(
            ev.currentTarget,
            AccountReportEllipsisPopover,
            {
                name: this.props.name,
                copyEllipsisText: this.copyEllipsisText.bind(this),
            },
            {
                closeOnClickAway: true,
                position: localization.direction === "rtl" ? "left" : "right",
            },
        );
    }
}
