// #region Imports
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { DomServiceLocator } from '../../../../Dom/Dom';
import { Disableable } from '../../../Behaviors/Disableable';
import { Selectable } from '../../../Behaviors/Selectable';
import { Themeable } from '../../../Behaviors/Themeable';
import { addInheritanceConsumerController } from '../../../Controllers/InheritanceConsumerController';
import { Component } from '../../../Decorators/ComponentDecorator';
import { TableSelectionMode } from '../../../Types/TableSelectionMode';
import { TableSelectionType } from '../../../Types/TableSelectionType';
import { CustomElement } from '../../Abstracts/CustomElement';
import { TableBodyElement } from './TableBodyElement';
import { TableElement } from './TableElement';
import { TABLE_INHERITANCE_CONTEXT } from './TableInheritanceContext';
import { tablerowElementTemplate } from './TableRowElementTemplate';
import { tableRowElementCosmopolitanStyle } from './Themes/TableRowElement.Cosmopolitan';
import { tableRowElementJoyStyle } from './Themes/TableRowElement.Joy';
import { tableRowElementMemphisStyle } from './Themes/TableRowElement.Memphis';
// #endregion
/**
 * Table Row - A horizontal grouping of table cells, representing a single data entry.
 *
 * @element mosaik-table-row
 *
 * @slot - The default slot.
 *
 * @cssprop {String} --table-row-background-color - The row background color CSS custom property.
 * @cssprop {String} --table-row-border-color - The row border color CSS custom property.
 * @cssprop {String} --table-row-border-radius - The row border radius CSS custom property.
 * @cssprop {String} --table-row-border-style - The row border style CSS custom property.
 * @cssprop {String} --table-row-border-width - The row border width CSS custom property.
 * @cssprop {String} --table-row-font-family - The row font family CSS custom property.
 * @cssprop {String} --table-row-font-letter-spacing - The row font letter spacing CSS custom property.
 * @cssprop {String} --table-row-font-line-height - The row font line height CSS custom property.
 * @cssprop {String} --table-row-font-size - The row font size CSS custom property.
 * @cssprop {String} --table-row-font-text-decoration - The row font text decoration CSS custom property.
 * @cssprop {String} --table-row-font-text-transform - The row font text transform CSS custom property.
 * @cssprop {String} --table-row-font-weight - The row font weight CSS custom property.
 * @cssprop {String} --table-row-foreground-color - The row foreground color CSS custom property.
 * @cssprop {String} --table-row-gap - The row gap CSS custom property.
 * @cssprop {String} --table-row-padding-bottom - The row padding bottom CSS custom property.
 * @cssprop {String} --table-row-padding-left - The row padding left CSS custom property.
 * @cssprop {String} --table-row-padding-right - The row padding right CSS custom property.
 * @cssprop {String} --table-row-padding-top - The row padding top CSS custom property.
 * @cssprop {String} --table-row-shadow - The row shadow CSS custom property.
 * @cssprop {String} --table-row-shadow-blur - The row shadow blur CSS custom property.
 * @cssprop {String} --table-row-shadow-color - The row shadow color CSS custom property.
 * @cssprop {String} --table-row-shadow-offset-x - The row shadow offset x CSS custom property.
 * @cssprop {String} --table-row-shadow-offset-y - The row shadow offset y CSS custom property.
 * @cssprop {String} --table-row-shadow-spread - The row shadow spread CSS custom property.
 * @cssprop {String} --table-row-transition-duration - The row transition duration CSS custom property.
 * @cssprop {String} --table-row-transition-mode - The row transition mode CSS custom property.
 * @cssprop {String} --table-row-transition-property - The row transition property CSS custom property.
 * @cssprop {String} --table-row-translate - The row translate CSS custom property.
 *
 * @fires selected {SelectedEvent} - Fired when the row is selected.
 * @fires deselected {DeselectedEvent} - Fired when the row is deselected.
 *
 * @example
 * Table row with cells:
 * ```html
 * <mosaik-table>
 *   <mosaik-table-body>
 *     <mosaik-table-row>
 *       <mosaik-table-cell text="Alice"></mosaik-table-cell>
 *       <mosaik-table-cell text="alice@example.com"></mosaik-table-cell>
 *     </mosaik-table-row>
 *   </mosaik-table-body>
 * </mosaik-table>
 * ```
 *
 * @public
 */
