import { r as registerInstance, c as createEvent, g as getElement } from './core-58de451e.js';
import { i as enterAsClick } from './utils-3aef1637.js';
import { k as keyFromData } from './table.utils-259f424b.js';
import { T as Tunnel } from './i18n.provider-f50e5e6a.js';
var CELL_SELECTOR = 'td.anj-selectable';
var Translations;
(function (Translations) {
    Translations["SELECT_CELL"] = "Select Cell";
})(Translations || (Translations = {}));
var CellSelection = /** @class */ (function () {
    function CellSelection(hostRef) {
        var _this = this;
        registerInstance(this, hostRef);
        this.cellClickListener = function (e) { return _this.onCellClick(e); };
        this.cellKeyListener = enterAsClick(this.onCellClick.bind(this));
        this.tableWillUpdateListener = function () { return _this.onTableWillChange(); };
        this.tableDidUpdateListener = function () { return _this.onTableDidChange(); };
        this.anjCellSelect = createEvent(this, "anjCellSelect", 7);
    }
    CellSelection.prototype.componentWillLoad = function () {
        this.table = this.element.closest('anj-table');
    };
    CellSelection.prototype.componentDidLoad = function () {
        this.table.addEventListener('anjTableWillUpdate', this.tableWillUpdateListener);
        this.table.addEventListener('anjTableDidUpdate', this.tableDidUpdateListener);
    };
    CellSelection.prototype.componentDidUnload = function () {
        this.onTableWillChange();
        this.table.removeEventListener('anjTableWillUpdate', this.tableWillUpdateListener);
        this.table.removeEventListener('anjTableDidUpdate', this.tableDidUpdateListener);
    };
    CellSelection.prototype.onTableWillChange = function () {
        var _this = this;
        Array.from(this.table.querySelectorAll(CELL_SELECTOR)).forEach(function (el) {
            el.removeEventListener('click', _this.cellClickListener);
            el.removeEventListener('keyup', _this.cellKeyListener);
        });
    };
    CellSelection.prototype.onTableDidChange = function () {
        var _this = this;
        Array.from(this.table.querySelectorAll(CELL_SELECTOR)).forEach(function (el) {
            el.tabIndex = 0;
            el.setAttribute('aria-label', _this.translator(Translations.SELECT_CELL));
            el.addEventListener('click', _this.cellClickListener);
            el.addEventListener('keyup', _this.cellKeyListener);
        });
    };
    CellSelection.prototype.onCellClick = function (event) {
        var target = event.target;
        var cell = target.tagName === 'TD' ? target : target.closest('td');
        var columnField = cell.dataset.columnField;
        var rowKey = keyFromData(cell.dataset.rowKey);
        this.anjCellSelect.emit({ cell: cell, columnField: columnField, rowKey: rowKey });
    };
    Object.defineProperty(CellSelection.prototype, "element", {
        get: function () { return getElement(this); },
        enumerable: true,
        configurable: true
    });
    return CellSelection;
}());
Tunnel.injectProps(CellSelection, ['translator']);
export { CellSelection as anj_cell_selection };
