"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SlickAutoTooltip = void 0;
const utils_1 = require("@slickgrid-universal/utils");
const index_js_1 = require("../core/index.js");
/**
 * AutoTooltips plugin to show/hide tooltips when columns are too narrow to fit content.
 * @constructor
 * @param {boolean} [options.enableForCells=true]        - Enable tooltip for grid cells
 * @param {boolean} [options.enableForHeaderCells=false] - Enable tooltip for header cells
 * @param {number}  [options.maxToolTipLength=null]      - The maximum length for a tooltip
 */
class SlickAutoTooltip {
    /** Constructor of the SlickGrid 3rd party plugin, it can optionally receive options */
    constructor(options) {
        this.pluginName = 'AutoTooltips';
        this._defaults = {
            enableForCells: true,
            enableForHeaderCells: false,
            maxToolTipLength: undefined,
            replaceExisting: true,
        };
        this._eventHandler = new index_js_1.SlickEventHandler();
        this._addonOptions = options;
    }
    get addonOptions() {
        return this._addonOptions;
    }
    get eventHandler() {
        return this._eventHandler;
    }
    /** Initialize plugin. */
    init(grid) {
        this._addonOptions = { ...this._defaults, ...this.addonOptions };
        this._grid = grid;
        if (this._addonOptions.enableForCells) {
            this._eventHandler.subscribe(this._grid.onMouseEnter, this.handleMouseEnter.bind(this));
        }
        if (this._addonOptions.enableForHeaderCells) {
            this._eventHandler.subscribe(this._grid.onHeaderMouseEnter, this.handleHeaderMouseEnter.bind(this));
        }
    }
    destroy() {
        this.dispose();
    }
    /** Dispose (destroy) the SlickGrid 3rd party plugin */
    dispose() {
        this._eventHandler?.unsubscribeAll();
    }
    // --
    // protected functions
    // ------------------
    /**
     * Handle mouse entering grid cell to add/remove tooltip.
     * @param {SlickEventData} event - The event
     */
    handleMouseEnter(event) {
        const cell = this._grid.getCellFromEvent(event);
        if (cell) {
            let node = this._grid.getCellNode(cell.row, cell.cell);
            let text;
            if (this._addonOptions && node && (!node.title || this._addonOptions?.replaceExisting)) {
                if (node.clientWidth < node.scrollWidth) {
                    text = node.textContent?.trim() ?? '';
                    if (this._addonOptions?.maxToolTipLength && text.length > this._addonOptions?.maxToolTipLength) {
                        text = text.substring(0, this._addonOptions.maxToolTipLength - 3) + '...';
                    }
                }
                else {
                    text = '';
                }
                node.title = text;
            }
            node = null;
        }
    }
    /**
     * Handle mouse entering header cell to add/remove tooltip.
     * @param {SlickEventData} event - The event
     * @param {Object} args.column - The column definition
     */
    handleHeaderMouseEnter(event, args) {
        const column = args.column;
        let node;
        const targetElm = event.target;
        if (targetElm) {
            node = targetElm.closest('.slick-header-column');
            if (node && !column?.toolTip) {
                const titleVal = targetElm.clientWidth < node.clientWidth ? (column?.name ?? '') : '';
                node.title = (0, utils_1.stripTags)((0, utils_1.getHtmlStringOutput)(titleVal, 'innerHTML'));
            }
        }
        node = null;
    }
}
exports.SlickAutoTooltip = SlickAutoTooltip;
//# sourceMappingURL=slickAutoTooltip.js.map