let TableRowElement = class TableRowElement extends Themeable(Selectable(Disableable(CustomElement))) {
    // #region Fields
    _inheritance = addInheritanceConsumerController(this, {
        context: TABLE_INHERITANCE_CONTEXT
    });
    _columnsChangedSubscription;
    _clickSubscription;
    _hasTableColumns;
    _table;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._columnsChangedSubscription = null;
        this._clickSubscription = null;
        this._hasTableColumns = false;
        this._table = null;
    }
    // #endregion
    // #region Properties
    /**
     * Returns the `is` property.
     *
     * @public
     * @static
     * @readonly
    */
    static get is() {
        return 'mosaik-table-row';
    }
    // #endregion
    // #region Methods
    onSlotChange(event) {
        const slot = event.target;
        // only set the gridTemplateColumns when the parent table has no columns property.
        if (!this._hasTableColumns) {
            this.style.gridTemplateColumns = slot.assignedElements().map(() => '1fr')
                .join(' ');
        }
    }
    /**
     * @public
     * @override
     */
    disconnectedCallback() {
        super.disconnectedCallback();
        this._columnsChangedSubscription?.dispose();
        this._clickSubscription?.dispose();
    }
    /**
     * @protected
     * @override
     */
    onApplyTemplate() {
        const table = DomServiceLocator.current.findAncestor(this, TableElement);
        if (table) {
            this._table = table;
            if (table.columns) {
                this._hasTableColumns = true;
            }
            this.style.gridTemplateColumns = table.columns;
            this._columnsChangedSubscription = table.on('columnsChanged', (d) => {
                if (table.columns) {
                    this._hasTableColumns = true;
                }
                const columns = d.detail.columns;
                this.style.gridTemplateColumns = typeof columns === 'string' ? columns : [...columns].join(' ');
            });
            // Enable row selection click handler when inside a table body
            this.updateSelectionClickHandler();
        }
    }
    /**
     * Re-evaluates the click handler when inherited selection properties change.
     *
     * @protected
     * @override
     */
    updated(changedProperties) {
        super.updated(changedProperties);
        if (changedProperties.has('selectionMode') || changedProperties.has('selectionType')) {
            this.updateSelectionClickHandler();
        }
    }
    /**
     * Sets up or tears down the click handler based on the inherited selection config.
     *
     * @private
     */
    updateSelectionClickHandler() {
        this._clickSubscription?.dispose();
        this._clickSubscription = null;
        const selectionMode = this._inheritance.get('selectionMode', TableSelectionMode.None);
        const selectionType = this._inheritance.get('selectionType', TableSelectionType.Row);
        // Only attach click handler if selection is row-based and not disabled
        if (selectionMode !== TableSelectionMode.None && selectionType === TableSelectionType.Row) {
            // Only attach if this row is in a table body (not header/footer)
            const isInBody = DomServiceLocator.current.findAncestor(this, TableBodyElement) !== null;
            if (isInBody && this._table) {
                this._clickSubscription = this.on('click', () => this.handleRowClick());
            }
        }
    }
    /**
     * Handles click events for row selection.
     *
     * @private
     */
    handleRowClick() {
        if (this.disabled || !this._table) {
            return;
        }
        this._table.handleRowSelect(this);
    }
};
TableRowElement = __decorate([
    Component({
        selector: 'mosaik-table-row',
        template: tablerowElementTemplate,
        themes: {
            joy: tableRowElementJoyStyle,
            memphis: tableRowElementMemphisStyle,
            cosmopolitan: tableRowElementCosmopolitanStyle
        },
        imports: []
    }),
    __metadata("design:paramtypes", [])
], TableRowElement);
export { TableRowElement };
//# sourceMappingURL=TableRowElement.js.map