'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const core = require('./core-859ae5a6.js');
require('./utils-2a7c8f6e.js');
const i18n_provider = require('./i18n.provider-ca81b0a0.js');
const table_utils = require('./table.utils-12a2d306.js');
const a11y = require('./a11y-fb385a78.js');

var Translations;
(function (Translations) {
    Translations["DESELECT"] = "Deselect";
    Translations["SELECT"] = "Select";
})(Translations || (Translations = {}));
const RowSelectControl = class {
    constructor(hostRef) {
        core.registerInstance(this, hostRef);
        this.clickListener = (e) => { this.onRowSelect(e); };
        /**
         * The key for the row
         */
        this.labelFormatter = this.defaultLabelFormatter;
        /**
         * The mode of selection
         * Possible values are: `"batch"` and `"single`. Defaults to "single"
         */
        this.mode = 'single';
        /**
         * If true, will render a checkbox for batch selection and a radio button for single
         */
        this.showCheck = false;
        this.anjRowSelectClick = core.createEvent(this, "anjRowSelectClick", 7);
    }
    onSelected() {
        if (!this.rowEl || this.checkOnly()) {
            return;
        }
        if (this.selected) {
            this.rowEl.classList.add('anj-selected');
        }
        else {
            this.rowEl.classList.remove('anj-selected');
        }
    }
    async componentDidLoad() {
        this.rowEl = this.element.parentElement.parentElement;
        this.onSelected();
        if (!this.checkOnly()) {
            this.rowEl.classList.add('anj-selectable');
            this.rowEl.addEventListener('click', this.clickListener);
        }
    }
    componentDidUnload() {
        if (!this.checkOnly()) {
            this.rowEl.removeEventListener('click', this.clickListener);
        }
    }
    onRowSelect(event) {
        if (event && event instanceof MouseEvent && event.composedPath().find((e) => e.tagName !== 'TR' && a11y.isFocusable(e))) {
            return;
        }
        const checkbox = event ? event.target : undefined;
        if (!checkbox || checkbox.checked !== this.selected) {
            this.selected = checkbox ? checkbox.checked : !this.selected;
            this.anjRowSelectClick.emit({ key: table_utils.keyFromData(this.rowKey) });
        }
    }
    defaultLabelFormatter(key, selected) {
        return selected ? `${this.translator(Translations.DESELECT)}: ${key}` : `${this.translator(Translations.SELECT)}: ${key}`;
    }
    checkOnly() {
        return (this.mode === 'batch' && this.showCheck);
    }
    render() {
        if (!this.showCheck)
            return;
        const label = this.labelFormatter(this.rowKey, this.selected);
        const className = 'anj-selection-control';
        return this.mode === 'batch' ?
            core.h("anj-checkbox", { value: `${this.rowKey}`, name: `${this.rowKey}-selected`, checked: this.selected, solo: true, "aria-label": label, class: className })
            : core.h("anj-radio", { value: `${this.rowKey}`, class: className, checked: this.selected, "aria-label": label, name: "anj-row-select", solo: true });
    }
    get element() { return core.getElement(this); }
    static get watchers() { return {
        "selected": ["onSelected"]
    }; }
    static get style() { return "anj-row-select-control[mode=batch] {\n  display: block;\n  line-height: 1;\n  margin: 0 0 -1px;\n}"; }
};
i18n_provider.Tunnel.injectProps(RowSelectControl, ['translator']);

exports.anj_row_select_control = RowSelectControl;
